我是C#编程的新手,想要求一些帮助。我正在尝试使用鼠标左键移动我在Windows应用程序表单上绘制的颜色填充矩形,我正在尝试使用鼠标右键将其拖放到另一个位置。目前我已设法绘制矩形,但右键单击是拖动整个表格。
这是我的代码:
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;
}
}
}
我只需要用鼠标右键拖放矩形。
编辑:感谢大家,我的答案非常快,而且这里的代码是有效的:
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();
}
}
}
答案 0 :(得分:2)
这似乎有效
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();
}
}
答案 1 :(得分:2)
试试这个: 请注意,我在此方法中向表单添加计时器,您无需调用 在鼠标事件上无效,timertick正在调用Refresh()以便表单 将在每个刻度线上画自己。
public partial class Form1 : Form
{
private Point MouseDownLocation;
public Form1()
{
InitializeComponent();
this.DoubleBuffered = true;
timer1.Start(); // add timer to the form
}
Rectangle rec = new Rectangle(0, 0, 0, 0);
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.FillRectangle(Brushes.DeepSkyBlue, rec);
}
private void timer1_Tick(object sender, EventArgs e)
{
Refresh();
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
rec.Width = e.X - rec.X;
rec.Height = e.Y - rec.Y;
}
else if (e.Button == MouseButtons.Right)
{
rec.X = e.X - MouseDownLocation.X;
rec.Y = e.Y - MouseDownLocation.Y;
}
}
protected override void OnMouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
MouseDownLocation = e.Location;
}
}