SDK App - 4. Message Loop

Category: SDK

Date: 02-16-2022

Return to Index


 
'There are essentially 5 parts to creating a window:
'A data structure is filled out with the window properties and the CreateWindowEx
'API is used to register the window, then the ShowWindow/UpdateWindow API are used
'to display the window.  A message loop keeps the window on the screen and a message
'function is used to respond to messages sent to the window (including sending a
'PostQuitMessage() to end the message loop and close the application.
1. Define
2. Register
3. Show
4. Message Loop
5. Message Response
 
'4. Message Loop
'Windows created in an application will be destroyed once the WinMain function
'completes.  These are called Modeless windows.  To keep WinMain running,
'and hence to keep windows on the screen, a simple code loop is executed -
'as in the following example.
 
  While GetMessage(Msg, %NULL, 0, 0)
    If IsFalse ISDialogMessage (hDlg, Msg)) Then
         IsFalse TranslateAccelerator (hWnd, hAccel, Msg) Then
            TranslateMessage Msg
            DispatchMessage  Msg
         End If
    End If
  Wend
 
'More information on message loops is available on MSDN at:
http://msdn.microsoft.com/en-us/library/ms644928%28VS.85%29.aspx
 
'Note that the Msg argument is a variable of Type tagMsg, defined in the Win32API.inc file as this:
  Type tagMSG
    hwnd AS DWord         'window receiving the message
    message AS DWord      'the specific message being received
    wParam As Long        'varies with message
    lParam As Long        'varies with message
    time AS DWord         'time of the message
    pt AS POINTAPI        'xy screen coordinates of the mouse when at the time of the message
  End Type
  Local Msg AS tagMsg     'tagMsg is defined in the PowerBASIC Win32API.inc file
 
'The loop actually serves two purposes. It checks for incoming messages and
'sends those intended for the application to the application messaging function
'which was specified in the WndClassEx structure used in the CreateWindow
'statement. (see http://gbl_00353)
 
'Here's a line-by-line discussion of the message loop (also called a message pump)
While GetMessage(Msg, %NULL, 0, 0)
    'Maintains the message loop until a PostQuitMessage() is received (sent by the application
    'to tell the message loop to end). When active it retreives the next message in the
    'current thread's message queue.
 
If IsFalse ISDialogMessage (hDlg, Msg)) Then
    'This appies to dialogs only, such as the PowerBASIC DDT windows. If the message is intended
    'for the specified dialog, the message is processed by the message loop. If the window is NOT
    'a dialog, then this state is omitted.
 
IsFalse TranslateAccelerator (hWnd, hAccel, Msg) Then
    'Convert keyboard inputs, if in the accelerator table, to WM_Command or WM_SysCommand messages
 
TranslateMessage Msg
    'Translates virtual-key messages into character messages, posting the messages to the thread's message queue.
 
DispatchMessage  Msg
    'Sends messages to the window procedure of hWnd.. Typically used to dispatch a message retrieved by the GetMessage Function.
 
 
'From MSDN, here's more information on the functions used in the message loop.
 
GetMessage Function
http://msdn.microsoft.com/en-us/library/ms644936%28VS.85%29.aspx
   'hWnd must be for a window that belongs to the current thread
   'if hWnd is NULL, messages for all windows belonging to the current thread are retrieved
   'or for any messages on the current thread's message queue whose hwnd value is NULL
   'if hWnd is -1, retrieves only messages on the current thread's message queue whose hwnd value is NULL
Return Value
   'If the Function retrieves a message other than WM_QUIT, the Return value is nonzero.
   'If the Function retrieves the WM_QUIT message, the Return value is zero.
   'If there is an Error, the Return value is -1
 
 
IsDialogMessage Function
http://msdn.microsoft.com/en-us/library/ms645498%28VS.85%29.aspx
   'If the message has been processed, the Return value is nonzero, otherwise zero
   'Intended for Modeless Dialog boxes, but can use it with any Window that contains controls (handling TAB/arrow control selections)
   'Handles translating AND dispatching of messages, so messages it handles must Not be passed to the TranslateMessage Or DispatchMessage Function.
    'Sends WM_GETDLGCODE messages to the Dialog Box procedure to determine which keys should be processed.
 
 
TranslateAccelerator Function
http://msdn.microsoft.com/en-us/library/ms646373%28VS.85%29.aspx
'Processes accelerator keys for menu commands. Translates a WM_KEYDOWN Or WM_SYSKEYDOWN message
'to a WM_COMMAND Or WM_SYSCOMMAND message (if there's entry for the key in the specified accelerator table)
'AND sends the WM_COMMAND Or WM_SYSCOMMAND message directly to the specified Window procedure. Does
'note return until the Window procedure has processed the message.
    hWnd - Handle to the Window whose messages are to be translated.
    hAccTable - Handle to the accelerator table
    lpMsg - Pointer to an Msg structure
Return Value - 'zero if successful, nonzero otherwise
    'Accelerator key combinations are translated into WM_SYSCOMMAND messages
    'All other accelerator key combinations are translated into WM_COMMAND messages.
    'There are a variety of other factors affecting results of accelerator translation. See MSDN for more details.
 
 
TranslateMessage Function
http://msdn.microsoft.com/en-us/library/ms644955%28VS.85%29.aspx
'Translates virtual-key messages into character messages, posting the messages to the thread's message queue.
Return Value
    'If the message is translated (that is, a character message is posted to the thread's message queue), the return value is nonzero.
    'If the message is WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, Or WM_SYSKEYUP, the Return value is nonzero, regardless of the translation.
    'If the message is Not translated (that is, a character message is Not posted to the thread's message queue), the return value is zero.
    'WM_KEYDOWN AND WM_KEYUP combinations produce a WM_CHAR Or WM_DEADCHAR message.
    'WM_SYSKEYDOWN AND WM_SYSKEYUP combinations produce a WM_SYSCHAR Or WM_SYSDEADCHAR message.
    'TranslateMessage produces WM_CHAR messages only For keys that are mapped to ASCII characters by the keyboard driver.
 
 
DispatchMessage Function
http://msdn.microsoft.com/en-us/library/ms644934%28VS.85%29.aspx
'Dispatches a message to a Window procedure. Typically used to dispatch a message retrieved by the GetMessage Function.
Return Value
    'Is value returned by the Window procedure (generally is ignored)
    'If lParam of Msg for WM_Timer Message is not NULL, lParam points to a function that is called instead of the Window procedure
 
'gbs_00356
'Date: 03-10-2012


created by gbSnippets
http://www.garybeene.com/sw/gbsnippets.htm