タスクトレイにアプリを常駐させる


キーワードとなるのは "NOTIFYICONDATA" と "Shell_NotifyIcon" ぐらいかな。
でも、僕自身 VCはまだまだなのでおかしなところがあったらお教えください。

#include <windows.h> #include <stdio.h> #include <string.h> #include "resource.h" #define TASK_TRAY (WM_USER + 1) LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM); BOOL InitApplication(HANDLE); BOOL InitInstance(HANDLE, int); NOTIFYICONDATA nidNoftyIconData; int WINAPI WinMain(HANDLE hCurInst, HANDLE hPrevInst, LPSTR lpCmdLine, int iCmdShow) { MSG msg; if (!hPrevInst) if (!InitApplication(hCurInst)) return FALSE; if (!InitInstance(hCurInst, iCmdShow)) return FALSE; while (GetMessage(&msg, NULL, 0, 0)){ TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } BOOL InitApplication(HANDLE hCurInst) { WNDCLASS wc; wc.style = (UINT)NULL; wc.lpfnWndProc = MainWndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hCurInst; wc.hIcon = LoadIcon(hCurInst, MAKEINTRESOURCE(IDI_ICON)); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = GetStockObject(BLACK_BRUSH); wc.lpszMenuName = NULL; wc.lpszClassName = "UzTaskTrayTestClass"; return RegisterClass(&wc); } BOOL InitInstance(HANDLE hCurInst, int iCmdShow) { HWND hwnd; hwnd = CreateWindow( "UzTaskTrayTestClass", "UzTaskTray Sample Program", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hCurInst, NULL ); if (!hwnd) return FALSE; nidNoftyIconData.cbSize = sizeof(NOTIFYICONDATA); nidNoftyIconData.hWnd = hwnd; nidNoftyIconData.uID = 1; nidNoftyIconData.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE; nidNoftyIconData.uCallbackMessage = TASK_TRAY; nidNoftyIconData.hIcon = LoadIcon(hCurInst, MAKEINTRESOURCE(IDI_ICON)); strcpy(nidNoftyIconData.szTip, "** Uz TaskTray Test **ダブルクリックしてください"); Shell_NotifyIcon(NIM_ADD, &nidNoftyIconData); return TRUE; } LRESULT CALLBACK MainWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_CREATE: break; case WM_DESTROY: Shell_NotifyIcon(NIM_DELETE, &nidNoftyIconData); PostQuitMessage(0); break; case TASK_TRAY: switch (LOWORD(lParam)) { case WM_LBUTTONDBLCLK: ShowWindow(hwnd, SW_SHOW); break; default: break; } break; default: break; } return DefWindowProc(hwnd, message, wParam, lParam); }


サンプル TaskTray.lzh