What exactly is the difference between WndProc and DefaultWndProc?
There is no method named "DefaultWndProc", I'll assume you are talking about DefWndProc. This question is difficult to answer since there is very little difference between the two. The DefWndProc() method corresponds to the way you write code in a language like C, the ability to call base.WndProc() is specific to .NET. They do the same thing, call the original window procedure of the window but with a small difference. The base.WndProc() method is capable of altering the message completely, DefWndProc() can only alter the Message.Result value. I can't think of a case where this matters.
The MSDN Library article for Control.WndProc() otherwise helps to remove the doubt, it stipulates that if you override the method then you should always use base.WndProc().
what is DefaultWndProc for, which I can call anytime?
Focusing on the "anytime" part of the phrase, this is very rarely the correct thing to do. You should almost always pinvoke SendMessage() to send a message to a window. Calling DefWndProc() should only ever be used if you intentionally want to bypass a custom WndProc() method. That is rare.
And where to call base.WndProc in my overridden method?
That depends on what you want to accomplish. There are three basic strategies:
- Look at the
m
argument and implement your own custom behavior, then call base.WndProc(). This is the most common way and ought to be your default choice. - Call base.WndProc() first, then alter the
m
argument or execute code to customize the default processing of the message. This is appropriate for certain kind of messages, WM_NCHITTEST is the best example. Your WM_PAINT case is another one, if you need to paint on top of what was painted by the default window procedure then you have to do it this way. - Not call base.WndProc() at all. Appropriate if you completely customize the message handling and don't want to use the default behavior. Very common for filtering message, this is how KeyPressEventArgs.Handled works for example.
Exactly which bullet is appropriate requires insight in the way the message gets handled normally. This entirely depends on the specific control you derive from and the specific message so its impossible to give generic guidance. Getting it wrong however is almost always easy to diagnose.