2012-02-27 89 views
0

我正在用快捷方式/快捷菜单创建系统托盘程序,但似乎无法在Windows过程中收到WM_COMMAND消息。当我点击菜单项时,它不会发送,我一直在检查是否已正确设置菜单。Windows未收到WM_COMMAND消息过程

这里是我的代码:“请不要在发布WM_COMMAND消息只是返回你会贴出值”

#include <Windows.h> 
#include <stdio.h> 
#include "resource.h" 
#define WM_TRAYICON (WM_USER + 0x0001) //a custom message for the notification icon 

HWND hwnd; //window handle 
HINSTANCE hinst; //module handle 
WNDCLASSEX wnd; //window class 
MSG msg; //event message or notification 
NOTIFYICONDATA nid; //notification icon object 
HMENU cmenu; 
MENUITEMINFO menuitem1; 
MENUITEMINFO menuitem2; 
CURSORINFO cursor; 

LRESULT CALLBACK MainWProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 
{ 
    switch(uMsg) 
    { 
    case WM_COMMAND: 
     printf("asfd\r\n"); 
     break; 
    case WM_CREATE: 
     printf("just created\r\n"); 
     break; 
    case WM_TRAYICON: 
     switch(LOWORD(lParam)) 
     { 
     case WM_CONTEXTMENU: 
      GetCursorInfo(&cursor); 
      //printf("xPos: %d\r\nyPos = %d\r\n\r\n", xPos, yPos); 
      TrackPopupMenuEx(cmenu, TPM_RETURNCMD | TPM_LEFTBUTTON | TPM_NOANIMATION | TPM_HORIZONTAL | TPM_VERTICAL, cursor.ptScreenPos.x, cursor.ptScreenPos.y, hwnd, NULL); 
      //DestroyMenu(
      break; 
     } 
     break; 
    case WM_INITMENU: 
     printf("open menu\r\n"); 
     break; 
    case WM_DESTROY: 
     //clean things up 
     Shell_NotifyIcon(NIM_DELETE, &nid); 
     break; 
    default: 
     break; 
    } 

    return DefWindowProc(hwnd, uMsg, wParam, lParam); 
} 

void main() 
{ 
    int result; 

    hinst = GetModuleHandle(NULL); 
    cursor.cbSize = sizeof(cursor); 

    memset(&wnd, 0, sizeof(wnd)); 
    wnd.cbSize = sizeof(wnd); 
    wnd.lpszClassName = "MainWClass"; 
    wnd.lpfnWndProc = MainWProc; 
    wnd.hInstance = hinst; 
    result = RegisterClassEx(&wnd); 

    hwnd = CreateWindowEx 
     (
     0, //extended styles 
     wnd.lpszClassName, //class name 
     "Main Window", //window name 
     WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL | WS_MINIMIZEBOX, //style tags 
     CW_USEDEFAULT, //horizontal position 
     CW_USEDEFAULT, //vertical position 
     CW_USEDEFAULT, //width 
     CW_USEDEFAULT, //height 
     (HWND) NULL, //parent window 
     (HMENU) NULL, //class menu 
     (HINSTANCE) wnd.hInstance, //some HINSTANCE pointer 
     NULL //Create Window Data? 
     ); 
    if(!hwnd) 
    { 
     printf("CreateWindowEx failed: %d\n", GetLastError()); 
     Sleep(INFINITE); 
    } 

    nid.cbSize = sizeof(nid); 
    nid.hWnd = hwnd; 
    nid.uID = 1; 
    nid.uVersion = NOTIFYICON_VERSION_4; 
    nid.uCallbackMessage = WM_TRAYICON; 
    nid.hIcon = LoadIcon(hinst, MAKEINTRESOURCE(IDI_ICON1)); 
    strcpy(nid.szTip, "My Tooltip!"); 
    nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP | NIF_SHOWTIP; 

    cmenu = CreatePopupMenu(); 
    menuitem1.cbSize = sizeof(menuitem1); 
    menuitem1.fMask = MIIM_TYPE; 
    menuitem1.fType = MFT_STRING; 
    menuitem1.hSubMenu = NULL; 
    //menuitem1.cch = ; 
    menuitem1.dwTypeData = "Open a world of wonder!"; 
    InsertMenuItem(cmenu, 0, true, &menuitem1); 

    if(! Shell_NotifyIcon(NIM_ADD, &nid)) 
    { 
     printf("Shell_NotifyIcon(NIM_ADD, &nid) failed.\r\n"); 
     Sleep(INFINITE); 
    } 
    if(! Shell_NotifyIcon(NIM_SETVERSION, &nid)) 
    { 
     printf("Shell_NotifyIcon(NIM_SETVERSION, &nid) failed.\r\n"); 
     Sleep(INFINITE); 
    } 

    UpdateWindow(hwnd); 
    for(; ;) 
    { 
     if(GetMessage(&msg, hwnd, 0, 0)) 
     { 
      TranslateMessage(&msg); 
      DispatchMessage(&msg); 
     } 
    } 
} 
+0

为什么在基于GUI的应用程序中使用printf?使用TRACE宏查看调试窗口中的输出。 – Ajay 2012-02-27 05:50:55

+1

我希望在控制台上看到它。 – Joshua 2012-02-27 06:19:27

+0

这是你的选择。但对我来说没有意义。 Debug-Windows不过是一个控制台。为GUI启动另一个控制台并不意味着合乎逻辑。你的GUI程序应该以'WinMain'开头,而不是'main'(原因很多)。所以,如果你的程序以'WinMain'开头,那么printf将无法工作。 – Ajay 2012-02-27 07:59:26

回答

3

嗯,你通过了TPM_RETURNCMD标志,这意味着

+0

Oh shit ....我以为“返回菜单项标识符”意味着将其返回给wndproc。非常感谢! – Joshua 2012-02-27 06:17:36