vc托盘图标的实现以及托盘右键菜单的响应

#define  UM_SHOW_TORY_ICON WM_USER + 200 + 1      // 用于响应托盘图标消息


void addToTray()

{
HINSTANCE hInst = m_pm.GetInstance();
HICON hIcon;
hIcon = LoadIcon(hInst,MAKEINTRESOURCE(IDI_ICON));


NOTIFYICONDATAW nd;
nd.cbSize = sizeof (NOTIFYICONDATA);
nd.hWnd = m_hWnd;


nd.uID = IDI_ICON;   //托盘图标的图标ID
nd.uFlags = NIF_ICON|NIF_MESSAGE|NIF_TIP;     
nd.uCallbackMessage = UM_SHOW_TORY_ICON;   //托盘图标响应的消息ID
nd.hIcon = hIcon;
wcscpy(nd.szTip,L"网吧魔方兑换中心"); //托盘图标鼠标悬浮提示文字

Shell_NotifyIcon(NIM_ADD, &nd);   
}


void ModifyToryIcon()
{
HINSTANCE hInst = m_pm.GetInstance();
HICON hIcon;
hIcon = LoadIcon(hInst,MAKEINTRESOURCE(IDI_ICON));


NOTIFYICONDATAW nd;
nd.cbSize = sizeof (NOTIFYICONDATA);
nd.hWnd = m_hWnd;


nd.uID = IDI_ICON;
nd.uFlags = NIF_ICON|NIF_MESSAGE|NIF_TIP;
nd.uCallbackMessage = UM_SHOW_TORY_ICON;
nd.hIcon = hIcon;
wcscpy(nd.szTip, L"网吧魔方兑换中心");


// 全局函数Shell_NotifyIcon()用于在托盘上增加、删除或修改图标
//  NIM_ADD 增加图标
// NIM_DELETE 删除图标
//  NIM_MODIFY 修改图标
Shell_NotifyIcon(NIM_MODIFY, &nd); 


}


void DelTray(HWND m_hWnd)
{
NOTIFYICONDATA nd;
nd.cbSize = sizeof (NOTIFYICONDATA);
nd.hWnd = m_hWnd;
nd.uID = IDI_ICON;
Shell_NotifyIcon(NIM_DELETE, &nd);
}


void showPopMenu()
{
CMenu menu;  
BOOL issuc = menu.LoadMenu(MAKEINTRESOURCE(IDR_MENU1));     //  IDR_MENU1使用的是系统菜单
if (issuc == FALSE)
{
return;
}
//载入事先定义的菜单 
if ( m_is_start_run == FALSE)   //此处用于更换菜单项勾选状态
{
menu.GetSubMenu(0).CheckMenuItem(ID_AUTOOPEN, MF_BYCOMMAND | MF_UNCHECKED);
}
else
{
menu.GetSubMenu(0).CheckMenuItem(ID_AUTOOPEN, MF_BYCOMMAND | MF_CHECKED);
}

POINT pos;  
GetCursorPos(&pos);  
SetForegroundWindow(m_hWnd);  //失去焦点时菜单自动消失
menu.GetSubMenu(0).TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, pos.x, pos.y, m_hWnd);   //根据鼠标当前焦点位置设置弹出菜单位置

}

//托盘图标消息响应以及图标右键菜单项的响应

virtual HRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
BOOL isHandle = FALSE;
switch(uMsg){  
case WM_COMMAND:
{
if (wParam == ID_SHOWCONSALE)
{
ShowWindow(true);
}
if ( wParam == ID_AUTOOPEN )
{
if (m_is_start_run == FALSE)
{
//设置开机启动

}
else
{
//取消开机启动
}


}
if ( wParam == ID_QUIT )
{
DelTray(m_hWnd);
PostQuitMessage(0);
}
}
case UM_SHOW_TORY_ICON:
{
if(lParam == WM_LBUTTONDBLCLK)  
{  
//鼠标双击时主窗口出现  
::ShowWindow(m_hWnd, SW_SHOWNORMAL);
}  
else if(lParam==WM_RBUTTONUP && wParam == IDI_ICON)
{     
//鼠标右键单击弹出菜单  
showPopMenu();
}
break;  
}
}
//业务消息
if (!isHandle)
return ui_dlg_helper::HandleMessage(uMsg, wParam, lParam);
return S_OK;


};
 

你可能感兴趣的:(VC++,DUILIB,WTL)