11

I am trying to create context menu for win32 application using

case WM_RBUTTONDOWN:
{
    HMENU hPopupMenu = CreatePopupMenu();
    InsertMenu(hPopupMenu, 0, MF_BYPOSITION | MF_STRING, ID_CLOSE, (LPCWSTR)"Exit");
    InsertMenu(hPopupMenu, 0, MF_BYPOSITION | MF_STRING, ID_EXIT, (LPCWSTR)"Play");
    SetForegroundWindow(hWnd);
    TrackPopupMenu(hPopupMenu, TPM_BOTTOMALIGN | TPM_LEFTALIGN, 0, 0, 0, hWnd, NULL);
}

But I always get context menu as shown below

alt text http://img191.imageshack.us/img191/866/70219076.png

I want text exit and play to be displayed in the menu

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
Xinus
  • 29,617
  • 32
  • 119
  • 165

3 Answers3

7

You can't convert a string literal to wide by casting, you have to declare it as a wide char string. The casting just defeats the compiler warning, it doesn't change the content of the string.

change this

(LPCWSTR)"Exit"
(LPCWSTR)"Play"

to this

_T("Exit")
_T("Play")

or this

L"Exit"
L"Play"
John Knoeller
  • 33,512
  • 4
  • 61
  • 92
2

Following worked for me

case WM_RBUTTONDOWN:
          {
            HMENU hPopupMenu = CreatePopupMenu();
            InsertMenu(hPopupMenu, 0, MF_BYPOSITION | MF_STRING, ID_CLOSE, L"Exit");
            InsertMenu(hPopupMenu, 0, MF_BYPOSITION | MF_STRING, ID_EXIT, L"Play");
            SetForegroundWindow(hWnd);
            TrackPopupMenu(hPopupMenu, TPM_BOTTOMALIGN | TPM_LEFTALIGN, 0, 0, 0, hWnd, NULL);
          }
Xinus
  • 29,617
  • 32
  • 119
  • 165
1

Are you specifying the encoding in the API function definition? I ran into that problem recently and removing the specification fixed the problem.

brian
  • 1,080
  • 9
  • 7