Make your own free website on

SendMessage/PostMessage - API makes Your programs run faster and smoother since it doesn't need to go through VB run-time for processing. Take this as Your advantage!


OK, so You want to experiment with API functions. That's a great idea because there is a lot of things You can do using API. In this article I will talk about using SendMessage and PostMessage API functions which offer pretty neat stuff.

If You more or less know how Windows works, You have also heard the concept of messages being send by system to certain controls, forms and so on, to do actions like closing the window. Well, You, as a programmer, can send the messages using SendMessage or PostMessage API functions from Your application! Both functions are very similar except for one thing: if You use SendMessage, the system will wait for the target (the object You send the message) to process the message before it will return control to Your application. PostMessage doesn't wait for the target to finish and return the control of Your application immediately after the message is sent.

SendMessage declaration looks like this:

Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

- hwnd - handle to the window or control
- wMsg - message
-wParam and lParam depend upon what wMsg is being sent

hwnd is the handle to the object You want to send message to. To obtain the handle of the controls within Your Visual Basic application use calls like form1.hwnd.

Let's look how to close the form using SendMessage API function. First, You have to decide where to declare SendMessage. If You declare it inside the form's general declaration section, declare it as Private, if in the module, as Public. To close the form, you need WM_CLOSE as wMsg parameter. Use this line to declare the constant:

    Private (or Public) Const WM_CLOSE=&H10

For WM_CLOSE message, wParam and lParam has to be set to 0. So the call to close the form1 would look like this:

   Dim rc As Long ' temp variable, no real meaning in this example

   rc=SendMessage(form1.hwnd, WM_CLOSE, 0, 0)

That's it!  You closed the form! There are a lot of messages You can send and each with it's own wParam and lParam.

Let's look at another example. Imagine You want to set the caption of the command button using SendMessage. To do this, You need different constant as wMsg parameter: WM_SETTEXT=&HC . As wParam, pass 0, and as lParam, pass the variable holding the caption string:

   Dim rc As Long
   Dim CapText as String' caption string
   CapText="Text new Text" & chr$(0)

   rc=SendMessage(command1.hwnd, WM_SETTEXT, 0, ByVal CapText)

Instead of Command1.hwnd You can use form1.hwnd and other control names.

 SendMessage is a great function to know. In the near future, I will provide You with more message constants so You could fully explore the power of SendMessage. Good Luck experimenting, and be careful - save You work before You run Your API apps

Api Tips - Home


Made By Laimonas Simutis. 2001.