首先添加一个响应托盘的函数
#define WM_SHOWTASK (WM_USER + 1986) //用于托盘
//添加一个托盘函数
BOOL CIPMsgDlg::TrayMessage(HWND hWnd, DWORD dwMessage, HICON hIcon, PSTR pszTip)
{
BOOL bReturn;
NOTIFYICONDATA NofityData;
NofityData.cbSize = sizeof(NOTIFYICONDATA);
NofityData.hWnd = hWnd;
NofityData.uID = IDR_MAINFRAME;
NofityData.uFlags = NIF_MESSAGE|NIF_ICON|NIF_TIP;
NofityData.uCallbackMessage = WM_SHOWTASK;
NofityData.hIcon = hIcon;
lstrcpyn(NofityData.szTip, pszTip, sizeof(NofityData.szTip));
bReturn = Shell_NotifyIcon(dwMessage, &NofityData);
if (hIcon)
DestroyIcon(hIcon);
return bReturn;
}
然后在OnInitDialog()里添加
TrayMessage(m_hWnd,NIM_ADD,m_hIcon,"IpMsg");//托盘
就可以产生一个托盘
//自定义消息处理函数,处理托盘鼠标事件
//头文件
afx_msg LRESULT onShowTask(WPARAM wParam,LPARAM lParam);
//源文件
ON_MESSAGE(WM_SHOWTASK,onShowTask)
//在这你一定要添加一个菜单 ID是IDR_LISTPOPMENU
LRESULT CIPMsgDlg::onShowTask(WPARAM wParam, LPARAM lParam)
{
switch(lParam)
{
case WM_RBUTTONUP://右键起来时弹出快捷菜单,这里只有一个“关闭”
{
LPPOINT lpoint = new tagPOINT;
CMenu menu;
menu.LoadMenu(IDR_LISTPOPMENU);//菜单ID
::GetCursorPos(lpoint);//得到鼠标位置
CMenu *pPopup;
pPopup = menu.GetSubMenu(1);
pPopup->TrackPopupMenu(TPM_LEFTALIGN,lpoint->x,lpoin t->y,this);
delete lpoint;
}
break;
case WM_LBUTTONDBLCLK: //双击左键的处理
{
this->ShowWindow(SW_SHOW);//简单的显示主窗口完事儿
}
break;
}
return 0;
}
//初始化时隐藏对话框 window消息函数
void CIPMsgDlg::OnNcPaint()
{
// TODO: Add your message handler code here
static int i = 2;
if(i > 0)
{
i --;
ShowWindow(SW_HIDE);
}
else
CDialog::OnNcPaint();
// Do not call CDialog::OnNcPaint() for painting messages
}
//然后可以添加托盘菜单命令 是自己添加的菜单命令
//托盘右键显示对话框菜单
void CIPMsgDlg::OnShowDlgMenu()
{
// TODO: Add your command handler code here
this->ShowWindow(SW_SHOW);
}
//托盘右键关闭菜单
void CIPMsgDlg::OnExitMenu()
{
// TODO: Add your command handler code here
this->SendMessage(WM_CLOSE);//关闭主对话框
vc最小化托盘
最新推荐文章于 2024-11-13 11:16:46 发布