如何绘制和移动在C#中使用鼠标的形状 [英] How to draw and move shapes using mouse in C#
问题描述
我是新来的C#编程,想索要一点点帮助。目前,我试图移动一个颜色填充矩形,我用我的鼠标左键Windows应用程序窗体上绘制,而我试图拖动和拖放使用我的鼠标右键另一个位置。目前我已经成功地绘制矩形,但右键点击被拖着整个表格
下面是我的代码:
公共部分Form1类:表格
{
私人点MouseDownLocation;
公共Form1中()
{
的InitializeComponent();
this.DoubleBuffered = TRUE;
}
矩形REC =新的Rectangle(0,0,0,0);
保护覆盖无效的OnPaint(PaintEventArgs的E)
{
e.Graphics.FillRectangle(Brushes.DeepSkyBlue,REC);
}
保护覆盖无效onmousedown事件(MouseEventArgs E)
{
如果(e.Button == MouseButtons.Left)
{
REC =新的Rectangle(例如,EY,0,0);
的Invalidate();
}
如果(e.Button == MouseButtons.Right)
{
MouseDownLocation = e.Location;
}
}
保护覆盖无效的OnMouseMove(MouseEventArgs E)
{
如果(e.Button == MouseButtons.Left)
{
rec.Width =的eX - rec.X;
rec.Height = e.Y - rec.Y;
的Invalidate();
}
如果(e.Button == MouseButtons.Right)
{
this.Left = e.X + this.Left - MouseDownLocation.X;
this.Top = e.Y + this.Top - MouseDownLocation.Y;
}
}
}
我只需要拖动和删除用鼠标右键矩形
修改:谢谢你我的一切我的回答速度非常快,这里有一个工作的代码:
公共部分Form1类:表格
{
私人点MouseDownLocation;
公共Form1中()
{
的InitializeComponent();
this.DoubleBuffered = TRUE;
}
矩形REC =新的Rectangle(0,0,0,0);
保护覆盖无效的OnPaint(PaintEventArgs的E)
{
e.Graphics.FillRectangle(Brushes.DeepSkyBlue,REC);
//生成形状
}
保护覆盖无效onmousedown事件(MouseEventArgs E)
{
如果(e.Button == MouseButtons.Left)
//也可以用这一个:
//如果(e.Button == System.Windows.Forms.MouseButtons.Left)
{
建议=新的Rectangle(前,EY,0,0);
的Invalidate();
}
如果(e.Button == MouseButtons.Right)
{
MouseDownLocation = e.Location;
}
}
保护覆盖无效的OnMouseMove(MouseEventArgs E)
{
如果(e.Button == MouseButtons.Left)
{
rec.Width =的eX - rec.X;
rec.Height = e.Y - rec.Y;
this.Invalidate();
}
如果(e.Button == MouseButtons.Right)
{
rec.Location =新的点((前 - MouseDownLocation.X)+ REC。左,(EY - MouseDownLocation.Y)+ rec.Top);
MouseDownLocation = e.Location;
this.Invalidate();
}
}
}
这似乎是工作。
保护覆盖无效的OnMouseMove(MouseEventArgs E)
{
如果(e.Button == MouseButtons.Left)
{
rec.Width =的eX - rec.X;
rec.Height = e.Y - rec.Y;
this.Invalidate();
}
如果(e.Button == MouseButtons.Right)
{
rec.Location =新的点((EX-MouseDownLocation.X)+ REC。左,(EY-MouseDownLocation.Y)+ rec.Top);
MouseDownLocation = e.Location;
this.Invalidate();
}
}
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 with my left mouse button, and I'm trying to drag-and-drop it to another location using my right mouse button. Currently I've managed to draw the rectangle, but the right click is dragging the entire form along.
Here's my code:
public partial class Form1 : Form
{
private Point MouseDownLocation;
public Form1()
{
InitializeComponent();
this.DoubleBuffered = true;
}
Rectangle rec = new Rectangle(0, 0, 0, 0);
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.FillRectangle(Brushes.DeepSkyBlue, rec);
}
protected override void OnMouseDown(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
rec = new Rectangle(e.X, e.Y, 0, 0);
Invalidate();
}
if (e.Button == MouseButtons.Right)
{
MouseDownLocation = e.Location;
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
rec.Width = e.X - rec.X;
rec.Height = e.Y - rec.Y;
Invalidate();
}
if (e.Button == MouseButtons.Right)
{
this.Left = e.X + this.Left - MouseDownLocation.X;
this.Top = e.Y + this.Top - MouseDownLocation.Y;
}
}
}
I only need to drag-and-drop the rectangles with right mouse button.
EDIT: Thanks to you all i got my answer very fast and here's the code that works:
public partial class Form1 : Form
{
private Point MouseDownLocation;
public Form1()
{
InitializeComponent();
this.DoubleBuffered = true;
}
Rectangle rec = new Rectangle(0, 0, 0, 0);
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.FillRectangle(Brushes.DeepSkyBlue, rec);
//Generates the shape
}
protected override void OnMouseDown(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
//can also use this one:
//if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
rec = new Rectangle(e.X, e.Y, 0, 0);
Invalidate();
}
if (e.Button == MouseButtons.Right)
{
MouseDownLocation = e.Location;
}
}
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();
}
}
}
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();
}
}
这篇关于如何绘制和移动在C#中使用鼠标的形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!