系统托盘气泡提示和上下文(右键)菜单

WTL嵌入类的架构可以使整个程序很清晰,系统托盘气泡提示、上下文菜单处理可以分别独立出来,以下是实现代码,很清晰就不注释了。基本上这两个类很少需要修改,因此我把它们放到了系统包含文件搜索路径中。

//ContextMenu.h

#pragma once

template <class T>

class CContextMenu

{

public:

       BOOL CreateContextMenu(UINT ID_Menu)

       {

              T* pT = static_cast<T*>(this);

              CMenu menu;

              menu.LoadMenu(ID_Menu);

              CMenu SubMenu(menu.GetSubMenu(0));

              POINT pos;

              GetCursorPos(&pos);

              SubMenu.TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, pos.x, pos.y, pT->m_hWnd);

              return TRUE;

       }

};

//ShellIcon.h

#pragma once

 

#define WM_ICON WM_USER + 180

#define NM_ICON_INFO WM_ICON + 1

CString g_childInfo;

 

template <class T, class MenuT = CIconMenu<T>, int MenuID = IDR_ICONMENU>

class CShellIcon : public MenuT

{

private:

       NOTIFYICONDATA m_data;

       UINT m_msgTaskbarRestart;

       CString m_appName;

public:

       CShellIcon()

       {

              m_appName.LoadString(IDS_APPNAME);

              m_msgTaskbarRestart = RegisterWindowMessage(TEXT("TaskbarCreated"));

       }

 

       ~CShellIcon()

       {

              Shell_NotifyIcon(NIM_DELETE, &m_data);

       }

       BOOL CreateShellIcon()

       {

              T* pT = static_cast<T*>(this);

              SecureZeroMemory(&m_data, sizeof(m_data));

              m_data.cbSize = sizeof(m_data);

              m_data.hIcon = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME),

              IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);

              m_data.hWnd = pT->m_hWnd;

              m_data.uID = IDR_MAINFRAME;

              m_data.uFlags = NIF_ICON | NIF_MESSAGE | NIF_INFO | NIF_TIP;

              m_data.uCallbackMessage = WM_ICON;

              m_data.dwInfoFlags = NIIF_USER;

              strcpy_s(m_data.szInfoTitle, m_appName);

              strcpy_s(m_data.szTip, m_appName);

              return Shell_NotifyIcon(NIM_ADD, &m_data);

       }

 

       void ModifyToolTips(LPCTSTR info)

       {

              strcpy_s(m_data.szInfo, info);

       }

 

       BOOL DispalyToolTips()

       {

              return Shell_NotifyIcon(NIM_MODIFY, &m_data);

       }

 

       BOOL BalloonToolTips(LPCTSTR info, DWORD dwInfoFlags = NIIF_USER)

       {

              ModifyToolTips(info);

              m_data.dwInfoFlags = dwInfoFlags;

              return DispalyToolTips();

       }

 

 

       BEGIN_MSG_MAP(CShellIcon)

              MESSAGE_HANDLER(WM_ICON, OnIcon)

              MESSAGE_HANDLER(m_msgTaskbarRestart, OnRestart)

              MESSAGE_HANDLER(WM_SIZE, OnSize)

              NOTIFY_CODE_HANDLER(NM_ICON_INFO, OnIconInfo)

              CHAIN_MSG_MAP(MenuT)

       END_MSG_MAP()

 

       LRESULT OnIcon(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)

       {

              T* pT = static_cast<T*>(this); char t;

              if (wParam != IDR_MAINFRAME) return 1;

              switch(lParam)

              {

              case WM_RBUTTONUP:

                     t = *m_data.szInfo;

                     *m_data.szInfo = '/0';

                     Shell_NotifyIcon(NIM_MODIFY, &m_data);

                     pT->CreateContextMenu(MenuID);

                     *m_data.szInfo = t;

                     break;

              case WM_LBUTTONUP:

                     pT->ShowWindow(SW_SHOW);

                     OpenIcon(pT->m_hWnd);

                     break;

//去掉下面的注释可以使鼠标悬停在图标上时出现气泡提示,个人不太喜欢

              case WM_MOUSEMOVE:

       //            DispalyToolTips(); break;

              default:

                     ;

              }

              return 0;

       }

       LRESULT OnIconInfo(int wParam, LPNMHDR pNMHDR, BOOL& /*bHandled*/)

       {

              BalloonToolTips(g_childInfo, wParam);

              g_childInfo.Empty();

              return 0;

       }

 

