Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should i use WndProc when i have form events?

I have a basic question about using WndProc in my form application. I want to know what is the use of the WndProc method when i have form events available. In which cases do i need to create custom messages? MSDN tells that it is used only to process the Windows messages.

like image 638
badmaash Avatar asked Apr 16 '12 05:04

badmaash


2 Answers

The WndProc is how WinForms provides a wrapper around the Win32 windows messages with an easier to use and understand .NET layer.

Typically it works in the following way. Take the example of the WM_LBUTTONDOWN windows message. The Windows.Forms.Control.WndProc will intercept this message and extract relevant information from the WPARAM and LPARAM of the message. Then it calls the protected virtual method OnMouseDown with relevant information nicely packaged into an MouseEventArgs. The implementation will then fire the MouseDown event at the end of its own processing.

So dealing with a set of OnXXXX methods/XXXX events is much easier then intercepting the windows messages directly.

But what if the windows message you are interested in is not handled by WinForms? In that case you can override the WndProc and handle it yourself directly. Another use is to intercept a message and then discard it before the control itself has a chance to process it. Or you can create custom messages for sending between controls within your application. Also useful for debugging when you want to see every message your control receives, you only need to add logging in the one place.

like image 52
Phil Wright Avatar answered Dec 20 '22 00:12

Phil Wright


.NET is a wrapper around Win32. It doesn't expose 100% of the methods, events, and properties of everything in Windows.

So sometimes you need to go under the covers.

However it isn't something you go looking for - when your problem requires it, a good ol' Stack Exchange search will usually let you know.

like image 20
John Arlen Avatar answered Dec 19 '22 23:12

John Arlen