Note

タスクトレイにアイコンを表示する

#define WIN32_LEAN_AND_MEAN
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <shellapi.h>
#include <stdio.h>
#include <tchar.h>
#define WM_NOTIFYICON WM_USER+100
LRESULT CALLBACK WndProc(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam);
_TCHAR *CRWndclass(UINT cbSize,UINT style,WNDPROC lpfnWndProc,int cbClsExtra,int cbWndExtra,HINSTANCE hInstance,HICON hIcon,HICON hIconSm,HCURSOR hCursor,HBRUSH hbrBackground,_TCHAR *lpszMenuName,_TCHAR *lpszClassName);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInst,LPSTR lpCmdLine,int nCmdShow){
	MSG msg;
	HWND hDumWnd;
	hDumWnd=CreateWindowEx(NULL,CRWndclass(0,0,WndProc,0,0,hInstance,0,0,0,0,0,_T("DummyWindow")),NULL,NULL,0,0,0,0,NULL,NULL,hInstance,NULL);
	while(GetMessage(&msg,NULL,0,0)!=0){
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam){
	NOTIFYICONDATA id;
	switch(Msg){
		case WM_CREATE:
			id.cbSize=sizeof(id);
			id.hWnd=hWnd;
			id.uID=0;
			id.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP;
			id.uCallbackMessage=WM_NOTIFYICON;
			id.hIcon=LoadIcon(NULL,IDI_WINLOGO);
			_tcscpy(id.szTip,_T("TaskTray"));
			Shell_NotifyIcon(NIM_ADD,&id);
			break;
		case WM_NOTIFYICON:
			switch(lParam){
				case WM_LBUTTONDOWN:
					MessageBox(NULL,_T("WM_LBUTTONDOWN"),_T("TaskTray"),MB_OK);
					break;
				case WM_RBUTTONUP:
					DestroyWindow(hWnd);
					break;
			}
			break;
		case WM_DESTROY:
			Shell_NotifyIcon(NIM_DELETE,&id);
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProc(hWnd,Msg,wParam,lParam);
			break;
	}
	return 0;
}
_TCHAR *CRWndclass(UINT cbSize,UINT style,WNDPROC lpfnWndProc,int cbClsExtra,int cbWndExtra,HINSTANCE hInstance,HICON hIcon,HICON hIconSm,HCURSOR hCursor,HBRUSH hbrBackground,_TCHAR *lpszMenuName,_TCHAR *lpszClassName){
	WNDCLASSEX wc;
	wc.cbSize=cbSize!=NULL?cbSize:sizeof(wc);
	wc.style=style!=NULL?style:CS_HREDRAW|CS_VREDRAW;
	wc.lpfnWndProc=lpfnWndProc;
	wc.cbClsExtra=cbClsExtra;
	wc.cbWndExtra=cbWndExtra;
	wc.hInstance=hInstance;
	wc.hIcon=hIcon;
	wc.hIconSm=hIconSm;
	wc.hCursor=hCursor!=NULL?hCursor:LoadCursor(hInstance,IDC_ARROW);
	wc.hbrBackground=hbrBackground;
	wc.lpszMenuName=lpszMenuName;
	wc.lpszClassName=lpszClassName;
	if(RegisterClassEx(&wc))
		return lpszClassName;
	else
		return NULL;
}