You need to create an application that will not have either a visible window or an icon on the taskbar, and only work from the tray. Please tell me the sequence of actions, as well as with what parameters you need to register WNDCLASSEX and use CreateWindow .
2 answers
If there is no visible window, it makes sense to create a Message-Only Window , a window without a graphical representation, designed exclusively for processing window messages:
const HWND g_hWnd = CreateWindowEx( /* dwExStyle */ 0, /* lpClassName */ szClassName, /* lpWindowName */ NULL, /* dwStyle */ 0, /* x, y, w, h */ 0, 0, 0, 0, /* hWndParent */ HWND_MESSAGE, /* hMenu */ NULL, /* hInstance */ g_hInst, /* lpParam */ NULL ); Then, in the usual way, create an icon in the notification panel:
#define WM_USER_SHELLICON (WM_USER + 1) NOTIFYICONDATA data; data.cbSize = sizeof(data); data.hWnd = g_hWnd; data.uID = 1; // Можно поставить любой идентификатор, всё равно иконка только одна data.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP; data.uCallbackMessage = WM_USER_SHELLICON; data.hIcon = LoadIcon(...); data.szTip = TEXT(...); // Или LoadResourceString, как вариант data.uVersion = NOTIFYICON_VERSION; Shell_NotifyIcon(NIM_ADD, &data); - Thanks for the detailed answer! But with the Message-Only Window, it will be possible to implement the menu by clicking on the tray icon? - Iceman
- @Iceman, yes,
TrackPopupMenudoes not require that the specifiedhWndbe a UI window. - ߊߚߤߘ - I see. Thanks. Only here something icon does not become: TrayData.hIcon = LoadIcon (NULL, MAKEINTRESOURCE (MAINICON)); - Empty - Iceman
- @Iceman, the first argument to
LoadIconshould be thehInstanceyour program (obtained throughWinMainorGetModuleHandle). When transferringNULLthis function searches for icons in system resources. - ߊߚߤߘ - Yeah, there is a contact. Thanks again! - Iceman
|
Once did this. I can’t say what is right :), but it worked. Here is the main loop:
g_hInst = GetModuleHandle(NULL); char * szClassName = "EmptyWindow"; WNDCLASSEX wcex = {sizeof(wcex)}; wcex.lpfnWndProc = WndProc; wcex.hInstance = g_hInst; wcex.lpszClassName = szClassName; RegisterClassEx(&wcex); if (g_hWnd = CreateWindow(szClassName, NULL, WS_OVERLAPPEDWINDOW, 0, 0, 0, 0, NULL, NULL, g_hInst, NULL)); { ShowWindow(g_hWnd,SW_HIDE); MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } } Well, the tray icon was created in WM_CREATE .
I do not know how ideologically this is true, but it worked :)
|