[英]How to move rectangle with mouse
我想在屏幕上移动一个矩形。 这是我当时所做的代码:
internal class GraphicContainer : Control
{
//---------------------METHODS---------------------
public GraphicContainer(Control control, string text, int left, int top)
: base(control, text, left, top, 400, 200)
protected override void OnPaint(PaintEventArgs pe)
{
// Call the OnPaint method of the base class.
base.OnPaint(pe);
// Declare and instantiate a new pen.
Pen pen = new Pen(Color.Fuchsia, 15);
SolidBrush myBrush= new System.Drawing.SolidBrush(Color.HotPink);
// Draw an aqua rectangle in the rectangle represented by the control.
//pe.Graphics.DrawRectangle(pen, new Rectangle(this.Location,this.Size));
Rectangle blublublu = new Rectangle(this.Location, this.Size - new Size(25, 25));
pe.Graphics.DrawRectangle(pen,blublublu);
pe.Graphics.FillRectangle(myBrush,blublublu);
}
protected override void OnMouseMove(MouseEventArgs e)
{
}
protected override void OnClick(EventArgs e)
{
}
}
我进行了很多搜索,没有找到应该在“ OnMouseMove”和“ OnClick”中编写什么代码才能移动鼠标。
瑞安·雷诺兹!
使blublublu成为一个领域。 您将必须使用MouseMove
, MouseUp
, MouseDown
:
Rectangle blublublu = new Rectangle(...); // possibly init in constructor
protected override void OnMouseDown(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Capture = true;
_x = e.X; // remember coords
_y = e.Y;
}
base.OnMouseDown(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
Capture = false;
base.OnMouseUp(e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
blublublu.X += _x - e.X; // not tested, maybe use Rectangle.Offset or create a new Rectangle
blublublu.Y += _y - e.Y;
Invalidate();
}
}
当鼠标按下时,您将捕获它(即使鼠标在控件之外,也可以接收mousemove事件)并记住坐标。 在鼠标移动期间,您可以通过差值(保存的鼠标位置减去电流)更改blublublu的当前坐标,然后调用Invalidate
。
您可以使Location
变量并使鼠标移动时的控件无效。 此外,只有在按下鼠标左键时才会发生这种情况
请注意,我不会覆盖OnMouseMove
,而是通过添加另一个处理程序来订阅事件。
Point mouseLocation;
public GraphicContainer(Control control, string text, int left, int top)
: base(control, text, left, top, 400, 200) {
//prevents flickering
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
//subscribes to mousemove
this.MouseMove += (obj,e) => {
//only when left mousebutton is pressed
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
//update the location
mouseLocation = e.Location;
//invalidate the control
this.Invalidate();
}
};
}
protected override void OnPaint(PaintEventArgs pe)
{
// Call the OnPaint method of the base class.
base.OnPaint(pe);
// Declare and instantiate a new pen.
Pen pen = new Pen(Color.Fuchsia, 15);
SolidBrush myBrush = new System.Drawing.SolidBrush(Color.HotPink);
// Draw an aqua rectangle in the rectangle represented by the control.
Rectangle blublublu = new Rectangle(mouseLocation, this.Size - new Size(25, 25));
pe.Graphics.DrawRectangle(pen, blublublu);
pe.Graphics.FillRectangle(myBrush, blublublu);
}
protected override void OnMouseMove(MouseEventArgs e)
{
//nothing here
}
请注意,我在构造函数中添加了this.SetStyle(...)
,因为这将防止在移动矩形时出现闪烁。 我this.Location
OnPaint
方法mouseLocation
更改为mouseLocation
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.