none
C#中如何移动和旋转几何图形 RRS feed

  • 问题

  • C#中如何移动和旋转几何图形,这几何图形实际是由若干个点组成,希望在窗体中用鼠标拖放,并可旋转或翻转,代码如何编写?请指教,谢谢!
    2019年5月18日 2:55

全部回复

  • Hi,

    如果要实现移动一个任意的图形,那么最简单的方式就是将该图形放入一个容器中,比如使用“PictureBox”。下面是相关的实现代码:

        public partial class Form1 : Form
        {
            Point mouseDownPoint = Point.Empty;
            Rectangle rect = Rectangle.Empty;
            bool isDrag = false;
            public Form1()
            {
                InitializeComponent();
            }
            private void pictureBox1_Paint(object sender, PaintEventArgs e)
            {
                Graphics g;
    
                g = e.Graphics; // 创建画板
                Pen p = new Pen(Color.Blue, 5); // 画笔颜色及宽度
                g.RotateTransform(0);
                p.DashStyle = DashStyle.Dot; // 定义虚线的样式为点
                g.DrawLine(p, 0, 0, 100, 100);
                //Rotate(g, 0);
            }
    
            private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    mouseDownPoint = e.Location;
                    rect = pictureBox1.Bounds;
                }
            }
    
            private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    isDrag = true;
                    rect.Location = getPointToForm(new Point(e.Location.X - mouseDownPoint.X, e.Location.Y - mouseDownPoint.Y));
                    this.Refresh();
                }
            }
    
            private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    if (isDrag)
                    {
                        isDrag = false;
                        pictureBox1.Location = rect.Location;
                        this.Refresh();
                    }
                    reset();
                }
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
    
            private void reset()
            {
                mouseDownPoint = Point.Empty;
                rect = Rectangle.Empty;
                isDrag = false;
            }
            private Point getPointToForm(Point p)
            {
                return this.PointToClient(pictureBox1.PointToScreen(p));
            }
    
            private void Form1_Paint(object sender, PaintEventArgs e)
            {
                if (rect != Rectangle.Empty)
                {
                    if (isDrag)
                        e.Graphics.DrawRectangle(Pens.Black, rect);
                }
                else
                {
                    e.Graphics.DrawRectangle(new Pen(this.BackColor), rect);
                }
            }
        }

    至于旋转你可以参考相关文档:Graphics.RotateTransform Method

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年5月20日 3:00
    版主
  • 按照您的代码,运行后没反应,也不报错,是否缺少什么东西?

    2019年5月21日 7:21
  • Hi,

    在你添加相关控件后,没有出现以下结果吗?你有没有将对应的方法绑定到事件?

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年5月21日 8:33
    版主
  • 试用以后,不错,谢谢!不过有个问题:这个移动后的图形会不会把以前的图形覆盖?
    2019年5月22日 7:43
  • Hi,

    在移动的过程中实际移动的是PictureBox对象。你始终操作的是同一个对象,所以并不存在覆盖一说。

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年5月22日 7:46
    版主