Sending Messages

Introduction

Visual C++ revolves around messages. Messages are sent to controls from windows giving them information and instructing them to do things. Messages are also sent from controls to windows, and to other controls. Being able to send messages to controls allows you to do much more than using Visual Basic procedures. Messages can be sent using the SendMessage function in the user32.dll. To use it, simply enter the following declaration into a form. If you want to enter it into a module, and use it throughout the project, change the Private to Public.

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

All you need to know to send a message to a control is its hWnd property and the command you want to send to it. To find the command you want to use, load up the API Text Viewer, and select Constants from the API Type list box. Constants that begin with WM_ tell the control to do something, EM_ to return a value.

The advantage of being able to send these commands is that you are using the controls code, which you presume is bug free, rather than your own. This will save space as well as time. Of course if you want to do something the control can't do you have to write your own code still.

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Most software today is very much like an Egyptian pyramid with millions of bricks piled on top of each other, with no structural integrity, but just done by brute force and thousands of slaves” - Alan Kay