简体   繁体   English

拦截Windows消息和Winforms

[英]Intercept Windows Messages And Winforms

So going back to the beginning of this issue. 因此,请回到本期开始。 I have using System.Windows.Forms to create a text editor. 我已经使用System.Windows.Forms创建文本编辑器。 The textbox control by default accepts an event when its contained text is double-clicked and highlights the entire row of text. 默认情况下,双击文本框控件包含的文本并突出显示整行文本时,该事件会接受一个事件。 I wanted to override this behavior with something different. 我想用其他方法覆盖此行为。 It turns out this requires that I intercept the windows message that kicks off this event and prevent it from ever getting triggered. 事实证明,这要求我拦截启动该事件的Windows消息,并防止其触发。 Here is a link I found on Stackoverflow that explains almost verbatim what I am doing: Is it possible to disable textbox from selecting part of text through double-click 这是我在Stackoverflow上找到的链接,几乎逐字解释了我在做什么: 是否可以通过双击禁用文本框来选择部分文本

But this explanation is incomplete! 但是这个解释是不完整的! Notice that DoubleClickEvent inherits from EventArgs. 请注意,DoubleClickEvent继承自EventArgs。 It should be inheriting from MouseEventArgs. 它应该继承自MouseEventArgs。 This is because PreviewDoubleClick is going to need information from the windows message about where the screen was clicked. 这是因为PreviewDoubleClick将需要Windows消息中有关单击屏幕位置的信息。 So that's the task I set off to accomplish: override the doubleclick event but still send it all of the click information from the windows message. 所以这就是我要完成的任务:覆盖doubleclick事件,但仍将Windows消息中的所有click信息发送给它。

If I set up breakpoints in the code, I begin to see where my information is stored. 如果我在代码中设置了断点,我将开始查看信息的存储位置。 Message m contains a couple properties, one of which is called LParam. 消息m包含几个属性,其中一个称为LParam。 As I understand, this is a pointer to the click information I want to retrieve. 据我了解,这是我要检索的点击信息的指针。 But this isn't C++... I cannot just dereference the pointer. 但这不是C ++。。。我不能仅仅取消引用指针。 .NET conveniently provides the Message.GetLParam method to help me out. .NET方便地提供了Message.GetLParam方法来帮助我。 I am having trouble getting this method to work. 我无法使这种方法起作用。

Here is my code: 这是我的代码:

    public class DoubleClickEventArgs : MouseEventArgs
{
    public DoubleClickEventArgs(MouseButtons Button, int clicks, int x, int y, int delta) : base (Button, clicks, x, y, delta) { }


    public bool Handled
    {
        get;
        set;
    }
}

public class NewTextBox : System.Windows.Forms.TextBox
{
    public event EventHandler<DoubleClickEventArgs> NewDoubleClick;
    private const int WM_DBLCLICK = 0xA3;
    private const int WM_LBUTTONDBLCLK = 0x203;

    protected override void WndProc(ref Message m)
    {
        if ((m.Msg == WM_DBLCLICK) || (m.Msg == WM_LBUTTONDBLCLK))
        {
            DoubleClickEventArgs e = (DoubleClickEventArgs) m.GetLParam(typeof(MouseEventArgs));

            if (NewDoubleClick != null)
            {
                NewDoubleClick(this, e);
            }

            if (e.Handled)
            {
                return;
            }
        }

        base.WndProc(ref m);
    }
}

As you can see it, looks a lot like the sample. 如您所见,它看起来很像示例。 The crazy casting I did to get it to compile on this line DoubleClickEventArgs e = (DoubleClickEventArgs) m.GetLParam(typeof(MouseEventArgs)); 我做了疯狂的转换,使其可以在此行上编译DoubleClickEventArgs e =(DoubleClickEventArgs)m.GetLParam(typeof(MouseEventArgs)); is obviously causing the crash. 显然是造成车祸的原因。 We get the error: No parameterless constructor defined for this object. 我们得到错误:没有为此对象定义无参数构造函数。

That makes sense. 那讲得通。 DoubleCLickEventArgs do require parameters to be created. DoubleCLickEventArgs确实需要创建参数。 But the parameters are defined by the lParam pointer... Can anyone give me a little guidance here? 但是参数是由lParam指针定义的。有人可以在这里给我一点指导吗? I'm struggling. 我正在挣扎。

To remove the highlight text behavior and provide your own: 要删除突出显示文本行为并提供您自己的文本,请执行以下操作:

private const int WM_DBLCLICK = 0xA3;
private const int WM_LBUTTONDBLCLK = 0x203;

protected override void WndProc(ref Message m)
{
    if ((m.Msg == WM_DBLCLICK) || (m.Msg == WM_LBUTTONDBLCLK))
    {
        //your behavior here like for example
        //this.ForeColor = Color.Red;
        return;
    }
    base.WndProc(ref m);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM