工控编程吧
标题:
上位机MFC如何实现系统托盘
[打印本页]
作者:
qq263946146
时间:
2019-8-15 11:15
标题:
上位机MFC如何实现系统托盘
分为四步走。
1.定义变量,添加头文件等。
#include "shlwapi.h"
#define WM_NOTIFYICON WM_USER+1
class CGkbc8Dlg : public CDialog
{
NOTIFYICONDATA m_Nid;
}
2.0初始化变量
//初始化NOTIFYICONDATA结构
m_Nid.cbSize = sizeof(NOTIFYICONDATA);
m_Nid.hWnd = m_hWnd;
m_Nid.uID = 100;
m_Nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
m_Nid.uCallbackMessage = WM_NOTIFYICON;
m_Nid.hIcon = (HICON)LoadImage(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDR_MAINFRAME), IMAGE_ICON, 16, 16, NULL);
StrCpy(m_Nid.szTip, _T("Demo"));
//隐藏窗口
ShowWindow(SW_HIDE);
//在任务栏中添加图标
Shell_NotifyIcon(NIM_ADD, &m_Nid);
3.实现消息WM_NOTIFYICON的关联。
afx_msg void OnNotifyIcon(WPARAM wParam, LPARAM lParam);
ON_MESSAGE(WM_NOTIFYICON, OnNotifyIcon)
void CGkbc8Dlg::OnNotifyIcon(WPARAM wParam, LPARAM lParam)
{
if (wParam == 100)
{
if (lParam == WM_LBUTTONDBLCLK)
{
//删除任务栏中的图标
Shell_NotifyIcon(NIM_DELETE, &m_Nid);
//显示窗口
ShowWindow(SW_SHOWNORMAL);
UpdateWindow();
}
else if ((lParam == WM_LBUTTONDOWN) || (lParam == WM_RBUTTONDOWN))
{
//显示快捷子菜单
CMenu menu;
if (!menu.LoadMenu(IDR_MENU1))
{
return;
}
CPoint point;
GetCursorPos(&point);
SetForegroundWindow();
CMenu* pSubMenu = menu.GetSubMenu(0);
if (pSubMenu == NULL)
{
return;
}
pSubMenu->TrackPopupMenu(TPM_LEFTBUTTON | TPM_RIGHTBUTTON,
point.x, point.y, this, NULL);
PostMessage(WM_NULL, 0, 0);
}
}
}
复制代码
4.实现程序退出时,对图标的清理。
void CGkbc8Dlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
//删除任务栏中的图标
Shell_NotifyIcon(NIM_DELETE, &m_Nid);
CDialog::OnClose();
}
复制代码
欢迎光临 工控编程吧 (https://www.gkbc8.com/)
Powered by Discuz! X3.4