API Tip

SendMessage/PostMessage Constants (Updated Version with numeric constant values)

       Last week we had a short intro to SendMessage and PostMessage API functions. There are thousands of messages used throughout Windows. Command buttons, edit controls (text boxes) and many other controls have their own set of messages they can send and receive. This week we will go deeper into this topic and have a look at some of the most popular and interesting Windows messages, that is, messages sent by Windows operating system to Your application.
       If You didn't read last week's API Tip, please do so to have a clear understanding what parameters SendMessage and PostMessage expect. To illustrate how the functions work, I will give You the examples of how to use the function together with constant name, its value, lParam and wParam values.

WM_CHAR
This message is sent to a control when a key is pressed on the keyboard. You can use this message to simulate keystrokes in Visual Basic.

wParam - ASCII value for the key you wish to send
lParam - set it to 1
Declaration: Private/Public Const WM_CHAR = &H102

Example:
                        dim rc as long
                        rc=SendMessage(txtText.hwnd, WM_CHAR, 86, 1)

In this example system sends a letter "V" to a text box named txtText. Using a txtText.hwnd you tell the system which control should receive the message. WM_CHAR describes the message, and wParam and lParam were described earlier. Just remember, before You use any of the API functions, declare them either in class module as public or in declaration section of a form as private. You also need to declare constants You are using in the function. (in this case it is WM_CHAR). To get the declaration of SendMessage, look at API Viewer or copy from last weeks article.

WM_COPY
This messages causes an edit control (like text box) to copy the selected part of the text of that control in the clipboard. The following example shows You how to use this function:

Constant's declaration: Private or Public Const WM_COPY = &H301
Example:
                        dim rc as long
                        rc=SendMessage(txtText.hwnd, WM_COPY, 0, 0)

 Note: lParam and wParam have to be set to 0 and before executing the code, the text inside the text box has to be selected.

WM_CUT
This messages cuts the selected text of the edit control into the clipboard. Again, lParam and wParam are set to 0.


Declaration: Private/Public Const WM_CUT = &H300
Example:
                        dim rc as long
                        rc=SendMessage(txtText.hwnd, WM_CUT, 0, 0)
 

WM_PASTE
This message causes to paste the contents of the clipboard into an edit control. 

Declaration: Private/Public Const WM_PASTE = &H302
Example:
                        dim rc as long
                        rc=SendMessage(txtText.hwnd, WM_PASTE, 0, 0)
 

WM_UNDO
This message is used to undo the last operation done in the edit control. 

Declaration: Private/Public Const WM_UNDO = &H304
Example:
                        dim rc as long
                        rc=SendMessage(txtText.hwnd, WM_UNDO, 0, 0)
 

WM_COPY, WM_CUT, WM_PASTE, and WM_UNDO are very helpful when you are writing your own text editor. The following next three message types are more useful to manipulate a window.

WM_SETTEXT
Use this message to set the text of the window (caption). It is very easy to use. Make sure you provide the text of the caption as lParam:

Declaration: Private/Public Const WM_SETTEXT = &HC
Example:
                        dim rc as Long

                        dim strText as String

                        strText="This is a new Caption!!!"
                        rc=SendMessage(Form1.hwnd, WM_SETTEXT, 0, ByVal strText)
 

WM_QUIT
Send this message to a window to request that it be destroyed.

Declaration: Private/Public Const WM_QUIT = &H12
Example:
                        dim rc as Long


                        rc=SendMessage(Form1.hwnd, WM_QUIT, 0, 0)
         

Api Tips - Home

          

Created By Laimonas Simutis. 2001. laijerrad@yahoo.com