-->在资源中导入:图标IDI_TASK,菜单IDR_RMENU,
-->在头文件中添加变量:HICON m_hIconTask;
-->构造函数中为变量装载图标资源:m_hIconTask = AfxGetApp()->LoadIcon(IDR_TASK);
-->头文件声明消息映射函数:afx_msg LRESULT OnShowTask(WPARAM wParam,LPARAM lParam);函数实现看下面。
-->cpp文件中自定义一个消息,用来传递mouse点到拖盘区的信息:#define WM_SHOW_TASK WM_USER+150
-->消息映射MESSAGE_MAP中同时也要添加消息:ON_MESSAGE(WM_SHOW_TASK,OnShowTask)
-->在OnSysCommand(UINT nID, LPARAM lParam)中添加代码,使窗口在最小化时在托盘区显示图标,并隐藏窗口:
if (nID==SC_MINIMIZE)
{
NOTIFYICONDATA nid;
nid.cbSize=(DWORD)sizeof(NOTIFYICONDATA);
nid.hWnd=this->m_hWnd;
nid.uID=IDI_TASK;
nid.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP ;
nid.uCallbackMessage=WM_SHOW_TASK;//自定义的在托盘区显示图标的消息
nid.hIcon=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_TASK));
strcpy(nid.szTip,"要发消息点我!");//信息提示条
Shell_NotifyIcon(NIM_ADD,&nid);//在托盘区添加图标
ShowWindow(SW_HIDE);//隐藏窗口
}
-->窗口函数DestroyWindow()中添加代码,删除图标,函数实现后附:Delete_Task_Icon();
//
LRESULT CCDROMDlg::OnShowTask(WPARAM wParam, LPARAM lParam)
{
if(wParam!=IDR_MAINFRAME) return 1;
switch(lParam)
{
case WM_RBUTTONUP://右键起来时弹出快捷菜单
{
LPPOINT lpoint=new tagPOINT;
::GetCursorPos(lpoint);//得到鼠标位置
CMenu menu;
int pMenuID = 0;
VERIFY(menu.LoadMenu(IDR_RMENU));//得到菜单
CMenu* pPopup = menu.GetSubMenu(pMenuID);//得到子菜单
ASSERT(pPopup != NULL);
DWORD Made = pPopup->TrackPopupMenu(TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_NONOTIFY|TPM_RETURNCMD,lpoint->x,lpoint->y,this,NULL);//使用子菜单
switch(Made)
{
case IDM_OPEN: {ShowWindow(SW_SHOW);ShowWindow(SW_SHOWNORMAL);} break;
case IDM_EXIT: {DestroyWindow();} break; //退出程序
}
pPopup->DestroyMenu();
HMENU hmenu=menu.Detach();//资源回收
menu.DestroyMenu();
delete lpoint;
}
break;
case WM_LBUTTONDOWN://左键的处理
{
ShowWindow(SW_SHOW); //简单的显示主窗口完事儿
ShowWindow(SW_SHOWNORMAL); //可以避免原来的最小化
}
break;
}
return 0;
}
BOOL CCDROMDlg::Delete_Task_Icon()
{
// 删除任务条中的图标
NOTIFYICONDATA nid;
nid.cbSize = sizeof(nid);
nid.hWnd = m_hWnd;
nid.uID = IDI_TASK;
return Shell_NotifyIcon(NIM_DELETE, &nid);
}