此文章主要讲述的是应用程序最小化时图标显示在托盘区域的功能,如QQ的效果
1.先定义一个宏#define MYWM_MESSAGE WM_USER+1,为后面托盘图标响应左右键点击的事件的消息ID
定义显示托盘图标的函数TrayMessage
BOOL CMainFrame::TrayMessage(DWORD dwMessage)
{
CString sTip(SDF_APP_TIP);
NOTIFYICONDATA tnd;
tnd.cbSize = sizeof(NOTIFYICONDATA);
tnd.hWnd = m_hWnd;
tnd.uID = IDR_MAINFRAME;
tnd.uFlags = NIF_MESSAGE|NIF_ICON;
tnd.uCallbackMessage = MYWM_MESSAGE;
VERIFY( tnd.hIcon = LoadIcon(AfxGetInstanceHandle(), MAKEINTRESOURCE (IDR_MAINFRAME)) );
tnd.uFlags = NIF_MESSAGE|NIF_ICON|NIF_TIP;
lstrcpyn(tnd.szTip, (LPCTSTR)sTip, sizeof(tnd.szTip)/sizeof(tnd.szTip[0]) );
return Shell_NotifyIcon(dwMessage, &tnd);
}
分别在OnCreate和OnClose函数中调用TrayMessage(NIM_ADD)和TrayMessage(NIM_DELETE)即可
2.响应鼠标点击消息
LRESULT CMainFrame::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO: 在此添加专用代码和/或调用基类
if (message == MYWM_MESSAGE)
{
switch (lParam)
{
case WM_LBUTTONDBLCLK:
switch (wParam)
{
case IDR_MAINFRAME:
//ShowWindow(SW_HIDE);
return TRUE;
}
break;
case WM_RBUTTONDOWN:
//TRACE("显示上下文菜单");
break;
}
}
return CFrameWnd::WindowProc(message, wParam, lParam);
}