当 TextBox 可滚动时,MouseScroll 事件不会触发

发布于 2024-10-04 18:10:21 字数 734 浏览 5 评论 0原文

我发现当我将 MouseWheel 事件附加到我的 UserControlTextBox 时,如果 TextBox 则不会触发该事件> 是可滚动的。它将滚动文本框而不触发 MouseWheel,我可以使其运行 MouseWheel,然后在执行 e.Cancel 操作后,使 TextBox 不滚动吗?

更新:带有视频和代码

视频

http://screenr.com/ZGF

代码

http://www.mediafire.com/?x3o09dz6dr5zoym

public MainWindow()
{
    InitializeComponent();
    textBox.MouseWheel += (s, e) =>
    {
        Random rand = new Random();
        Debug.WriteLine(rand.NextDouble());
    };
}

I found that when I attach a MouseWheel event to my UserControl or a TextBox, it does not trigger if the TextBox is scrollable. It will scroll the textbox and not trigger MouseWheel, can I make it such that it runs MouseWheel and then after doing something e.Cancel it so that the TextBox does not scroll?

UPDATE: With Video & Code

Video

http://screenr.com/ZGF

Code

http://www.mediafire.com/?x3o09dz6dr5zoym

public MainWindow()
{
    InitializeComponent();
    textBox.MouseWheel += (s, e) =>
    {
        Random rand = new Random();
        Debug.WriteLine(rand.NextDouble());
    };
}

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

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

发布评论

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

评论(1

黑色毁心梦 2024-10-11 18:10:21

我假设您的意思是 UserControl 上的 MouseWheel 事件不会触发。这是正常的,当消息是多行时,文本框很乐意接受消息。 MouseWheel 事件在设计器中不可见的原因。仅当具有焦点的控件不处理该消息时,父窗口才会看到该消息。

不确定是否应该修复此问题,用户确实希望文本框滚动。但是您可以通过拦截消息使文本框看不到它并将消息传递给父级。将新类添加到您的项目中,粘贴下面所示的代码。编译。从工具箱顶部放下新控件。

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

class MyTextBox : TextBox {
    protected override void WndProc(ref Message m) {
        if (m.Msg == 0x20a && this.Parent != null) {
            PostMessage(this.Parent.Handle, m.Msg, m.WParam, m.LParam);
        }
        else base.WndProc(ref m);
    }
    [DllImport("user32.dll")]
    private static extern IntPtr PostMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
}

I assume you mean that the MouseWheel event on the UserControl won't trigger. That's normal, the TextBox is happy to accept the message when it is multiline. The reason that the MouseWheel event is not visible in the designer. The parent window will only see the message when the control with the focus won't process it.

Not sure if you should fix this, the user would really expect the text box to scroll. But you can by intercepting the message so the text box can't see it and passing the message to the parent. Add a new class to your project, paste the code shown below. Compile. Drop the new control from the top of the toolbox.

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

class MyTextBox : TextBox {
    protected override void WndProc(ref Message m) {
        if (m.Msg == 0x20a && this.Parent != null) {
            PostMessage(this.Parent.Handle, m.Msg, m.WParam, m.LParam);
        }
        else base.WndProc(ref m);
    }
    [DllImport("user32.dll")]
    private static extern IntPtr PostMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文