![](/img/trans.png)
[英]mouse_right click event not working for a panel in winform using c#?
[英]Mouse move event on panel - c# winform
我有一個包含按鈕的面板。 我想在鼠標懸停時移動這個面板。 我試過如下。 但是當鼠標放在按鈕上方時,面板不會移動。 僅當鼠標位於面板上時才有效。 我必須讓它在面板的任何一點上工作,而不管其中的控件如何。
this.panelLeft.Controls.Add(this.button1);
this.panelLeft.Location = new System.Drawing.Point(21, 171);
this.panelLeft.Name = "panelLeft";
this.panelLeft.Size = new System.Drawing.Size(662, 324);
this.panelLeft.TabIndex = 15;
this.panelLeft.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panelLeft_MouseDown);
this.panelLeft.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panelLeft_MouseMove);
private void panelLeft_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
MouseDownLocation = e.Location;
}
}
private void panelLeft_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
panelLeft.Left = e.X + panelLeft.Left - MouseDownLocation.X;
panelLeft.Top = e.Y + panelLeft.Top - MouseDownLocation.Y;
}
}
因為事件處理程序只是為面板添加相同的代碼到按鈕
Btn.MouseDown += panelLeft_MouseDown;
或者循環遍歷面板內的每個控件並將事件處理程序分配給它們
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.