Changing How Pop-Up Menus Respond to Mouse Actions

Last reviewed: November 2, 1995
Article ID: Q65256
The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK) versions 3.0 and 3.1
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows NT versions 3.5 and 3.51
        - Microsoft Windows 95 version 4.0
    

SUMMARY

The TrackPopupMenu function allows an application to receive input from a menu that is displayed anywhere within the application's client area. This article demonstrates how to change the menu's default behavior for mouse selections.

MORE INFORMATION

The default action for floating pop-up menus maintained with TrackPopupMenu is as follows:

  1. If the menu is displayed in response to a keystroke, the pop-up menu is visible until the user selects a menu item or presses ESC.

  2. If the menu is displayed in response to a WM_*BUTTONUP message, it acts as if it were displayed in response to a keystroke.

NOTE: In the context of this article, the * in the WM_*BUTTONUP and WM_*BUTTONDOWN messages can be L (left mouse button), M (middle mouse button), or R (right mouse button).

An application can change the behavior of a floating pop-up menu displayed in response to a WM_*BUTTONDOWN message to keep it visible after the mouse button is released. However, when an application uses the techniques described below, it changes the menu's user interface. Specifically, to change menu selections with the mouse, the user must first release the mouse button and then press it again. Dragging the mouse between items with the button down, without releasing the button at least once, will not change the selection.

To cause a floating pop-up menu to remain visible after it is displayed in response to a WM_*BUTTONDOWN message, follow these four steps:

  1. In the application, allocate a 256 byte buffer to hold the key state.

  2. Call the GetKeyboardState function with a far pointer to the buffer to retrieve the keyboard state.

  3. Set the keyboard state for the VK_*BUTTON index in the keyboard state array to 0.

  4. Call SetKeyboardState with a far pointer to the buffer to register the change with Windows.

The keyboard state array is 256 bytes. Each byte represents the state of a particular virtual key. The value 0 indicates that the key is up, and the value 1 indicates that the key is down. The array is indexed by the VK_ values listed in Appendix A of the "Microsoft Windows Software Development Kit Reference Volume 2" for version 3.0.

The code fragment below changes the state of the VK_LBUTTON to 0 (up) during the processing of a WM_LBUTTONDOWN message. This causes TrackPopupMenu to act as if the menu were displayed as the result of a WM_LBUTTONUP message or of a keystroke. Therefore, the menu remains visible even after the mouse button is released and the WM_LBUTTONUP message is received. Items on this menu can be selected with the mouse or the keyboard.

   switch (iMessage)
      {
   case WM_LBUTTONDOWN:
      static BYTE rgbKeyState[256];

      GetKeyboardState(rgbKeyState);
      rgbKeyState[VK_LBUTTON] = 0;      // 0==UP, 1==DOWN
      SetKeyboardState(rgbKeyState);

      // Create the pop-up menu and call TrackPopupMenu.
      break;
      }


Additional reference words: 3.00 3.10 3.50 3.51 4.00 95
KBCategory: kbui
KBSubcategory: UsrMen


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: November 2, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.