简体   繁体   English

如何在用户控件中拦截父窗体的 wndproc

[英]How can I intercept wndproc of parent form in usercontrol

Is there method for receive wndproc of parent form in user control?是否有在用户控件中接收父窗体的 wndproc 的方法?

I'm making titlebar user control and I want to receive windows message about form resize.我正在制作标题栏用户控件,我想接​​收有关窗体调整大小的 Windows 消息。

(this form is formborderstyle=none.) (这种形式是 formborderstyle=none。)

Since the WndProc function doesn't intercept all windows messages I got used to create a MessageFilter , since this one does filter every windows message.由于WndProc函数不会拦截我用来创建MessageFilter所有 Windows 消息,因为这个函数确实过滤了每条 Windows 消息。 So at first you create a new class which will implement the interface IMessageFilter .因此,首先您创建一个新类,它将实现IMessageFilter接口。

class MessageFilter : IMessageFilter
{
    public static IntPtr MyHandle { get; set; }

    public bool PreFilterMessage(ref Message m)
    {
        if (m.Msg == /*windows code for resizing*/ && m.HWnd == MyHandle)
        {
            //do what you desire
            return true;
        }
        else
            return false;
    }
}

So you filter all messages and wait until the message contains the code for resizing and that the code aims to your specific window.因此,您过滤所有消息并等待消息包含调整大小的代码并且该代码针对您的特定窗口。 In Windows all controls like a button, form, ... have a handle, which is unique.在 Windows 中,按钮、窗体等所有控件都有一个句柄,这是独一无二的。 The handle is used to tell to which specific control the windows message shell be send, so we can use this as a criterion for the resize detection.句柄用于告诉windows消息外壳发送到哪个特定控件,因此我们可以将其用作调整大小检测的标准。

MyHandle shell contain the window handle of your form you want to listen to it's resizing. MyHandle shell 包含您想要收听它调整大小的窗体的窗口句柄。 So you should set it eg in the constructor:所以你应该在构造函数中设置它:

MessageFilter.MyHandle = this.Handle;

Now we created our own MessageFilter , now we have to add it to your application that it listens to the windows messages.现在我们创建了我们自己的MessageFilter ,现在我们必须将它添加到您的应用程序中,它侦听 Windows 消息。 This should be set in the constructor.这应该在构造函数中设置。

MessageFilter msgFilter = new MessageFilter();

Application.AddMessageFilter(msgFilter);

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

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