//处理Explorer外壳崩溃后任务栏重建,你会发现很多程序都没处理,结果就是Explorer一崩溃

//图标就找不到了,如果最小化到任务栏更惨,还得用任务管理器关闭,这个函数我没机会测试,希望有作用

 

       LRESULT OnRestart(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)

       {

              T* pT = static_cast<T*>(this);

              SecureZeroMemory(&m_data, sizeof(m_data));

              m_data.cbSize = sizeof(m_data);

              m_data.hWnd = pT->m_hWnd;

              m_data.uID = IDR_MAINFRAME;

              Shell_NotifyIcon(NIM_DELETE, &m_data);

              CreateShellIcon();

              return 0;

       }

 

       LRESULT OnSize(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)

       {

              T* pT = static_cast<T*>(this);

              if (pT->IsIconic()) pT->ShowWindow(SW_HIDE);

              return 0;

       }

 

};

//MyIconMenu.h

#pragma once

#include "stdafx.h"

#include <ContextMenu.h>

template <class T>

class CMyIconMenu : public CContextMenu<T>

{

public:

       BEGIN_MSG_MAP(CContextMenu)

              COMMAND_ID_HANDLER(ID_RESUME, OnResume)

              COMMAND_ID_HANDLER(ID_QUIT, OnQuit)

       END_MSG_MAP()

 

       LRESULT OnResume(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)

       {

              T* pT = static_cast<T*>(this);

              pT->ShowWindow(SW_SHOW);

              OpenIcon(pT->m_hWnd);

              return 0;

       }

 

       LRESULT OnQuit(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)

       {

              PostQuitMessage(0);

              return 0;

       }

};

CContextMenu的使用

如果你的窗口需要上下文菜单,只需要从CContextMenu派生一个类,例如:template <class T> class CMyContextMenu : public CContextMenu<T>

然后完成消息映射,例如CMyIconMenu所做的。在你的窗口类(假设为CMyWindowClass)的继承列表里面添加public CMyContextMenu<CMyWindowClass>,在消息映射表中添加CHAIN_MSG_MAP(CMyContextMenu<CMyWindowClass>)

接下来只要在需要显示上下文菜单的地方,调用CreatContextMenu(UINT ID_Menu)就可以了。

CShellIcon的使用

首先要把stdafx.hIE的版本改成5或者6,例如:

#define _WIN32_IE      0x0600

然后加入CString支持,可能每个项目都几乎用到CString,大家不妨直接修改模板文件,让向导直接把CString加进去算了。

CContextMenu派生一个类来完成菜单映射,例如CMyIconMenu所作的。

以下用模式对话框来演示如何给其加入托盘功能,蓝色的部分为添加的部分。

#pragma once

#include "MyIconMenu.h"

#include <ShellIcon.h>

class CMainDlg : public CDialogImpl<CMainDlg>, public CShellIcon<CMainDlg, CMyIconMenu<CMainDlg>, IDR_ICONMENU>

{

public:

       enum { IDD = IDD_MAINDLG };

 

       typedef CShellIcon<CMainDlg, CMyIconMenu<CMainDlg>, IDR_ICONMENU> CMyShellIcon;

 

       BEGIN_MSG_MAP(CMainDlg)

              MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)

              COMMAND_ID_HANDLER(ID_APP_ABOUT, OnAppAbout)

              COMMAND_ID_HANDLER(IDOK, OnOK)

              COMMAND_ID_HANDLER(IDCANCEL, OnCancel)

              CHAIN_MSG_MAP(CMyShellIcon)

       END_MSG_MAP()

       LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)

       {

              // center the dialog on the screen

              CenterWindow();

 

              // set icons

              HICON hIcon = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME),

                     IMAGE_ICON, ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON), LR_DEFAULTCOLOR);

              SetIcon(hIcon, TRUE);

              HICON hIconSmall = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME),

                     IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);

              SetIcon(hIconSmall, FALSE);

 

              CreateShellIcon();

 

              return TRUE;

       }

……………………

};

在想弹出气泡的时候调用BOOL BalloonToolTips,如果子窗体想弹出提示,向父窗体发送WM_NOTIFY,pNMHDR->code = NM_ICON_INFO,wParam为图标类型 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值