How to draw and move shapes using mouse in C#

后端 未结 2 1697
庸人自扰
庸人自扰 2020-12-30 15:49

I\'m new to programming in C# and wanted to ask for a little bit of help. I\'m currently trying to move a color-filled rectangle that I draw on a Windows Application form wi

2条回答
  •  半阙折子戏
    2020-12-30 16:32

    This seems to work

        protected override void OnMouseMove(MouseEventArgs e)
        {
    
    
            if (e.Button == MouseButtons.Left)
            {
                rec.Width = e.X - rec.X;
                rec.Height = e.Y - rec.Y;
                this.Invalidate();
            }
    
            if (e.Button == MouseButtons.Right)
            {
                rec.Location = new Point((e.X-MouseDownLocation.X) + rec.Left, (e.Y-MouseDownLocation.Y) + rec.Top);
                MouseDownLocation = e.Location;
                this.Invalidate();
            }
        }
    

提交回复
热议问题