系统栏图标添加规则:(以CDialog为例)
1, 在Dialog类中加入显示系统栏图标的变量 NOTIFYICONDATA m_NotifyIcon;
2, 在Dialog类中加入消息处理函数afx_msg LRESULT OnNotifyIcon(WPARAM wParam, LPARAM lParam) 来响应对系统栏图标的响应。
3, 在BEGIN_MESSAGE_MAP(), END_MESSAGE_MAP()中间添加消息处理映射ON_MESSAGE(WM_NC, &CMyCPL2Dlg::OnNotifyIcon)
4, 在Dialog类的初始化函数OnInitDialog()中添加系统栏图标初始化:
m_NotifyIcon.cbSize = sizeof(NOTIFYICONDATA);//
m_NotifyIcon.hIcon = AfxGetApp()->LoadIcon(IDI_MYCPLDLG);//图标图案
m_NotifyIcon.hWnd = m_hWnd;//
lstrcpy(m_NotifyIcon.szTip, "UVC Control");//鼠标指示时显示提示
m_NotifyIcon.uCallbackMessage = WM_NC;//消息响应函数
m_NotifyIcon.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE;//图标类型
Shell_NotifyIcon(NIM_ADD, &m_NotifyIcon);//系统栏添加图标
5, 添加消息响应函数
LRESULT CMyCPL2Dlg::OnNotifyIcon(WPARAM wParam, LPARAM lParam)
{
if ((lParam == WM_LBUTTONDOWN)) // Lbutton down
{
ModifyStyleEx(0,WS_EX_TOPMOST);
ShowWindow(SW_SHOW); // Show Main Window
//Shell_NotifyIcon(NIM_DELETE, &NotifyIcon);
}
}
6, 如果有需要,可以在关闭对话框时删除系统栏图标。添加函数DestroyWindow()
BOOL CMyCPL2Dlg::DestroyWindow()
{
// TODO: Add your specialized code here and/or call the base class
Shell_NotifyIcon(NIM_DELETE, &m_NotifyIcon); // Delete Icon
return CDialog::DestroyWindow();
}