[英]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.