当文本框是滚动MouseScroll事件不会触发 [英] MouseScroll event does not trigger when TextBox is scrollable

查看:694
本文介绍了当文本框是滚动MouseScroll事件不会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现当我附上一个鼠标滚轮事件,以我的用户控件文本框,它不会触发如果文本框可滚动。这将滚动文本框并不会触发鼠标滚轮,我可以把它这样,它运行鼠标滚轮和 e.Cancel 然后做一些事情之后,使 ?文本框不滚动



更新:由于视讯及代码



视频



http://screenr.com/ZGF



代码



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

 公共主窗口()
{
的InitializeComponent();
textBox.MouseWheel + =(S,E)=>
{
随机兰特=新的随机();
的Debug.WriteLine(rand.NextDouble());
};
}


解决方案

我假定你的意思是,鼠标滚轮上的用户控件的不会触发事件。这是正常的,文本框是乐于接受的消息时,这是多行。之所以MouseWheel事件不在设计者可见。父窗口将只能看到消息时与焦点的控件不会处理它。



不知道你应该解决这个问题,用户将真正期望文本框中滚动。但是你可以通过截获的消息,使文本框不能看到它,将消息传递给家长。添加一个新类到您的项目,粘贴下面所示的代码。编译。 。从工具箱的上方新的控制

 使用系统;使用System.Windows.Forms的
;使用System.Runtime.InteropServices
;

类MyTextBox:文本框{
保护覆盖无效的WndProc(参考消息M){
如果(!m.Msg == 0x20a&放大器;&安培; this.Parent = NULL) {
PostMessage的(this.Parent.Handle,m.Msg,m.WParam,m.LParam);
}
,否则base.WndProc(REF米);
}
函数[DllImport(user32.dll中)]
私人静态外部的IntPtr PostMessage的(IntPtr的的HWND,INT味精,IntPtr的WP,IntPtr的LP);
}


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());
    };
}

解决方案

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);
}

这篇关于当文本框是滚动MouseScroll事件不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