如何在按下鼠标时跟踪鼠标输入事件c#visual studio?

时间:2017-12-16 18:56:16

标签: c# winforms visual-studio mouseevent

我尝试实现以下目标: 鼠标按下 - >如果鼠标进入面板,颜色会发生变化 当鼠标向上 - >面板在鼠标输入时不会改变。

- 问题:当鼠标按下时,鼠标输入事件没有反应

我该如何解决这个问题?

//for all panels in the menu
private void walletpanel0_MouseEnter(object sender, EventArgs e)
    {
        if ( mouseDown && previousPanel != (Panel)sender)
        {
            Panel p = (Panel)sender;
            p.BackColor = Color.FromArgb(255, 26, 41, 56);

            previousPanel.BackColor = Color.FromArgb(255, 44, 62, 73);

            previousPanel = p;
         }
     }

private void walletpanel0_MouseDown(object sender, MouseEventArgs e)
    {
        mouseDown = true;
        //to select the panel when clicked on it
        if (previousPanel != (Panel)sender)
        {
            Panel p = (Panel)sender;
            p.BackColor = Color.FromArgb(255, 26, 41, 56);

            previousPanel.BackColor = Color.FromArgb(255, 44, 62, 73);

            previousPanel = p;
        }
    }

1 个答案:

答案 0 :(得分:1)

据我所知,MouseEnterMouseMoveMouseLeave事件在按下鼠标按钮时不会触发。释放按钮后,如果鼠标移动在按钮关闭时触发,则会触发订阅的事件。

基本问题是当MouseDown上发生Control事件时,Control"捕获"鼠标并且在释放按钮之前不会释放它,这意味着其他Controls不会接收到移动事件。

假设您的所有Panels订阅了它,以下事件处理程序应该可以帮助您:

private void Panels_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button != MouseButtons.None)
    {
        Control control = (Control)sender;

        if (control.Capture)
            control.Capture = false;
    }
}