TA的每日心情 | 开心 2023-6-19 14:36 |
---|
签到天数: 2 天 [LV.1]初来乍到
24:00金丹期
- 积分
- 80
|
需求:当点击系统托盘图标 主窗口不需要显示
实现:
1、 在此类中设置回调函数接口 CShellNotifyHwnd2
回调函数定义
#include <functional> //
typedef std::function<bool()> FunShellNotifyIconLCLick;
void SetFunShellNotifyIconLCLick(FunShellNotifyIconLCLick fun) {
m_FunShellNotifyIconLCLick = fun;
}
系统托盘图标点击 先调用回调函数,如果回调函数已出来则 不执行默认处理
LRESULT CShellNotifyHwnd2::OnIconNotify(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL)
{
LRESULT bRet = S_FALSE;
switch (lParam)
{
case WM_LBUTTONDOWN:
{
bool bHandle = false;
if (NULL != m_FunShellNotifyIconLCLick)
{
bHandle = m_FunShellNotifyIconLCLick();
}
if (!bHandle)
{
if (m_pMainWnd->IsWindowVisible())
m_pMainWnd->ShowWindow(SW_HIDE);
else
{
m_pMainWnd->ShowWindow(SW_SHOW);
SetForegroundWindow(m_pMainWnd->m_hWnd);
}
}
bRet = S_OK;
}break;
2、SShellNotifyIcon 在此类中添加设置回调函数接口
void SShellNotifyIcon::SetFunShellNotifyIconLCLick(FunShellNotifyIconLCLick fun)
{
m_FunShellNotifyIconLCLick = fun;
if (NULL != m_MsgOnlyWnd)
{
m_MsgOnlyWnd->SetFunShellNotifyIconLCLick(fun);
}
}
3、在主窗口中设置回调函数
SShellNotifyIcon* pNotifyIcon = (SShellNotifyIcon*) FindChildByID2<SShellNotifyIcon>(8);
if (NULL != pNotifyIcon)
{
FunShellNotifyIconLCLick fun = std::bind(&CMainDlg::OnTrayIconLButtonDown, this);
pNotifyIcon->SetFunShellNotifyIconLCLick(fun);
}
回调函数处理如下
bool CMainDlg::OnTrayIconLButtonDown()
{
return true; //主窗体就不显示了
}
|
|