Home   .......

Using Windows API in VB Tutorial

Messages

Products  
eXamples  
Files  
e-Mail Us  
 

 

OK, now you know what API function is, but you have definitely heart of messages (if you haven't you will soon do) and wonder what this is.

Messages are the basic way Windows tells your program that some kind of input has occurred and you must process it. A message to your form is sent when the user clicks on a button, moves the mouse over it or types text in a textbox.

All messages are sent along with four parameters - a window handle, a message identifier and two 32-bit (Long) values. The window handle contains the handle of the window the message is going to. The identifier is actually the type of input occurred (click, mousemove) and the two value specify an additional information for the message (like where is the mouse cursor when the mouse is been moved).

But, when messages are sent to you, why don't you see them, looks like someone is stealing your mail. And before you get angry enough, let me tell you.

The theft is actually VB. But he does not steal your mail, but instead read it for you and give you just the most important in a better look (with some information hidden from time to time). This better look is the events you write code for.

So, when the user moves the mouse over your form, Windows sends WM_MOUSEMOVE to your window, VB get the message and its parameters and executes the code you've entered for Button_MouseMove event. Along the way, VB has transformed the second 32-bit value of the message (it contains the x and y position in pixels, 16-bit each) into two Single type value of twips.

Now, say you need the coordinates of the mouse in pixels. VB converted them into twips, and now you will convert them into pixels again. Apart from losing time, it is somehow irritating to know Windows gives you what you need and VB "favorably" alters it so that you must re-alter it. Here you will ask - Can't I receive the messages myself. OK, there is a way called SubClassing, but you should use it only if really necessary as it goes a bit against the concepts of safe programming of VB.

Something else that needs to be said: You can send massages to your own window or to another one yourself. You just call SendMessage or PostMessage (SendMessage will cause the window to process the message immediately and Post message will post it onto a queue, called message queue, after any other messages waiting to be processed (it will return after the message is processed, i.e. with some delay)). You must specify the window handle to send the message to, the message identifier (all message identifiers are available as constants in VB API Text Viewer) and the two 32-bit values.

 
Copyright (c) 1998, Billy&George Software and Peter Dimitrov
Revised March 2000