Skip to content

Instantly share code, notes, and snippets.

@shunsuke0125
Created March 25, 2013 03:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shunsuke0125/5234815 to your computer and use it in GitHub Desktop.
Save shunsuke0125/5234815 to your computer and use it in GitHub Desktop.
Windowアプリケーションで通知領域にアイコンを常駐させる処理
// 自動生成されるInitInstance関数
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) {
HWND hWnd;
// グローバル変数にインスタンス処理を設定する
hInst = hInstance;
// ウィンドウを生成する
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
// 通知領域にアイコンを追加する
NOTIFYICONDATA notif;
char cToolTipMsg[19] = "アプリケーション名"; // 配列数=文字のバイト数+'\0'にする
notif.cbSize = sizeof(NOTIFYICONDATA);
notif.hWnd = hWnd;
notif.uID = 0;
notif.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
notif.CallbackMessage = WM_TRAYICONMESSAGE; // CALLBACK WndProc関数内で取得するメッセージ(要定義)
notif.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SMALL));
strcpy_s(notif.szTip, cToolTipMsg);
while (true) {
if (Shell_NotifyIcon(NIM_ADD, &notif)) {
return TRUE;
} else {
if (GetLastError() != ERROR_TIMEOUT) {
MessageBox(hWnd,
"通知領域にアイコンを登録出来ませんでした。",
"通知領域登録エラー",
MB_ICONEXCLAMATION | MB_OK);
DestroyWindow(hWnd);
}
if (Shell_NotifyIcon(NIM_MODIFY, &notif)) {
return TRUE;
} else {
Sleep(1000);
}
}
}
// ウィンドウを非表示状態にする
if (!hWnd) {
return FALSE;
}
ShowWindow(hWnd, SW_HIDE);
UpdateWindow(hWnd);
return TRUE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment