鼠标输入/离开时的C#面板可见性

时间:2014-02-16 17:15:50

标签: c# winforms mouse panel visible

请注意我正在尝试执行以下操作,当我将鼠标悬停在按钮上时我希望面板可见,当鼠标离开按钮或面板时,面板不应该可见。下面你可以看到我的代码,但面板不能保持可见。

    private void FormMain()
    {
        buttonMenu.MouseEnter += new EventHandler(buttonMenu_MouseEnter); //open panel
        buttonMenu.MouseLeave += new EventHandler(buttonMenu_MouseLeave);
        panelMenu.MouseEnter += new EventHandler(panelMenu_MouseEnter);
        panelMenu.MouseLeave += new EventHandler(panelMenu_MouseLeave);
        mbB1.MouseEnter += new EventHandler(mbB1_MouseEnter);//button in panel
        mbB2.MouseEnter += new EventHandler(mbB2_MouseEnter);//button in panel

    }

    private void buttonMenu_MouseEnter(object sender, EventArgs e)
    {
        this.panelMenu.Visible = true;
    }
    private void buttonMenu_MouseLeave(object sender, EventArgs e)
    {
        this.panelMenu.Visible = false;
    }
    private void panelMenu_MouseEnter(object sender, EventArgs e)
    {
        this.panelMenu.Visible = true;
    }
    private void panelMenu_MouseLeave(object sender, EventArgs e)
    {
        this.panelMenu.Visible = false;
    }
    private void mbB1_MouseEnter(object sender, EventArgs e)
    {
        this.panelMenu.Visible = true;
    }
    private void mbB2_MouseEnter(object sender, EventArgs e)
    {
        this.panelMenu.Visible = true;
    }

1 个答案:

答案 0 :(得分:1)

我想到的一个解决方案是使用短计时器。您可以进一步优化它以减少LoC,但它可以工作。你也可以降低计时器的延迟,但我觉得100ms是一个安全的延迟。

另一方面,我认为这不是一个好的设计。如果您需要此类行为,则应在按钮上使用ContextMenuStripClick事件,并在panelMenu.MouseLeave上使用隐藏事件。不过,如果这是你真正需要的,这就是我解决它的方式:

public partial class Form1 : Form
{
    private bool mouseInPanel;
    private Timer hideTimer;

    public Form1()
    {
        InitializeComponent();

        buttonMenu.MouseEnter += new EventHandler(button_MouseEnter);
        buttonMenu.MouseLeave += new EventHandler(button_MouseLeave);
        mbB1.MouseEnter += panelButton_MouseEnter;
        mbB2.MouseEnter += panelButton_MouseEnter;
        panelMenu.MouseEnter += new EventHandler(panelMenu_MouseEnter);
        panelMenu.MouseLeave += new EventHandler(panelMenu_MouseLeave);

        hideTimer = new Timer {Interval = 100};
        hideTimer.Tick += hidePanel;
    }

    private void button_MouseEnter(object sender, EventArgs e)
    {
        this.panelMenu.Visible = true;
    }

    private void button_MouseLeave(object sender, EventArgs e)
    {
        hideTimer.Start();
    }

    private void panelMenu_MouseEnter(object sender, EventArgs e)
    {
        mouseInPanel = true;
        this.panelMenu.Visible = true;
    }
    private void panelMenu_MouseLeave(object sender, EventArgs e)
    {
        mouseInPanel = false;
        hideTimer.Start();
    }

    private void panelButton_MouseEnter(object sender, EventArgs e)
    {
        mouseInPanel = true;
        this.panelMenu.Visible = true;
    }

    private void hidePanel(object sender, EventArgs e)
    {
        hideTimer.Stop();
        if (!mouseInPanel) this.panelMenu.Visible = false;
    }
}

我认为您的鼠标操作需要一点延迟,否则面板(以及您的按钮mbB1和mbB2)会在触发这些按钮的操作之前被隐藏。

这是因为通过输入面板按钮,您可以离开面板,并在mbB1 / mbB2的操作触发之前消失(以及接收鼠标操作的能力)。