Control With Scrollbar上的.NET C#MouseEnter侦听器

Run*_*CMD 8 .net c# winforms

只要鼠标位于特定控件上,我们就会显示某种形式.当鼠标离开控件时,我们会在超时后隐藏控件.这是标准的悬停行为.

但是,当控件(例如Treeview)具有滚动条,并且鼠标处于ON或滚动条上时,事件不会触发...

如果我们可以获得对滚动条控件的引用,这将解决我们的问题,因为我们会将相同的侦听器事件添加到滚动条.但是,据我所知,滚动条无法访问...

我们怎样才能解决这个问题?

Han*_*ant 3

滚动条位于树视图的非客户区。当鼠标移动到那里时,它开始生成非客户端消息,例如 WM_NCMOUSEMOVE 和 WM_NCMOUSELEAVE。您必须对 TreeView 进行子类化并重写 WndProc() 才能检测这些消息。

但这并不能真正解决你的问题,你仍然会遇到边缘情况的困难。使用计时器的低技术方法总是有效的:

    private Form frmPopup;

    private void treeView1_MouseEnter(object sender, EventArgs e) {
        timer1.Enabled = true;
        if (frmPopup == null) {
            frmPopup = new Form2();
            frmPopup.StartPosition = FormStartPosition.Manual;
            frmPopup.Location = PointToScreen(new Point(treeView1.Right + 20, treeView1.Top));
            frmPopup.FormClosed += (o, ea) => frmPopup = null;
            frmPopup.Show();
        }
    }

    private void timer1_Tick(object sender, EventArgs e) {
        Rectangle rc = treeView1.RectangleToScreen(new Rectangle(0, 0, treeView1.Width, treeView1.Height));
        if (!rc.Contains(Control.MousePosition)) {
            timer1.Enabled = false;
            if (frmPopup != null) frmPopup.Close();
        }
    }
Run Code Online (Sandbox Code Playgroud)