Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

NativeWindow.WndProc Method

Invokes the default window procedure associated with this window.

[Visual Basic]
Protected Overridable Sub WndProc( _
   ByRef m As Message _
)
[C#]
protected virtual void WndProc(
   ref Message m
);
[C++]
protected: virtual void WndProc(
 Message* m
);
[JScript]
protected function WndProc(
   m : Message
);

Parameters

m
A Message that is associated with the current Windows message.

Remarks

This method is called when a window message is sent to the handle of the window.

Notes to Inheritors:  Override this method to implement specific message processing. Call base.WndProc for unhandled messages.

Example

[Visual Basic, C#, C++] The following example demonstrates intercepting operating system window messages in a window procedure. The example creates a class that inherits from NativeWindow to accomplish this.

[Visual Basic, C#, C++] The MyNativeWindowListener class hooks into the window procedure of the form passed into the constructor, and overrides the WndProc method to intercept the WM_ACTIVATEAPP window message. The class demonstrates the use of the AssignHandle and ReleaseHandle methods to identify the window handle the NativeWindow will use. The handle is assigned based upon the Control.HandleCreated and Control.HandleDestroyed events. When the WM_ACTIVATEAPP window message is received, the class calls the form1 ApplicationActivated method.

[Visual Basic, C#, C++] This code is an excerpt from the example shown in the NativeWindow class overview. Some code is not shown for the purpose of brevity. See NativeWindow for the whole code listing.

[Visual Basic] 
' NativeWindow class to listen to operating system messages.
Public Class MyNativeWindowListener
    Inherits NativeWindow

    ' Constant value was found in the "windows.h" header file.
    Private Const WM_ACTIVATEAPP As Integer = &H1C

    Private parent As Form1

    Public Sub New(ByVal parent As Form1)

        AddHandler parent.HandleCreated, AddressOf Me.OnHandleCreated
        AddHandler parent.HandleDestroyed, AddressOf Me.OnHandleDestroyed
        Me.parent = parent
    End Sub

    ' Listen for the control's window creation and hook into it.    
    Private Sub OnHandleCreated(ByVal sender As Object, ByVal e As EventArgs)
        ' Window is now created, assign handle to NativeWindow.
        AssignHandle(CType(sender, Form).Handle)
    End Sub

    Private Sub OnHandleDestroyed(ByVal sender As Object, ByVal e As EventArgs)
        ' Window was destroyed, release hook.
        ReleaseHandle()
    End Sub
    
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
    Protected Overrides Sub WndProc(ByRef m As Message)
        ' Listen for operating system messages

        Select Case (m.Msg)
            Case WM_ACTIVATEAPP

                ' Notify the form that this message was received.
                ' Application is activated or deactivated, 
                ' based upon the WParam parameter.
                parent.ApplicationActived(m.WParam.ToInt32() <> 0)

        End Select

        MyBase.WndProc(m)
    End Sub
End Class

[C#] 
// NativeWindow class to listen to operating system messages.
public class MyNativeWindowListener: NativeWindow{

    // Constant value was found in the "windows.h" header file.
    private const int WM_ACTIVATEAPP = 0x001C;

    private Form1 parent;

    public MyNativeWindowListener(Form1 parent){

        parent.HandleCreated += new EventHandler(this.OnHandleCreated);
        parent.HandleDestroyed+= new EventHandler(this.OnHandleDestroyed);
        this.parent = parent;
    }

    // Listen for the control's window creation and then hook into it.
    internal void OnHandleCreated(object sender, EventArgs e){
        // Window is now created, assign handle to NativeWindow.
        AssignHandle(((Form1)sender).Handle);
    }
    internal void OnHandleDestroyed(object sender, EventArgs e) {
        // Window was destroyed, release hook.
        ReleaseHandle();
    }
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
    protected override void WndProc(ref Message m){
        // Listen for operating system messages

        switch (m.Msg){
            case WM_ACTIVATEAPP:

                // Notify the form that this message was received.
                // Application is activated or deactivated, 
                // based upon the WParam parameter.
                parent.ApplicationActived(((int)m.WParam != 0));

                break;                
        }
        base.WndProc(ref m);
    }        
}

[C++] 
// NativeWindow class to listen to operating system messages.
__gc class MyNativeWindowListener : public NativeWindow {

    // Constant value was found in the S"windows.h" header file.
private:
    const static int  WM_ACTIVATEAPP = 0x001C;
    Form1*  parent;

public:
    MyNativeWindowListener(Form1* parent) {
        parent->HandleCreated += new EventHandler(this, &MyNativeWindowListener::OnHandleCreated);
        parent->HandleDestroyed += new EventHandler(this, &MyNativeWindowListener::OnHandleDestroyed);
        this->parent = parent;
    }

    public private: 
        // Listen for the control's window creation and then hook into it.
        void OnHandleCreated(Object* sender, EventArgs* /*e*/) {
            // Window is now created, assign handle to NativeWindow.
            AssignHandle((dynamic_cast<Form1*>(sender))->Handle);
        }
        void OnHandleDestroyed(Object* /*sender*/, EventArgs* /*e*/) {
            // Window was destroyed, release hook.
            ReleaseHandle();
        }

protected:
    [System::Security::Permissions::PermissionSet(System::Security::Permissions::SecurityAction::Demand, Name="FullTrust")] 
    void WndProc(Message* m) {
        // Listen for operating system messages

        switch (m->Msg) {
            case WM_ACTIVATEAPP:

                // Notify the form that this message was received.
                // Application is activated or deactivated,
                // based upon the WParam parameter.
                parent->ApplicationActived(((int)m->WParam != 0));

                break;
        }
        NativeWindow::WndProc(m);

    }
};

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

NativeWindow Class | NativeWindow Members | System.Windows.Forms Namespace | NativeWindow | Handle | AssignHandle | CreateHandle | DestroyHandle | ReleaseHandle | Message

Show:
© 2015 Microsoft