When programming under Windows, we often find that there are many functions that the Windows system has already implemented. If they can be called directly, it can save a lot of program writing and improve the running efficiency of the program. In many cases, we can use "Ctrl + Being able to call these functions of the system in a program eliminates the need to worry about how to implement these operations. After constant exploration, I finally found that SendMessage and PostMessage can take on this important task. It is a treasure, so I can’t wait to introduce them to all my friends.
These two API functions can be easily found using VB5's "API Browser":
DeclareFunctionSendMessageLib "user32" Alias "SendMessageA"_(ByValhwndAsLong,ByValwMsgAsLong,ByValwParamAsLong,_lParamAsAny)AsLong
DeclareFunctionPostMessageLib "user32" Alias "PostMessageA"_(ByValhwndAsLong,ByValwMsgAsLong,ByValwParamAsLong,_ByVallParamAsLong)AsLong
The functions of these two functions are almost the same, except that SendMessage directly calls the Windows function to send the message. This function returns only after the message is completely processed, while PostMessage adds a message to the message queue of the form. This message will will be handled during normal event handling at some time in the future. The following only takes SendMessage as an example.
Although there are four parameters in the function, the key ones are the first two: hwnd and wMsg. Hwnd is a handle. Each form and control in a Microsoft Windows application has a handle. The handle can indicate the operation object of the function; wMsg is a hexadecimal number that represents the specific message to be sent by the function.
The following uses specific examples to illustrate how to use SendMessage to implement the "cut", "copy", "paste", "undo" and "delete" functions:
Place a text box Text1 and five buttons in the form to perform the above five functions respectively, and write the following program.
OptionExplicit
PRivateDeclareFunctionSendMessageLib "user32" Alias "SendMessageA"_(ByValhwndAsLong,ByValwMsgAsLong,ByValwParamAsLong,lParamAsAny)AsLong
ConstWM_CUT=&H300
ConstWM_COPY=&H301
ConstWM_PAST=&H302
ConstWM_CLEAR=&H303
ConstWM_UNDO=&H304
DimfbAsLong
PrivateSubcmdClear_Click()
fb=PostMessage(Text1.hwnd,WM_CLEAR,0,0)
EndSub
PrivateSubcmdCopy_Click()
fb=SendMessage(Text1.hwnd,WM_COPY,0,0)
EndSub
PrivateSubcmdCut_Click()
fb=SendMessage(Text1.hwnd,WM_CUT,0,0)
EndSub
PrivateSubcmdPast_Click()
fb=SendMessage(Text1.hwnd,WM_PAST,0,0)
EndSub
PrivateSubcmdUndo_Click()
fb=SendMessage(Text1.hwnd,WM_UNDO,0,0)
EndSub
In addition to TextBox, SendMessage can also operate on RitchTextBox and ComboBox, etc., as long as the hwnd parameter is changed accordingly. ->