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);
}
}
}
爲什麼在基於GUI的應用程序中使用printf?使用TRACE宏查看調試窗口中的輸出。 – Ajay 2012-02-27 05:50:55
我希望在控制檯上看到它。 – Joshua 2012-02-27 06:19:27
這是你的選擇。但對我來說沒有意義。 Debug-Windows不過是一個控制檯。爲GUI啓動另一個控制檯並不意味着合乎邏輯。你的GUI程序應該以'WinMain'開頭,而不是'main'(原因很多)。所以,如果你的程序以'WinMain'開頭,那麼printf將無法工作。 – Ajay 2012-02-27 07:59:26