Ludwin Feiten

Subscribe to Ludwin Feiten: eMailAlertsEmail Alerts
Get Ludwin Feiten: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn


PowerBuilder: Article

PBDJ Feature — Working with Windows Messages in PowerBuilder

Solving practical problems

PowerBuilder provides the send() function to send messages to the Windows objects. The information about its usage is fragmentary and widely dispersed throughout the help and manuals. Since searching for this information was quite tedious, I decided to write down what information I could gather.

I hesitated to start this article with the statement that PowerBuilder is a Windows application; it seemed ridiculous. However, that's the basis for this article. Windows has an event-driven user interface and we can use the events for our own purpose.

In addition to using the Windows functions to call the windows API, we can also send messages to Windows controls. Sometimes it's helpful to send messages to the controls since they're triggered by the operating system, for example, an emulated mouse click, a keystroke, or any other event.

In Windows, messages are sent with the Windows function:

SendMessage(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam );

And PowerBuilder script provides the same functionality in the send function. The syntax is:

Send ( handle, message#, lowword, long )

With:

  • handle: A long whose value is the system handle of a window (that you have created in PowerBuilder or another application) to which you want to send a message.
  • message#: An UnsignedInteger whose value is the system message number of the message you want to send.
  • lowword: A long whose value is the integer value of the message. If this argument is not used by the message, enter 0.
  • long: The long value of the message or a string.
Note: In Windows, not only Windows are "windows" but also almost every other visual control such as Buttons, Scrollbars, Editfields, etc., that you can get a handle to.

Here are same examples from the PowerBuilder helpfile:

This statement scrolls the window w_emp up one page:

Send(Handle(w_emp), 277, 2, 0)

The following statements click the CommandButton cb_OK:

Send(Handle(Parent), 273, 0, Handle(cb_OK))

We see that it's quite easy to send Windows messages if you know the message numbers and parameters, but how do you know them? This seemed (to me) to be the most difficult part. I've looked through all the help files but couldn't find a place that contains all the information about the numbers and parameters. So I started searching.

What I found: it's not too difficult to find the information you need about messages when you know where to search. You need access to the Windows API help, which can be found at http://msdn.microsoft.com/library/. Here you have a description of all the messages you can send.

You'll also need the winuser.h file. On most installations it can be found at: C:\Program Files\Sybase\Shared\PowerBuilder\cgen\h\nt\winuser.h. (A more complete version is the winuser.h that comes with MS Visual Studio.) While the API help uses constants like WM_KEYDOWN in its description, there are numerical equivalents in the winuser.h.

I could go on by just placing a table with the event IDs and parameters for you. However, as the saying goes: Give a man a fish, and you have fed him for today. Teach a man to fish, and he will sit in a boat and drink beer all day (or teach a man to fish, and you have fed him for a lifetime.)

So get your fishing rod (the "fish" will be at the end of the article ;-)) and go for the beer (or let us solve some practical problems).

How to Do It
The first task will be to tab out a field programmatically, which triggers the modified event of that field in some applications. This is easily done by emulating the message of a pressed Tab-Key from the keyboard, i.e., we have to send the message to the window that is the same as the one sent by the OS when we press Tab-Key. But what message is sent by the keyboard?

We have to look in the Windows API help. This is a challenge and you need luck as well as a keen understanding of the Windows API help. I've found that it's not that well structured and intuitive. However, if you know that we are looking for a Notification (see Figure 1), searching with "Key pressed notification" brings us near enough to find "WM_KEYDOWN" at:

Win32 and COM Development > User Interface > Windows User Interface > User Input > Keyboard Input > Notification

This is the adequate system message, which is described as follows:

  • The WM_KEYDOWN message is posted to the window with the keyboard focus when a nonsystem key is pressed. A nonsystem key is a key that is pressed when the ALT key is not pressed.

    SYNTAX

    WM_KEYDOWN
        WPARAM wParam
        LPARAM lParam

    PARAMETERS

  • wParam: Specifies the virtual-key code of the nonsystem key.
  • lParam: Specifies the repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag, as shown in Table 1.
  • Return Value: An application should return zero if it processes this message.
The next step is to find the number of this system event. This is where winuser.h steps in. Here we search for WM_KEYDOWN and we find:

#define WM_KEYDOWN         0x0100

This is the definition of the Windows constant WM_KEYDOWN as hex 0x0100, which is dec 256. (I use the windows Calculator in the scientific view for conversion.)

wParam has to be the ASCII-Code for the Tab-Key which is 9. lParam is set to Zero.

Our call is: send(Handle(this), 256,9, long(0,0))

Scroll a Window
If you want to emulate a scroll-down event for a window object, you must find the event that is sent to the window when the scroll bar is moved. You can find it at:

Win32 and COM Development > User Interface > Windows Shell > Windows Controls > Individual Control Information > Scroll Bars >


More Stories By Ludwin Feiten

Ludwin Feiten is a senior consultant with Power People and has many years of experience in software development. He started working with PowerBuilder with version 3.1a. Ludwin runs the German PowerBuilder User Group and is a frequent speaker at PBUG meetings.

Comments (1) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
Kiran 09/11/06 12:20:42 PM EDT

Am new to Powerbuilder, this article describes how to send windows message but i would like to know how to interpret the message sent by another application, ie, how to receive and process the message using powerbuilder. Could any one help me in this regard.

Regards,
Kiran.