The definition for 'SendMessage' according to the Windows API:

 LRESULT SendMessage(
HWND hWnd, // Handle of destination window
UINT Msg, // Message to send
WPARAM wParam, // First message parameter
LPARAM lParam); // Second message parameter


where the hWnd parameter stands for the handle of the window receiving the messages; Msg is the value of the message sent - usually a constant like LB_FINDSTRING or WM_COPY. The WPARAM and LPARAM parameters are 32-bit integers (Type 'Long' in Visual Basic).

Both wParam and lParam depend upon the message value. The return value of a Sendmessage call can not be understood without referring to the Api-Documentation. wParam and/or the function's return value are often ignored.

The VB-Declaration, valid for VB6 and VBA6 is:

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

With this single declaration you could cover every thinkable use case.

Often SendMessage is declared several times, eg. once for numeric parameters, once for strings and once for 'Any Type'. Like so:

Declare Function SendMessageBynum Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Declare Function SendMessageByString Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Declare Function SendMessageAny Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

The declarations only vary the definition for lParam.

The idea behind all this is, that you don't have to remember, if wParam/lParam needs to be passed as a memory address (by Ref) or a number (by Value) or which type wParam/lParam needs to be.

Examples:


    Dim tabpos(0) As Long
    
    tabpos(0) = 40  ' About 10 characters
    Call SendMessageAny(List1.hwnd, LB_SETTABSTOPS, 1, tabpos(0))   'Tabulator in ListBox-Control setzen


The Api on the message LB_SETTABSTOPS (list box set tab stops) :

An application sends an LB_SETTABSTOPS message to set the tab-stop positions in a list box.

LB_SETTABSTOPS 
wParam = (WPARAM) cTabs;            // number of tab stops 
lParam = (LPARAM) (LPINT) lpnTabs;  // address of tab-stop array 

Parameters

cTabs
Value of wParam. Specifies the number of tab stops in the list box.
lpnTabs
Value of lParam. Pointer to the first member of an array of integers containing the tab stops, in dialog template units. The tab stops must be sorted in ascending order; backward tabs are not allowed.

Return Values

If all the specified tabs are set, the return value is TRUE; otherwise, it is FALSE.



   Dim st As SYSTEMTIME
   
   GetLocalTime st
   SendMessageString hStatBar, SB_SETTEXT, 3, Format$(st.wHour & st.wMinute & st.wSecond, "00:00:00")  'display time in statusbar


The Api on SB_SETTEXT (status bar set text):

 

SB_SETTEXT 
    wParam = (WPARAM) iPart | uType; 
    lParam = (LPARAM) (LPSTR) szText; 

Sets the text in the specified part of a status window.

  • Returns TRUE if successful, or FALSE otherwise.
iPart
Zero-based index of the part to set. If this value is 255, the status window is assumed to be a simple window having only one part.
uType
Type of drawing operation. This parameter can be one of the following values:
0 The text is drawn with a border to appear lower than the plane of the window.
SBT_NOBORDERS The text is drawn without borders.
SBT_OWNERDRAW The text is drawn by the parent window.
SBT_POPOUT The text is drawn with a border to appear higher than the plane of the window.
SBT_RTLREADING Displays text using right-to-left reading order on Hebrew or Arabic systems.
szText
Address of a null-terminated string that specifies the text to set. If uType is SBT_OWNERDRAW, this parameter represents 32 bits of data. The parent window must interpret the data and draw the text when it receives the WM_DRAWITEM message. The text for each part is limited to 127 characters.

The message invalidates the portion of the window that has changed, causing it to display the new text when the window next receives the WM_PAINT message.



Taken from a keypress handler in a WndProc; hwnd is the handle of a scrollable form (View Port):

       Case WM_KEYDOWN   'key pressed?
          Select Case wParam   'which one?
            Case VK_HOME       'home-key
               SendMessageByNum hwnd, WM_VSCROLL, SB_TOP, 0


The Api on WM_VSCROLL (window message vertical scroll)

The WM_VSCROLL message is sent to a window when a scroll event occurs in the window's standard vertical scroll bar. This message is also sent to the owner of a vertical scroll bar control when a scroll event occurs in the control.

WM_VSCROLL 
nScrollCode = (int) LOWORD(wParam); // scroll bar value 
nPos = (short int) HIWORD(wParam);  // scroll box position 
hwndScrollBar = (HWND) lParam;      // handle to scroll bar 

Parameters

nScrollCode
Value of the low-order word of wParam. Specifies a scroll bar value that indicates the user's scrolling request. This parameter can be one of the following values:
ValueMeaning
SB_BOTTOM Scrolls to the lower right.
SB_ENDSCROLL Ends scroll.
SB_LINEDOWN Scrolls one line down.
SB_LINEUP Scrolls one line up.
SB_PAGEDOWN Scrolls one page down.
SB_PAGEUP Scrolls one page up.
SB_THUMBPOSITION The user has dragged the scroll box (thumb) and released the mouse button. The nPos parameter indicates the position of the scroll box at the end of the drag operation.
SB_THUMBTRACK The user is dragging the scroll box. This message is sent repeatedly until the user releases the mouse button. The nPos parameter indicates the position that the scroll box has been dragged to.
SB_TOP Scrolls to the upper left.
nPos
Value of the high-order word of wParam. Specifies the current position of the scroll box if the nScrollCode parameter is SB_THUMBPOSITION or SB_THUMBTRACK; otherwise, nPos is not used.
hwndScrollBar
Value of lParam. If the message is sent by a scroll bar, then hwndScrollBar is the handle to the scroll bar control. If the message is not sent by a scroll bar, hwndScrollBar is NULL.

Return Values

If an application processes this message, it should return zero.


Other commonly used declarations:

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

 

Go to top