OVERRIDING WNDPROC

The Windows operating system sends all sorts of messages to applications that tell them about changes in the Windows environment. Messages tell forms to draw, move, resize, hide, minimize, close, respond to changes in the Windows environment, and do just about everything else related to Windows.

All Windows applications have a subroutine tucked away somewhere that responds to those messages. That routine is traditionally called a WindowProc. A Visual Basic .NET form processes these messages in a routine named WndProc. You can override that routine to take special actions when the form receives certain messages.

Example program FixedAspectRatio, which is available on the book’s website, looks for WM_SIZING messages. When it finds those messages, it adjusts the form’s new width and height so they always have the same aspect ratio (ratio of height to width).


WNDPROC WARNING
When you override the WndProc method, it is very important that the new method calls the base class’s version of WndProc as shown in the following statement:
MyBase.WndProc(m)
If the program doesn’t do this, it won’t respond properly to events. For example, the form won’t be able to draw itself correctly, resize or move itself, or even create itself properly.

When you override the WndProc method, you must also figure out what messages to intercept, what parameters those messages take, and what you can do to affect them safely. One way to learn about messages is to insert the following WndProc and then perform the action that you want to study (resizing the form, in this example):

Protected Overrides Sub WndProc(ByRef m As Message)
    Debug.WriteLine(m.ToString())
    MyBase.WndProc(m)
End Sub

Example program ViewWindowsMessages, which is available for download on the book’s website, uses this code to display information about the messages it receives.

The following statement shows the result for the WM_SIZING message sent to the form while the user resizes it. It at least shows the message name (WM_SIZING) and its numeric value (hexadecimal 0x214).

msg=0x214 (WM_SIZING) hwnd=0x30b8c wparam=0x2 lparam=0x590e29c result=0x0

Searching for the message name on the Microsoft website and on other programming sites usually gives you the other information you need to know (such as what m.WParam and m.LParam mean).

Note also that the Form class inherits the WndProc subroutine from the Control class, so all other Windows Forms controls inherit it as well. That means you can override their WndProc routines to change their behaviors.

For example, the following code shows how the NoCtxMnuTextBox class works. This control is derived from the TextBox control. Its WndProc subroutine checks for WM_CONTEXTMENU messages and calls the base class’s WndProc for all other messages. By failing to process the WM_CONTEXTMENU message, the control prevents itself from displaying the TextBox control’s normal Copy/Cut/Paste context menu when you right-click it.

Public Class NoCtxMnuTextBox
    Inherits System.Windows.Forms.TextBox
        
    Protected Overrides Sub WndProc(ByRef m As Message)
        Const WM_CONTEXTMENU As Integer = &H7B
 
        If m.Msg <> WM_CONTEXTMENU Then
            MyBase.WndProc(m)
        End If
    End Sub
End Class

The NoContextMenu example program, which is available for download on the book’s website, uses similar code to display a text box that does not display a context menu when you right-click it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.87.196