Explanation of SendMessage message numbers?

£可爱£侵袭症+ 提交于 2019-12-10 12:42:49

问题


I've successfully used the Windows SendMessage method to help me do various things in my text editor, but each time I am just copying and pasting code suggested by others, and I don't really know what it means. There is always a cryptic message number that is a parameter. How do I know what these code numbers mean so that I can actually understand what is happening and (hopefully) be a little more self-sufficient in the future? Thanks.

Recent example:

using System.Runtime.InteropServices;

[DllImport("user32.dll")]
static extern int SendMessage(IntPtr hWnd, uint wMsg,UIntPtr wParam, IntPtr lParam);

SendMessage(myRichTextBox.Handle, (uint)0x00B6, (UIntPtr)0, (IntPtr)(-1));

回答1:


This is the windows message code.
They are defined in the header files, and generally available translated as an include of some sort with different languages.

example:
WM_MOUSEMOVE = &H200
MK_CONTROL = &H8
MK_LBUTTON = &H1
MK_MBUTTON = &H10
MK_RBUTTON = &H2
MK_SHIFT = &H4
MK_XBUTTON1 = &H20
MK_XBUTTON2 = &H40

see http://msdn.microsoft.com/en-us/library/ms644927(VS.85).aspx#windows_messages.




回答2:


Each message in Windows is signified by a number. When coding using the Windows SDK in native code, these are provided by defines such as WM_CHAR, WM_PAINT, or LVM_GETCOUNT, but these defines are not carried over to the .NET framework because in the most part, the messages are wrapped by .NET events like OnKeyPressed and OnPaint, or methods and properties like ListView.Items.Count.

On the occasion where there is no wrapped event, property or method, or you need to do something that isn't supported at the higher level, you need the message numbers that Windows uses underneath the .NET framework.

The best way to find out what a particular number actually means is to look in the Windows SDK headers and find the definition for that number.




回答3:


Maybe you'll find the code at http://www.codeproject.com/KB/cs/cswindowsmessages.aspx useful. It provides a C# enumeration of many common windows messages.




回答4:


Among all the examples I found on the internet, this one is the best because it is the only one that takes care of showing the using System.Runtime.InteropServices. Without this directive I could not have SendMessage working in c#. Also very helpful is the list of Windows Messages this page offer. Here I repeat the example:

using System.Runtime.InteropServices; 

[DllImport("user32.dll")] 
static extern int SendMessage(IntPtr hWnd, uint wMsg,UIntPtr wParam, IntPtr lParam); 

SendMessage(myRichTextBox.Handle, (uint)0x00B6, (UIntPtr)0, (IntPtr)(-1)); 



回答5:


Try out the PInvoke interop Assistant. This tool has a search functionality that allows you to browse through all of the defined windows messages. It will then generate the correct C#/VB.Net code for those values.

It's also not limited to just constant values. It can also generated structs, functions, function pointers and enums.



来源:https://stackoverflow.com/questions/206221/explanation-of-sendmessage-message-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!