我有一些关于覆盖 Windows 窗体/NativeWindow 的 WndProc 方法的问题。
WndProc 和 DefWndProc 之间到底有什么区别(编辑:我之前认为它被称为“DefaultWndProc”)?我只能覆盖 WndProc,但 DefWndProc 是做什么用的,我可以随时调用它?
在我的重写方法中在哪里调用 base.WndProc?或者我应该改为调用 DefWndProc 吗?我想到了以下职位:
protected override void WndProc(ref Message m)
{
// 1st: I call the base handler at the start, in front of my handling.
// Are there disadvantages here?
base.WndProc(ref m);
switch (m.Msg)
{
case (int)WindowsMessage.Paint:
// 2nd: Do whatever you want to do now. I could also place
// base.WndProc for each message manually here, at a point I
// can control myself. It makes the method a little messy
// since I have several base calls for each message I handle.
base.WndProc(ref m);
break;
default:
// 3rd: If I put it here, it never gets called for messages I
// have handled. I think it is disastrous for specific
// messages which need additional handling of the system.
base.WndProc(ref m);
}
}
// 4th: Or put it here. It gets called even after messages I have
// already handled. If I made some drawings in WM_PAINT, doesn't
// calling the system's default method draw "over" my paintings?
// And is this really needed?
base.WndProc(ref m);
}
你有什么建议吗?是否有最好的情况,还是很大程度上取决于我处理的消息?