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.
我有一个关于在申请表中使用WndProc的基本问题。我想知道当我有可用的表单事件时,WndProc方法的用途是什么。在哪些情况下需要创建自定义消息?MSDN告诉我们它只用于处理Windows消息。
2 个解决方案
#1
4
The WndProc is how WinForms provides a wrapper around the Win32 windows messages with an easier to use and understand .NET layer.
WndProc是WinForms如何围绕Win32 windows消息提供一个包装器,更容易使用和理解。net层。
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.
通常它以以下方式工作。以WM_LBUTTONDOWN windows消息为例。Windows.Forms.Control。WndProc将截获此消息并从消息的WPARAM和LPARAM中提取相关信息。然后它调用受保护的OnMouseDown虚拟方法,并将相关信息打包到MouseEventArgs中。然后,实现将在自己的处理结束时触发MouseDown事件。
So dealing with a set of OnXXXX methods/XXXX events is much easier then intercepting the windows messages directly.
因此,处理一组OnXXXX方法/XXXX事件比直接拦截windows消息要容易得多。
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.
但是,如果您感兴趣的windows消息不是WinForms处理的,该怎么办呢?在这种情况下,您可以重写WndProc并自己直接处理它。另一个用途是拦截消息,然后在控件本身有机会处理它之前丢弃它。或者可以创建自定义消息,以便在应用程序中的控件之间发送消息。当您希望查看控件接收的每个消息时,您只需在一个地方添加日志记录,这对于调试也很有用。
#2
2
.NET is a wrapper around Win32. It doesn't expose 100% of the methods, events, and properties of everything in Windows.
. net是Win32的包装。它没有100%地公开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.
但是它不是你要找的东西——当你的问题需要它时,一个好的旧的栈交换搜索通常会让你知道。
#1
4
The WndProc is how WinForms provides a wrapper around the Win32 windows messages with an easier to use and understand .NET layer.
WndProc是WinForms如何围绕Win32 windows消息提供一个包装器,更容易使用和理解。net层。
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.
通常它以以下方式工作。以WM_LBUTTONDOWN windows消息为例。Windows.Forms.Control。WndProc将截获此消息并从消息的WPARAM和LPARAM中提取相关信息。然后它调用受保护的OnMouseDown虚拟方法,并将相关信息打包到MouseEventArgs中。然后,实现将在自己的处理结束时触发MouseDown事件。
So dealing with a set of OnXXXX methods/XXXX events is much easier then intercepting the windows messages directly.
因此,处理一组OnXXXX方法/XXXX事件比直接拦截windows消息要容易得多。
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.
但是,如果您感兴趣的windows消息不是WinForms处理的,该怎么办呢?在这种情况下,您可以重写WndProc并自己直接处理它。另一个用途是拦截消息,然后在控件本身有机会处理它之前丢弃它。或者可以创建自定义消息,以便在应用程序中的控件之间发送消息。当您希望查看控件接收的每个消息时,您只需在一个地方添加日志记录,这对于调试也很有用。
#2
2
.NET is a wrapper around Win32. It doesn't expose 100% of the methods, events, and properties of everything in Windows.
. net是Win32的包装。它没有100%地公开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.
但是它不是你要找的东西——当你的问题需要它时,一个好的旧的栈交换搜索通常会让你知道。