• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆-159  評論-223  文章-30  trackbacks-0
               關于系統托盤圖標類,網上也有很多的代碼,但都不太簡潔靈活易用,為了這一目的,本人自行封裝了一個API版本的實現類,這個類的設計思想來源于觀察者模式,從而較好地解耦了托盤通知消息的發送、接收、處理這三者間的關系,使這三者可以是同一個對象,也可以是不同的對象,具體的情況可根據業務邏輯靈活選擇控制,主要包括以下幾方面的特點:
            1)對于托盤通知消息的接收處理,提供了一個默認常用的實現,也就是通知消息接收由托盤類對象本身接收,消息事件的處理也由托盤對象本身處理,雙擊圖標顯示某個窗口,右鍵單擊圖標顯示快捷菜單。
            2)支持事件處理擴展,有兩種方法,一是提供一個接口設置消息事件處理器,從而將處理邏輯轉到其它對象,這個對象要支持消息事件處理器接口約定;二是提供消息事件處理的虛函數回調,可由派生類重寫定制。
            3)托盤圖標ID的管理,ID為UINT類型整數,一個對象對應一個ID,每當對象創建時,分配一個,從1開始,為防止沖突造成托盤圖標創建失敗,當對象銷毀時,并不遞減ID計數,因此ID一直是遞增的,這也是最簡單的方案。
            4)支持內部或外部窗口來接收通知消息,這兩種模式可以隨時相互轉換,一個托盤對象只對應一個窗口,多個托盤對象可以對應同一個窗口。如果是內部窗口,那么內部窗口是全局的有且僅有一個,由其窗口過程根據托盤ID將消息分發到對應的托盤對象。如果是外部窗口,那么可以由外部窗口自己處理,也可以調用托盤類的方法轉到其它對象處理。

               下面來看下基本托盤圖標類的接口定義:
             1#ifndef _BASIC_TRAYICON_H
             2#define _BASIC_TRAYICON_H
             3
             4#include "trayicon_msg_handler.h"
             5#include <shellapi.h>
             6#include <map>
             7
             8extern const UINT WM_TRAY_NOTIFY_MSG;
             9
            10class CBasicTrayIcon : public CTrayMsgDefaultHandler
            11{
            12public:
            13    CBasicTrayIcon();
            14    virtual    ~CBasicTrayIcon();
            15
            16    bool Create(HWND hNotifyWnd=NULL);
            17    void Destroy();
            18
            19    bool SetNotifyWnd(HWND hWnd);
            20    void SetMsgHandler(ITrayMsgHandler* pMsgHandler);
            21
            22    bool SetIcon(HICON hIcon);
            23    bool SetIcon(LPCTSTR lpIconName);
            24    bool SetIcon(UINT uID);
            25    bool SetTooltipText(LPCTSTR lpText);
            26    bool SetTooltipText(UINT uID);
            27
            28    UINT GetID() const;
            29    HICON GetIcon() const;
            30    LPCTSTR GetTooltipText() const;
            31
            32    void Show();
            33    void Hide();
            34
            35public:
            36    static CBasicTrayIcon* FromID(UINT uID);
            37    void OnNotify(UINT uEvent);
            38    
            39private:
            40    static LRESULT CALLBACK NotifyWndProc(HWND, UINT, WPARAM, LPARAM);
            41    static bool RegisterNotifyWndClass();
            42    static bool CreateNotifyWindow();
            43    static HWND s_hNotifyWnd;
            44    static UINT s_uIDCount;
            45    static std::map<UINT,CBasicTrayIcon*> s_trayicons;
            46
            47private:
            48    NOTIFYICONDATA     m_tnd;
            49    ITrayMsgHandler* m_pMsgHandler;
            50}
            ;
            51
            52#endif
               從上可看出,CBasicTrayIcon從通知消息事件默認處理器CTrayMsgDefaultHandler繼承,也就是說CBasicTrayIcon類已實現了消息事件的默認處理。下面說明幾個重要的類方法
               1)Create---創建托盤圖標,請注意這里只是創建,沒有關聯圖標,因此在任務欄上并不會顯示,要顯示需要接著調用SetIcon系列方法,參數hWnd為托盤通知消息接收窗口,默認為NULL,當為NULL的時候,則會創建一個內部隱藏的窗口來接收消息,否則由其指定的外部窗口來接收托盤通知消息。如果成功那么返回true,否則返回false。
               2)Destroy---銷毀托盤圖標。
               3)SetNotifyWnd---設置通知消息接收窗口,當參數hWnd為NULL時,則轉移到內部窗口接收通知消息,否則轉為其指定的外部窗口接收通知消息。因此,這個方法實現了特點4。
               4)SetMsgHandler---設置托盤通知消息處理器,參數pMsgHandler為ITrayMsgHandler指針類型,當其為NULL時,則處理為其指定的消息事件處理器,否則為托盤對象自己。因此,這個方法實現了特點2。
               5)SetIcon---設置托盤圖標,有3個重載形式,如果成功那么返回true,否則返回false。
               6)SetToolipText---設置提示文本,有2個重載形式,如果成功返回true,否則返回false。
               7)Show---顯示托盤圖標,具體的內部實現根據_WIN_IE的版本而定,如果成功返回true,否則返回fasle。
               8)Hide---隱藏托盤圖標,具體的內部實現根據_WIN_IE的版本而定,如果成功返回true,否則返回fasle。
               9)FromID---這是個靜態方法,從ID轉換為托盤對象,如果成功那么返回對應的指針,否則返回NULL。
               10)OnNotify---通知消息事件分發處理。公開方法9和10,是為了支持由外部窗口接收通知消息時可以轉交給其它對象的靈活性(見特點4)。這個類默認實現消息的處理者為托盤對象自己,即m_pMsgHandler在構造時指向它自己。

               接下來,看下消息事件處理器的接口定義:
             1#ifndef _TRAYICON_MSG_HANDLER_H
             2#define _TRAYICON_MSG_HANDLER_H
             3
             4struct ITrayMsgHandler
             5{
             6    virtual ~ITrayMsgHandler() {}
             7    virtual void OnMouseMove() {}
             8    virtual void OnLButtonDown() {}
             9    virtual void OnLButtonDblclk() {}
            10    virtual void OnRButtonUp() {}
            11    virtual void OnRButtonDown() {}
            12}
            ;
            13
            14class CTrayMsgDefaultHandler : public ITrayMsgHandler
            15{
            16public:
            17    CTrayMsgDefaultHandler();
            18    virtual ~CTrayMsgDefaultHandler();
            19
            20    bool SetMenu(UINT uID);
            21    bool SetMenu(LPCTSTR lpName);
            22    void SetMenuOwnWnd(HWND hWnd);
            23
            24protected:
            25    virtual void OnLButtonDblclk();
            26    virtual void OnRButtonUp();
            27
            28    bool ShowMenu();
            29    void DestroyMenu();
            30
            31protected:
            32    HWND  m_hOwnWnd;
            33    HMENU m_hMenu;
            34}
            ;
            35
            36#endif
            nbsp;  從上可知,很顯然,托盤消息事件處理器接口ITrayMsgHandler為5種鼠標事件提供了空實現,而CTrayMsgDefaultHandler默認處理器實現了鼠標左鍵雙擊(顯示m_hOwnWnd指定的窗口)和右鍵單擊的行為(彈出上下文菜單),因此這個類實現了特點1,在使用時要先調用SetMenu方法來設置菜單ID或名稱以創建菜單,調用SetMenuOwnWnd方法來設置菜單擁有者窗口。如果需要特定的行為,那么依上文特點2所述,從CBasicTrayIcon或者ITrayMsgHandler派生子類實現那5種(或其中幾種)事件即可,對于后種方法,再調用CBasicTrayIcon::SetMsgHandler方法設置為新的特定消息事件處理器。

               最后,列出CBasicTrayIcon的源碼,如下所示
              1#include "stdafx.h"
              2#include "basic_trayicon.h"
              3using namespace std;
              4
              5HWND CBasicTrayIcon::s_hNotifyWnd = NULL;
              6UINT CBasicTrayIcon::s_uIDCount = 0;
              7map<UINT,CBasicTrayIcon*> CBasicTrayIcon::s_trayicons;
              8
              9const UINT WM_TRAY_NOTIFY_MSG = ::RegisterWindowMessage(_T("TrayNotifyMsg"));
             10
             11bool CBasicTrayIcon::RegisterNotifyWndClass()
             12{
             13    WNDCLASSEX wndclass;
             14
             15    wndclass.cbSize = sizeof(wndclass);    
             16    wndclass.style = CS_HREDRAW|CS_VREDRAW;    
             17    wndclass.lpfnWndProc = NotifyWndProc;    
             18    wndclass.cbClsExtra = 0;        
             19    wndclass.cbWndExtra = 0;        
             20    wndclass.hInstance = ::GetModuleHandle(NULL); 
             21    wndclass.hIcon = 0;
             22    wndclass.hCursor = 0;
             23    wndclass.hbrBackground = 0;
             24    wndclass.lpszMenuName = NULL;        
             25    wndclass.lpszClassName = _T("NotifyWndClass");
             26    wndclass.hIconSm = NULL;        
             27
             28    return 0!=::RegisterClassEx(&wndclass); 
             29}

             30
             31bool CBasicTrayIcon::CreateNotifyWindow()
             32{
             33    if (s_hNotifyWnd) return true;
             34
             35    if (!RegisterNotifyWndClass())
             36        return false;
             37
             38    s_hNotifyWnd = ::CreateWindowEx( 
             39        0,            
             40        _T("NotifyWndClass"),                
             41        _T(""),        
             42        WS_OVERLAPPEDWINDOW,        
             43        CW_USEDEFAULT,            
             44        CW_USEDEFAULT,                
             45        CW_USEDEFAULT,                
             46        CW_USEDEFAULT,                
             47        NULL,                        
             48        NULL,                
             49        ::GetModuleHandle(NULL),                
             50        NULL) ;            
             51    return NULL!=s_hNotifyWnd;
             52}

             53
             54CBasicTrayIcon* CBasicTrayIcon::FromID(UINT uID)
             55{
             56    map<UINT,CBasicTrayIcon*>::iterator iter = s_trayicons.find(uID);
             57    if (iter==s_trayicons.end()) 
             58        return NULL;
             59    return iter->second;
             60}

             61
             62void CBasicTrayIcon::OnNotify(UINT uEvent)
             63{
             64    switch(uEvent)
             65    
             66        case WM_LBUTTONDBLCLK: m_pMsgHandler->OnLButtonDblclk(); break;
             67        case WM_LBUTTONDOWN: m_pMsgHandler->OnLButtonDown(); break;
             68        case WM_RBUTTONDOWN: m_pMsgHandler->OnRButtonDown(); break;
             69        case WM_RBUTTONUP:   m_pMsgHandler->OnRButtonUp(); break;
             70        case WM_MOUSEMOVE:   m_pMsgHandler->OnMouseMove(); break;
             71    }

             72}

             73
             74LRESULT CALLBACK CBasicTrayIcon::NotifyWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
             75{
             76    if (uMsg==WM_TRAY_NOTIFY_MSG)
             77    {
             78        CBasicTrayIcon* p = CBasicTrayIcon::FromID(wParam);
             79        if (p) p->OnNotify(lParam);
             80        return 0;
             81    }

             82    return ::DefWindowProc(hWnd, uMsg, wParam, lParam);
             83}

             84
             85CBasicTrayIcon::CBasicTrayIcon()
             86:m_pMsgHandler(this)
             87{
             88    m_tnd.uID = ++s_uIDCount;
             89    s_trayicons.insert(make_pair(m_tnd.uID,this));
             90}

             91
             92CBasicTrayIcon::~CBasicTrayIcon()
             93{
             94    s_trayicons.erase(m_tnd.uID);
             95    Destroy();
             96}

             97
             98bool CBasicTrayIcon::Create(HWND hNotifyWnd)
             99{
            100    if (NULL==hNotifyWnd)
            101    {
            102        if (!CreateNotifyWindow())
            103            return false;
            104        hNotifyWnd = s_hNotifyWnd;
            105    }

            106    m_tnd.cbSize = sizeof(NOTIFYICONDATA);
            107    m_tnd.hWnd = hNotifyWnd;
            108    m_tnd.uCallbackMessage = WM_TRAY_NOTIFY_MSG;
            109    m_tnd.uFlags = NIF_MESSAGE;
            110    return Shell_NotifyIcon(NIM_ADD, &m_tnd);
            111}

            112
            113void CBasicTrayIcon::Destroy()
            114{
            115    Shell_NotifyIcon(NIM_DELETE, &m_tnd);
            116}

            117
            118bool CBasicTrayIcon::SetNotifyWnd(HWND hWnd)
            119{
            120    if (NULL==hWnd)
            121    {
            122        if (!CreateNotifyWindow())
            123            return false;
            124        hWnd = s_hNotifyWnd;
            125    }

            126    m_tnd.hWnd = hWnd;
            127    m_tnd.uCallbackMessage = WM_TRAY_NOTIFY_MSG;
            128    m_tnd.uFlags |= NIF_MESSAGE;
            129    return Shell_NotifyIcon(NIM_MODIFY, &m_tnd);
            130}

            131
            132void CBasicTrayIcon::SetMsgHandler(ITrayMsgHandler* pMsgHandler)
            133{
            134    m_pMsgHandler = (pMsgHandler ? pMsgHandler : this);
            135}

            136
            137bool CBasicTrayIcon::SetIcon(HICON hIcon)
            138{
            139    m_tnd.uFlags |= NIF_ICON;
            140    m_tnd.hIcon = hIcon;
            141    return Shell_NotifyIcon(NIM_MODIFY, &m_tnd);
            142}

            143
            144bool CBasicTrayIcon::SetIcon(LPCTSTR lpIconName)
            145{
            146    HICON hIcon = ::LoadIcon(::GetModuleHandle(NULL),lpIconName);
            147    return SetIcon(hIcon);
            148}

            149
            150bool CBasicTrayIcon::SetIcon(UINT uID)
            151{
            152    HICON hIcon = ::LoadIcon(::GetModuleHandle(NULL),MAKEINTRESOURCE(uID));
            153    return SetIcon(hIcon);
            154}

            155
            156bool CBasicTrayIcon::SetTooltipText(LPCTSTR lpText)
            157{
            158    m_tnd.uFlags |= NIF_TIP;
            159    size_t min = std::min<size_t>(sizeof(m_tnd.szTip)/sizeof(TCHAR)-1,_tcslen(lpText));
            160    _tcsncpy(m_tnd.szTip,lpText,min);
            161    m_tnd.szTip[min] = _T('\0');
            162    return Shell_NotifyIcon(NIM_MODIFY, &m_tnd);
            163}

            164
            165bool CBasicTrayIcon::SetTooltipText(UINT uID)
            166{
            167    TCHAR szBuf[sizeof(m_tnd.szTip)/sizeof(TCHAR)];
            168    ::LoadString(GetModuleHandle(NULL),uID,szBuf,sizeof(szBuf)/sizeof(szBuf));
            169    return SetTooltipText(szBuf);
            170}

            171
            172UINT CBasicTrayIcon::GetID() const
            173{
            174    return m_tnd.uID;
            175}

            176
            177HICON CBasicTrayIcon::GetIcon() const
            178{
            179    assert(m_tnd.uFlags&NIF_ICON);
            180    return m_tnd.hIcon;
            181}

            182
            183LPCTSTR CBasicTrayIcon::GetTooltipText() const
            184{
            185    assert(m_tnd.uFlags&NIF_TIP);
            186    return m_tnd.szTip;
            187}

            188
            189void CBasicTrayIcon::Show()
            190{
            191    DWORD dwFlag;
            192#if (_WIN32_IE >= 0x0500)
            193    m_tnd.uFlags |= NIF_STATE;
            194    m_tnd.dwState = 0;
            195    m_tnd.dwStateMask = NIS_HIDDEN;
            196    dwFlag = NIM_MODIFY;
            197#else
            198    dwFlag = NIM_ADD;
            199#endif
            200    Shell_NotifyIcon(dwFlag,&m_tnd);
            201}

            202
            203void CBasicTrayIcon::Hide()
            204{
            205    DWORD dwFlag;
            206#if (_WIN32_IE >= 0x0500)
            207    m_tnd.uFlags |= NIF_STATE;
            208    m_tnd.dwState = NIS_HIDDEN;
            209    m_tnd.dwStateMask = NIS_HIDDEN;
            210    dwFlag = NIM_MODIFY;
            211#else
            212    dwFlag = NIM_DELETE;
            213#endif
            214    Shell_NotifyIcon(dwFlag,&m_tnd);
            215}
               CTrayMsgDefaultHandler的源碼,如下所示
             1#include "stdafx.h"
             2#include "trayicon_msg_handler.h"
             3
             4CTrayMsgDefaultHandler::CTrayMsgDefaultHandler()
             5:m_hOwnWnd(NULL)
             6,m_hMenu(NULL)
             7{
             8}

             9
            10CTrayMsgDefaultHandler::~CTrayMsgDefaultHandler()
            11{
            12    DestroyMenu();
            13}

            14
            15bool CTrayMsgDefaultHandler::SetMenu(UINT uID)
            16{
            17    return SetMenu(MAKEINTRESOURCE(uID));
            18}

            19
            20bool CTrayMsgDefaultHandler::SetMenu(LPCTSTR lpName)
            21{
            22    return m_hMenu=LoadMenu(GetModuleHandle(NULL),lpName);
            23}

            24
            25void CTrayMsgDefaultHandler::SetMenuOwnWnd(HWND hWnd)
            26{
            27    m_hOwnWnd = hWnd;
            28}

            29
            30bool CTrayMsgDefaultHandler::ShowMenu()
            31{
            32    assert(m_hMenu);
            33    HMENU hSubMenu = GetSubMenu(m_hMenu,0);
            34    if (NULL==hSubMenu) return false;
            35
            36    POINT pos;
            37    GetCursorPos(&pos);
            38    return ::TrackPopupMenu(hSubMenu, TPM_LEFTALIGN|TPM_BOTTOMALIGN, pos.x, pos.y,0,m_hOwnWnd,NULL);
            39}

            40
            41void CTrayMsgDefaultHandler::DestroyMenu()
            42{
            43    if (m_hMenu) ::DestroyMenu(m_hMenu);
            44}

            45
            46void CTrayMsgDefaultHandler::OnLButtonDblclk()
            47{
            48    if (m_hOwnWnd&&::IsWindow(m_hOwnWnd))
            49        ::ShowWindow(m_hOwnWnd,SW_RESTORE);
            50}

            51
            52void CTrayMsgDefaultHandler::OnRButtonUp()
            53{
            54    ShowMenu();
            55}

             

            posted on 2011-12-04 03:15 春秋十二月 閱讀(1960) 評論(0)  編輯 收藏 引用 所屬分類: C/C++
            久久精品人人做人人爽电影蜜月| 99久久久国产精品免费无卡顿| 噜噜噜色噜噜噜久久| 久久只这里是精品66| 久久精品国产亚洲AV无码偷窥| 伊人久久大香线蕉影院95| 四虎国产精品免费久久| 国产精品18久久久久久vr| 日本久久久久久久久久| 国内精品九九久久久精品| 伊人久久大香线蕉AV一区二区 | 色综合久久夜色精品国产| 久久亚洲欧美国产精品| 四虎影视久久久免费观看| 97精品久久天干天天天按摩| 久久亚洲精品国产精品婷婷| 久久99精品国产一区二区三区| 亚洲Av无码国产情品久久| 久久线看观看精品香蕉国产| 亚洲成色WWW久久网站| 久久久久99精品成人片 | 色综合久久天天综线观看| 国产高潮国产高潮久久久| 大香伊人久久精品一区二区| 国产伊人久久| 国产精品18久久久久久vr | 久久精品这里只有精99品| 国产精品毛片久久久久久久| 77777亚洲午夜久久多人| 三级片免费观看久久| 久久99精品国产麻豆不卡| 9久久9久久精品| 99国产欧美久久久精品蜜芽| av色综合久久天堂av色综合在| 欧美午夜精品久久久久久浪潮| 国产精品美女久久久网AV| 精品一区二区久久| 日本道色综合久久影院| 亚洲国产精品久久久久婷婷软件 | 一级女性全黄久久生活片免费| 精品人妻伦一二三区久久|