Created
March 25, 2013 03:52
-
-
Save shunsuke0125/5234815 to your computer and use it in GitHub Desktop.
Windowアプリケーションで通知領域にアイコンを常駐させる処理
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 自動生成される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, ¬if)) { | |
return TRUE; | |
} else { | |
if (GetLastError() != ERROR_TIMEOUT) { | |
MessageBox(hWnd, | |
"通知領域にアイコンを登録出来ませんでした。", | |
"通知領域登録エラー", | |
MB_ICONEXCLAMATION | MB_OK); | |
DestroyWindow(hWnd); | |
} | |
if (Shell_NotifyIcon(NIM_MODIFY, ¬if)) { | |
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