当移动到控件的 ScrollBar 上时会触发 MouseLeave 事件

发布于 2024-09-12 16:54:27 字数 1078 浏览 6 评论 0原文

由于无法解决问题,我重新发布此问题(原始此处< /a>)。

在 TreeView、ListBox 中,或者从我的 google 搜索看来,任何带有 ScrollBar 的东西,ScrollBar 不被视为控件的一部分。

我有一个 TreeView,我将其放入自定义控件中,它是 Dock Fill。因此,它充当自定义 TreeView,将我们所有的逻辑都集中在一个地方进行管理。

在我们程序的某些部分中,我们根据 MouseEnter 事件将其滑出,并在 MouseLeave 事件中将其滑回,但是我们目前为此使用第 3 方库的 TreeView,我的任务是替换它。

因此,我已将所有内容移至 Windows TreeView,但无法找到一种方法来可靠地捕获 MouseLeave(仅当它离开整个 TreeView(包括滚动条)时)。

我见过一种黑客解决方案,将其包装在具有多个像素的面板中并捕获面板的 MouseLeave,但我几乎不相信这是 Microsoft 在这种情况下希望我们做的事情。

简而言之:

ScrollBar 不会触发控件的 MouseEnter 或 MouseLeave,这使得使用 MouseEnter/MouseLeave 滑出控件无法使用,因为用户无法使用 ScrollBar。

处理这种情况的首选方法是什么?

在上一个问题中,我得到的建议是使用 Spy++ 并尝试附加到 WndProc() 来处理 ScrollBar 的 MouseEnter/MouseLeave。

然而,这不起作用,因为 Spy++ 显示的消息没有在表单级别或控件级别的 WndProc() 中触发。就好像 .NET 只是看不到 ScrollBar 一样。

对于这样一个简单的请求,使用 WndProc() 似乎也是不现实的,有没有其他方法可以做到这一点,或者如果 WndProc() 是唯一的方法,是否有人实际上能够实现这一目标并告诉我如何实现?

I am reposting this question due to inability to solve the problem (original here).

In the TreeView, ListBox, or it seems from my google searches anything with a ScrollBar, the ScrollBar is not considered a part of the control.

I have a TreeView that I'm putting into a custom control, and it's Dock Fill. So there it acts as a custom TreeView which has all our logic to manage it in one place.

In parts of our program we slide it out based on a MouseEnter event, and slide it back in on a MouseLeave event, however we are currently using a 3rd party library's TreeView for this, which I have been tasked with replacing.

So I've moved everything over to the Windows TreeView, but can not find a way to reliable capture the MouseLeave -only- if it leaves the entire TreeView, scrollbar included.

I've seen one hackish solution of wrapping it in a panel with several pixels and capturing the MouseLeave of the panel, but I hardly believe this is what Microsoft had intended us to do in this situation.

In Short:

The ScrollBar does not fire MouseEnter or MouseLeave for the control, and that makes using MouseEnter/MouseLeave for sliding out the control unusable since the user can not use the ScrollBar.

What is the preferred way to handle this situation?

In the previous question I was given the advice to use Spy++ and attempt to attach to WndProc() to handle MouseEnter/MouseLeave for the ScrollBar.

This however did not work as the messages Spy++ showed were not firing in the WndProc() at form level, or control level. It's as if .NET just can't see the ScrollBar.

Using WndProc() also seems unrealistic for such a simple request, is there any other way to do this, or if WndProc() is the only way, has anyone actually been able to achieve this and show me how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

稚然 2024-09-19 16:54:27

对此没有干净的解决方案。你的面板技巧也不起作用,当用户快速移动鼠标时,它将完全错过。

平底船。获得 MouseEnter 后,启动 200 毫秒计时器。在 Tick 事件中,检查鼠标是否仍在树视图上悬停。例如:

    private void treeView1_MouseEnter(object sender, EventArgs e) {
        timer1.Enabled = true;
        treeView1.Width = 220;
    }

    private void timer1_Tick(object sender, EventArgs e) {
        Point pos = treeView1.PointToClient(Cursor.Position);
        if (!treeView1.DisplayRectangle.Contains(pos)) {
            timer1.Enabled = false;
            treeView1.Width = 50;
        }
    }

顺便说一句,Application.Idle 事件也可以工作,只是有点尴尬。

There is no clean solution for this. Your panel trick doesn't work either, it will be completely missed when the user moves the mouse quickly.

Punt. Once you get MouseEnter, start a 200 msec Timer. In the Tick event, check if the mouse is still hovering the tree view. For example:

    private void treeView1_MouseEnter(object sender, EventArgs e) {
        timer1.Enabled = true;
        treeView1.Width = 220;
    }

    private void timer1_Tick(object sender, EventArgs e) {
        Point pos = treeView1.PointToClient(Cursor.Position);
        if (!treeView1.DisplayRectangle.Contains(pos)) {
            timer1.Enabled = false;
            treeView1.Width = 50;
        }
    }

The Application.Idle event works too btw, just a wee bit more awkward.

夕色琉璃 2024-09-19 16:54:27

感谢您的提示,我遇到了同样的问题,我修改了timer_Tick事件以通过将宽度添加20来包含滚动条。

    private void tmrListPos_Tick(object sender, EventArgs e)
    {
        Point posLB = clbPlants.PointToClient(Cursor.Position);
        Rectangle rectPlants = clbPlants.DisplayRectangle;
        rectPlants.Width = rectPlants.Size.Width + 20;
        if (!rectPlants.Contains(posLB))
        {
            tmrListPos.Enabled = false;
            clbPlants.Size = clbPlants.MinimumSize;
        }
    }

Thanks for the Tip was having the same issue, I modified the timer_Tick event to include the scrollbar by adding 20 to the width.

    private void tmrListPos_Tick(object sender, EventArgs e)
    {
        Point posLB = clbPlants.PointToClient(Cursor.Position);
        Rectangle rectPlants = clbPlants.DisplayRectangle;
        rectPlants.Width = rectPlants.Size.Width + 20;
        if (!rectPlants.Contains(posLB))
        {
            tmrListPos.Enabled = false;
            clbPlants.Size = clbPlants.MinimumSize;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文