日期:2010-07-31  浏览次数:20534 次

  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
template <class T, class MenuT, int MenuID>
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 = LoadIcon(_Module.get_m_hInst(), MAKEINTRESOURCE(IDR_MAINFRAME));
              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);
  &nb