Posted on 2010-10-13 06:53
傅先生 阅读(722)
评论(0) 编辑 收藏 引用 所属分类:
VC常用
//////
命令栏处理---------隐藏
/////
////////托盘处理----------显示
--------------------------------------------------------------------
1,定义基础消息处理机制
2,为创建托盘注册
3,定义消息处理机制.
///////////////全局化
#define UM_TRAYNOTIFICATION (WM_USER+100)
NOTIFYICONDATA m_nid;
///////////////////////////////////////
//初始化m_nid
m_nid.cbSize = sizeof(NOTIFYICONDATA);
m_nid.hWnd = this->m_hWnd;
m_nid.uID = IDR_MENU1;
m_nid.uFlags = NIF_ICON|NIF_TIP|NIF_MESSAGE;
m_nid.hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
strcpy (m_nid.szTip, "我的托盘听我的");
m_nid.uCallbackMessage = UM_TRAYNOTIFICATION;
增加一个
Shell_NotifyIcon(NIM_ADD, &m_nid);
/////////////////////////////////
////////消息处理/////////////////////
void CMainFrame::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == SC_CLOSE) //触发关闭按钮
{
//OnClose(); //不进行关闭
// AfxMessageBox("dfddfd");
RemoveTray();
PostQuitMessage(0);
}
CWnd::OnSysCommand(nID,lParam);
if (nID==SC_MINIMIZE)
{
/*
NOTIFYICONDATA nid;
nid.cbSize=(DWORD)sizeof(NOTIFYICONDATA);
nid.hWnd=this->m_hWnd;
nid.uID=IDR_MAINFRAME;
nid.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP ;
nid.uCallbackMessage=UM_TRAYNOTIFICATION;//自定义的在托盘区显示图标的消息
nid.hIcon=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME));
strcpy(nid.szTip,"要发消息点我!");//信息提示条
Shell_NotifyIcon(NIM_ADD,&nid);//在托盘区添加图标
*/
Shell_NotifyIcon(NIM_ADD, &m_nid);
ShowWindow(SW_HIDE);//隐藏窗口
}
}
//移除托盘图标,在程序退出时一定要记得调用,否则图标会残留在托盘上
void CMainFrame::RemoveTray() {
Shell_NotifyIcon(NIM_DELETE, &m_nid);
}
LRESULT CMainFrame::OnTrayNotification(WPARAM wId, LPARAM lEvent)
{
if(wId!=m_nid.uID || (lEvent!=WM_LBUTTONUP && lEvent!=WM_RBUTTONUP))
return lEvent;
ShowWindow(SW_SHOW);
//加载菜单
CMenu menu;
if(!menu.LoadMenu(wId))
return 0; //获取弹出菜单
CMenu *pSubMenu = menu.GetSubMenu(0);
if(!pSubMenu)
return 0;
if(lEvent == WM_RBUTTONUP)
{ //设置默认菜单项
::SetMenuDefaultItem(pSubMenu->m_hMenu, 0, TRUE); //获取鼠标位置
CPoint mouse;
GetCursorPos(&mouse); //设置快捷菜单
::SetForegroundWindow(m_nid.hWnd);
::TrackPopupMenu(pSubMenu->m_hMenu, 0, mouse.x, mouse.y, 0, m_nid.hWnd, NULL);
}
else
{
::SendMessage(m_nid.hWnd, WM_COMMAND, pSubMenu->GetMenuItemID(0), 0);
}
return 1;
}