//インクルード
#include<Windows.h>
//定数定義
#define CLASS_NAME "WinClass"
#define WINDOW_NAME "WindowsSDK"
//プロトタイプ宣言
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int AddNotifyIcon(HWND hWnd);
void DeleteNotifyIcon(HWND hWnd);
//WinMain関数(エントリーポイント)
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wcex = {
sizeof(WNDCLASSEX), CS_CLASSDC, WndProc, 0, 0, hInstance, NULL, LoadCursor(NULL, IDC_ARROW), (HBRUSH)(COLOR_WINDOW + 1), NULL, CLASS_NAME, NULL
};
RegisterClassEx(&wcex);
HWND hWnd = CreateWindowEx(
0, CLASS_NAME, WINDOW_NAME, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, NULL, NULL, hInstance, NULL
);
//ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
AddNotifyIcon(hWnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0) != 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
UnregisterClass(CLASS_NAME, wcex.hInstance);
return (int)msg.wParam;
}
//ウィンドウプロシージャ
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_KEYDOWN:
switch (wParam) {
case VK_ESCAPE:
DestroyWindow(hWnd);
break;
}
break;
case WM_DESTROY:
DeleteNotifyIcon(hWnd);
PostQuitMessage(0);
break;
default:
break;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
int AddNotifyIcon(HWND hWnd) {
NOTIFYICONDATA nid;
nid.cbSize = sizeof(nid);
nid.hWnd = hWnd;
nid.uID = 0;
nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
nid.uCallbackMessage = 101;
nid.hIcon = ::LoadIcon(NULL, IDI_WINLOGO);
strcpy_s(nid.szTip, "sample");
return (int)Shell_NotifyIcon(NIM_ADD, &nid);
}
void DeleteNotifyIcon(HWND hWnd) {
NOTIFYICONDATA nid;
nid.cbSize = sizeof(nid);
nid.hWnd = hWnd;
nid.uID = 0;
nid.uFlags = 0;
Shell_NotifyIcon(NIM_DELETE, &nid);
return;
}
どうすればECSキーを押したらプログラムが終了するようになるのでしょうか?
自分で調べたところ、フック?というのを使えばうまくいくみたいなのを見かけましたが、よく分かりませんでした。
OS:Windows10
コンパイラ:VisualStudio2017
ライブラリ:WindowsSDK
言語:C