Win messages

From Team Developer SqlWindows Wiki
Revision as of 23:18, 6 December 2008 by DaveRabelink (Talk | contribs)

Jump to: navigation, search

This page covers Windows messages for various features.


Pointer2.png How to detect mouse leaving a window Pointer.png

When the mouse cursor is leaving a window a message is send to the window indicating this.
But to enable this feature, you will have to call the Windows API function TrackMouseEvent first to register the window.

This is the declaration for TrackMouseEvent

Library name: USER32.DLL
   Function: TrackMouseEvent
      Export Ordinal: 0
         Boolean: BOOL
            Number: DWORD
            Number: DWORD
            Window Handle: HWND
            Number: DWORD

The following list of possible constant declarations for the function TrackMouseEvent

(The NC constants are for the non-client area's like caption or borders)

! Below the flags for TrackMouseEvent

Number: TME_HOVER         = 0x0001
Number: TME_LEAVE         = 0x0002
Number: TME_NONCLIENT     = 0x0010

! Below the message constants being send on notification

Number: WM_NCMOUSEHOVER   = 0x02A0
Number: WM_MOUSEHOVER     = 0x02A1
Number: WM_NCMOUSELEAVE   = 0x02A2
Number: WM_MOUSELEAVE     = 0x02A3

! Below the mousemove message as used in the sample

Number: WM_MOUSEMOVE     = 0x0200

To register a window to receive messages when leaving or hovering the window

! Below register window to receive LEAVE notification
Call TrackMouseEvent( 16, TME_LEAVE, hWndForm, 1 )

! Below register window to receive HOVER notification
Call TrackMouseEvent( 16, TME_HOVER, hWndForm, 400 )

The next sample shows an implementation of this feature.
When the user moves the mouse within the window, the message WM_MOUSEMOVE is received.
There the window will be registered to be notified when the mouse is leaving.
A boolean (wbTracking) is used to hold if the window is allready registered.

The window will be registered until it receives a notification.

Form Window: frmSample
   Window Variables
      Boolean: wbTracking
   Message Actions
         ! The mouse is leaving the window, do custom actions here
         Set wbTracking = FALSE
         If NOT wbTracking
            If TrackMouseEvent( 16, TME_LEAVE, hWndForm, 1 )
               Set wbTracking = TRUE

Here you can download a sample:

Pointer2.png How to detect (double) clicks on a window Pointer.png

Several windows messages are send to the window when clicking.

Received when the user presses the left mousebutton
Received when the user releases the left mousebutton
Received when the user doubleclicks the left mousebutton
Received when the user presses the right mousebutton
Received when the user releases the right mousebutton
Received when the user doubleclicks the right mousebutton

Below the constant declarations for the messages

! Left button
Number: WM_LBUTTONDOWN    = 0x0201
Number: WM_LBUTTONUP      = 0x0202
Number: WM_LBUTTONDBLCLK  = 0x0203
! Right button
Number: WM_RBUTTONDOWN    = 0x0204
Number: WM_RBUTTONUP      = 0x0205
Number: WM_RBUTTONDBLCLK  = 0x0206


Pointer2.png How to drag a window without a caption Pointer.png

When a window does not have a caption or you want to be able to drag a window while clicking anywhere on the window, this is the easiest way.

First you will have to declare the next constants.

Number: WM_LBUTTONDOWN         = 0x0201
Number: HTCAPTION		= 2

When clicking on a window (anywhere in the client area) send a message to the window indicating the mouse is within the caption area.

Form Window: frmSample
   Message Actions
         ! The user clicks somewhere in the client area of the window.
         ! Send a message to trick windows the user has clicked on the caption.
         ! While moving the mouse, the window will be dragged
         Call SalSendMsg( hWndForm, WM_NCLBUTTONDOWN, HTCAPTION, NUMBER_Null )

Here you can download a sample:

Pointer2.png How to detect/suppress system commands from a window (minimise, maximise, close etc) Pointer.png

A window receives the WM_SYSCOMMAND message when the user presses any of the syscommand options of a window.
First you will have to declare the constant for this message

Number: WM_SYSCOMMAND   = 0x0112

The wParam of this message contains the action taken.
Next is a list of all possible action values.

Number: SC_MAXIMIZE         = ????

Catch the message and determine which action was taken

wParam & 0xFFF0


Pointer2.png How to detect that CTRL + SPACE was pressed within an graphical Object Pointer.png

The WM_CHAR message is posted to the window with the keyboard focus when a WM_KEYDOWN message is translated by the TranslateMessage function. The WM_CHAR message contains the character code of the key that was pressed. For Unicode there is also a WM_UNICHAR Message.
First you will have to declare the constant for this message

Number: WM_CHAR   = 0x0102

The wParam specifies the character code of the key.
The lParam specifies the repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag, as shown in the following table.

    Specifies the repeat count for the current message. 
    The value is the number of times the keystroke is autorepeated as a result of the user holding down the key. 
    If the keystroke is held long enough, multiple messages are sent. However, the repeat count is not cumulative.
    Specifies the scan code. The value depends on the OEM.
    Specifies whether the key is an extended key, such as the right-hand ALT and CTRL keys that appear on an 
    enhanced 101- or 102-key keyboard. The value is 1 if it is an extended key; otherwise, it is 0.
    Reserved; do not use.
    Specifies the context code. The value is 1 if the ALT key is held down while the key is pressed; otherwise, the value is 0.
    Specifies the previous key state. The value is 1 if the key is down before the message is sent, or it is 0 if the key is up.
    Specifies the transition state. The value is 1 if the key is being released, or it is 0 if the key is being pressed.

Catch the message and determine with GetKeyState which Keys has been pressed

   If GetKeyState( VK_CONTROL ) < 0 AND GetKeyState( VK_SPACE ) < 0
      Return FALSE

To have the GetKeyState Function availible you need to declare it in USER32.DLL like the following:

Global Declarations
   External Functions
      Library Name: USER32.DLL
         Function: GetKeyState
            Export Ordinal: 0
            Return: Number: SHORT
            Parameters: Number: INT

The Constants VK_CONTROLL and VK_SPACE are the following:

Number: VK_CONTROLL   = 0x0011
Number: VK_SPACE      = 0x0020

The Reason to have a Return FALSE in the On WM_CHAR message is, that otherwise a SPACE-Character would be printed. But in this way nothing will happens.

TODO Translate in correct English and proof if the Links are copied in the right way...