工程下载地址:https://download.csdn.net/download/m0_37567738/88215365
直接上代码:
#include<windows.h>
#include<shellapi.h>
#include"resource.h"
#define WM_IAWENTRAY WM_USER+5
HINSTANCE g_hInst;
HWND g_hWnd;
NOTIFYICONDATA nid;
INT_PTR CALLBACK MainDlgProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE,LPSTR lpCmdLine,int nShowCmd)
{
g_hInst=hInstance;
g_hWnd=CreateDialog(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,MainDlgProc);
ShowWindow(g_hWnd,nShowCmd);
nid.cbSize=sizeof(NOTIFYICONDATA);
nid.hWnd=g_hWnd;
nid.uID=IDI_TRAY;
nid.uFlags=NIF_ICON | NIF_MESSAGE | NIF_TIP;
nid.uCallbackMessage=WM_IAWENTRAY;
nid.hIcon=LoadIcon(g_hInst,MAKEINTRESOURCE(IDI_TRAY));
strcpy(nid.szTip," 测试系统托盘图标,呵呵 \r\n点击打开主界面"); //
MSG uMsg;
while(GetMessage(&uMsg,NULL,0,0)){
TranslateMessage(&uMsg);
DispatchMessage(&uMsg);
}
return 0;
}
INT_PTR CALLBACK MainDlgProc(HWND hDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch(uMsg){
case WM_INITDIALOG:
{
HICON hIcon=LoadIcon(g_hInst,MAKEINTRESOURCE(IDI_TRAY));
SendMessage(hDlg,WM_SETICON,ICON_BIG,(LPARAM)hIcon);
SendMessage(hDlg,WM_SETICON,ICON_SMALL,(LPARAM)hIcon);
SetDlgItemText(hDlg,IDC_INFO,TEXT("Write By Iawen --2009.02.03"));
return TRUE;
}
case WM_IAWENTRAY:
if(wParam==IDI_TRAY){
if(lParam==WM_LBUTTONDOWN){
ShowWindow(hDlg,SW_SHOWNORMAL);
return TRUE;
}
}
return FALSE;
case WM_DESTROY:
Shell_NotifyIcon(NIM_DELETE,&nid);
return TRUE;
case WM_SYSCOMMAND:
switch(wParam)
{
case SC_CLOSE:
DestroyWindow(hDlg);
PostQuitMessage(0);
return TRUE;
case SC_MINIMIZE:
ShowWindow(g_hWnd,SW_HIDE);
Shell_NotifyIcon(NIM_ADD,&nid);
return TRUE;
}
}
return FALSE;
}