为win32 API创建上下文菜单

时间:2010-02-22 16:13:47

标签: winapi contextmenu

我正在尝试使用

为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 个答案:

答案 0 :(得分:6)

您无法通过强制转换将字符串文字转换为宽,您必须将其声明为宽字符串。该转换只是打败了编译器警告,它不会改变字符串的内容。

更改此

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

到这个

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

或者

L"Exit"
L"Play"

答案 1 :(得分:1)

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

答案 2 :(得分:0)

以下为我工作

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