/*****************************************************/
/* -- Demonstration of how to right mouse activate   */
/*    of menu items on a menu bar.                   */
/*****************************************************/
#include <windows.h>

VOID             SetKey(int, BOOL);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int PASCAL
WinMain(HINSTANCE hins, HINSTANCE hinsPrev,
  LPSTR lpszCmdLine, int wShow)
/*****************************************************/
/* -- Entry point.                                   */
/*****************************************************/
    {
    MSG     msg;
    HWND    hwnd;

    if (hinsPrev == NULL)
        {
        WNDCLASS    wcs;

        /* Register the main window's class. */
        wcs.style = 0;
        wcs.lpfnWndProc = WndProc;
        wcs.cbClsExtra = 0;
        wcs.cbWndExtra = 0;
        wcs.hInstance = hins;
        wcs.hIcon = NULL;
        wcs.hCursor = LoadCursor(NULL, IDC_ARROW);
        wcs.hbrBackground =
          (HBRUSH)(COLOR_APPWORKSPACE + 1);
        wcs.lpszMenuName = "RMenu";
        wcs.lpszClassName = "RMenu";
        if (!RegisterClass(&wcs))
            return 0;
        }

    if ((hwnd = CreateWindow("RMenu",
      "Right menu demo", WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL,
      hins, NULL)) == NULL)
        return 0;

    ShowWindow(hwnd, wShow);
    UpdateWindow(hwnd);

    while (GetMessage(&msg, NULL, 0, 0))
        {
        /* Right button in menu bar? */
        if (msg.message == WM_NCRBUTTONDOWN &&
          msg.hwnd == hwnd &&
          DefWindowProc(hwnd, WM_NCHITTEST, 0,
            msg.lParam) == HTMENU)
            {
            /* Pretend its the left. */
            SetKey(VK_LBUTTON, TRUE);
            msg.message = WM_NCLBUTTONDOWN;

            }

        TranslateMessage(&msg);
        DispatchMessage(&msg);
        }

    return 0;
    }

LRESULT CALLBACK
WndProc(HWND hwnd, UINT wm, WPARAM wParam,
  LPARAM lParam)
/*****************************************************/
/* -- Frame window proc.                             */
/*****************************************************/
    {
    switch (wm)
        {
    default:
        return DefWindowProc(hwnd, wm, wParam, lParam);
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;

    case WM_MENUSELECT:
        if (GetKeyState(VK_RBUTTON) < 0 &&
          GetKeyState(VK_LBUTTON) < 0)
            {
            /* Undo the left button kludge. */
            SetKey(VK_LBUTTON, FALSE);
            PostMessage(hwnd, WM_LBUTTONUP, 0, 0L);

            if (wParam != NULL)
                PostMessage(hwnd, WM_USER, wParam, 0);
            }
        break;

    case WM_USER:
        {
        RECT    rect;
        int     x, y;

        /* Float a menu in the center of the window. */
        GetClientRect(hwnd, &rect);
        ClientToScreen(hwnd, (LPPOINT)&rect);
        ClientToScreen(hwnd, (LPPOINT)&rect.right);
        x = (rect.left + rect.right) / 2;
        y = (rect.top + rect.bottom) / 2;
        GetWindowRect(GetDesktopWindow(), &rect);
        TrackPopupMenu((HMENU)wParam,
          TPM_CENTERALIGN | TPM_RIGHTBUTTON,
          x, y, 0, hwnd, &rect);
        }
        break;
        }
    return 0;
    }


VOID
SetKey(int wKey, BOOL fDown)
/*****************************************************/
/* -- Set the given key to the given state.          */
/*****************************************************/
    {
    BYTE    rgb[256];

    GetKeyboardState(rgb);
    rgb[wKey] = fDown ? 0x80 : 0x00;
    SetKeyboardState(rgb);
    }


