简体   繁体   English

面板上的鼠标移动事件 - c# winform

[英]Mouse move event on panel - c# winform

I have a panel which contains a button.我有一个包含按钮的面板。 I want to move this panel on mouse over.我想在鼠标悬停时移动这个面板。 I ahve tried like below.我试过如下。 But when mouse put above the button, the panel is not moving.但是当鼠标放在按钮上方时,面板不会移动。 Only working when mouse is on the panel.仅当鼠标位于面板上时才有效。 I have to make it work on any point of panel irrespective of controls in it.我必须让它在面板的任何一点上工作,而不管其中的控件如何。

        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;
        }
    }

Because the event handler is only added for panel add the same code to the button因为事件处理程序只是为面板添加相同的代码到按钮

Btn.MouseDown += panelLeft_MouseDown;

Or loop over each control inside panel and assign the event handler to them或者循环遍历面板内的每个控件并将事件处理程序分配给它们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM