代码之家  ›  专栏  ›  技术社区  ›  Xinus

为win32 API创建上下文菜单

  •  7
  • Xinus  · 技术社区  · 16 年前

    我正在尝试使用创建win32应用程序的上下文菜单

    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);
    }

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

    出口 和 显示在菜单中

    3 回复  |  直到 16 年前
        1
  •  6
  •   John Knoeller    16 年前

    不能通过强制转换将字符串文字转换为wide,必须将其声明为wide char字符串。强制转换只会破坏编译器警告,它不会更改字符串的内容。

    改变这个

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

    为了这个

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

    L"Exit"
    L"Play"
    
        2
  •  1
  •   brian    16 年前

    是否在API函数定义中指定编码?我最近遇到了这个问题,删除规范解决了这个问题。

        3
  •  1
  •   Xinus    16 年前

    跟踪对我有用

    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);
              }