source: trunk/Utilities/ShowHresult/NotifyIconWindow.h @ 116

Last change on this file since 116 was 116, checked in by roman, 11 years ago

Added NS_ identifiers

  • Property svn:keywords set to Id
File size: 21.6 KB
Line 
1////////////////////////////////////////////////////////////
2// Copyright (C) Roman Ryltsov, 2008-2011
3// Created by Roman Ryltsov roman@alax.info
4//
5// $Id: NotifyIconWindow.h 116 2012-09-08 09:50:30Z roman $
6
7#pragma once
8
9#include <vfwmsgs.h>
10#include <asferr.h> // FACILITY_NS
11#include <nserror.h> // FACILITY_NS
12#include <mferror.h> // FACILITY_MF
13#include <wmcodecdsp.h> // FACILITY_WMAAECMA
14#include <ddraw.h> // _FACD3D
15//#include <d3d9helper.h> // _FACD3D
16#include <d3d9.h> // _FACD3D
17#include <d2derr.h> // FACILITY_D2D
18#include <wincodec.h> // FACILITY_WINCODEC_ERR
19#include <wia_lh.h> // FACILITY_WIA
20#include <corerror.h> // FACILITY_URT
21#include "rowinhttp.h"
22#include "AboutDialog.h"
23
24////////////////////////////////////////////////////////////
25// CNotifyIconWindow
26
27class CNotifyIconWindow : 
28        public CMessageOnlyWindowImpl<CNotifyIconWindow>
29{
30public:
31
32DECLARE_WND_CLASS(_T("CNotifyIconWindow"))
33
34BEGIN_MSG_MAP_EX(CNotifyIconWindow)
35        _Z4(atlTraceUI, 4, _T("uMsg 0x%04X, wParam 0x%08X, lParam 0x%08X\n"), uMsg, wParam, lParam);
36        //CHAIN_MSG_MAP(CMessageOnlyWindowImpl<CNotifyIconWindow>)
37        MSG_WM_CREATE(OnCreate)
38        MSG_WM_DESTROY(OnDestroy)
39        MSG_WM_CHANGECBCHAIN(OnChangeCbChain)
40        MSG_WM_DRAWCLIPBOARD(OnDrawClipboard)
41        MSG_WM_MOUSEMOVE(OnMouseMove)
42        MSG_WM_LBUTTONDBLCLK(OnLButtonDblClk)
43        MSG_WM_RBUTTONUP(OnRButtonUp)
44        MESSAGE_HANDLER_EX(NIN_BALLOONUSERCLICK, OnNotifyIconBalloonUserClick)
45        MESSAGE_HANDLER_EX(WM_NOTIFYICON, OnNotifyIcon)
46        COMMAND_ID_HANDLER_EX(ID_APP_ABOUT, OnApplicationAbout)
47        COMMAND_ID_HANDLER_EX(ID_APP_EXIT, OnApplicationExit)
48        REFLECT_NOTIFICATIONS()
49END_MSG_MAP()
50
51public:
52
53        //////////////////////////////////////////////////////////
54        // Window Message Identifiers
55
56        enum
57        {
58                WM_FIRST = WM_APP,
59                WM_NOTIFYICON,
60        };
61
62private:
63        ULONGLONG m_nCommonControlsVersion;
64        ULONGLONG m_nShellVersion;
65        NOTIFYICONDATA m_NotifyIconData;
66        CWindow m_NextClipboardViewerWindow;
67        CString m_sQuery;
68
69        static BOOL IsQuartzResult(HRESULT nResult, CString* psMessage = NULL)
70        {
71                if(HRESULT_FACILITY(nResult) != FACILITY_ITF)
72                        return FALSE;
73                const SCODE nCode = HRESULT_CODE(nResult);
74                if(nCode < 0x0200)// && nCode <= 0xFFFF)
75                        return FALSE;
76                const CString sMessage = AtlFormatSystemMessage(CDataLibraryMap::LoadLibrary(_T("quartz.dll")), nResult);
77                if(sMessage.IsEmpty())
78                        return FALSE;
79                if(psMessage)
80                        *psMessage = sMessage;
81                return TRUE;
82        }
83        static BOOL LookupQuartzIdentifier(HRESULT nValue, CString& sIdentifier)
84        {
85                _A(sIdentifier.IsEmpty());
86                static const struct { HRESULT nValue; LPCSTR pszName; } g_pMap[] = 
87                {
88                        #define A(x) { x, #x },
89                        #include "DsIdentifier.inc"
90                        #undef A
91                };
92                for(SIZE_T nIndex = 0; nIndex < DIM(g_pMap); nIndex++)
93                        if(g_pMap[nIndex].nValue == nValue)
94                        {
95                                sIdentifier = CString(g_pMap[nIndex].pszName);
96                                return TRUE;
97                        }
98                return FALSE;
99        }
100        static BOOL IsWmResult(HRESULT nResult, CString* psMessage = NULL)
101        {
102                if(HRESULT_FACILITY(nResult) != FACILITY_NS)
103                        return FALSE;
104                // SUGG: Cache loaded libraries
105                HMODULE hModule = NULL;
106                const SCODE nCode = HRESULT_CODE(nResult);
107                CString sMessage;
108                if(nCode >= 2000 && nCode < 3000) // 2000 -  2999 = ASF (defined in ASFERR.MC)
109                        sMessage = AtlFormatSystemMessage(CDataLibraryMap::LoadLibrary(_T("asferror.dll")), nResult);
110                else
111                        sMessage = AtlFormatSystemMessage(CDataLibraryMap::LoadLibrary(_T("wmerror.dll")), nResult);
112                if(sMessage.IsEmpty())
113                        return FALSE;
114                if(psMessage)
115                        *psMessage = sMessage;
116                return TRUE;
117        }
118        static BOOL LookupWmIdentifier(HRESULT nValue, CString& sIdentifier)
119        {
120                static const struct { HRESULT nValue; LPCSTR pszName; } g_pMap[] = 
121                {
122                        #define A(x) { x, #x },
123                        #include "WmIdentifier.inc"
124                        #undef A
125                };
126                for(SIZE_T nIndex = 0; nIndex < DIM(g_pMap); nIndex++)
127                        if(g_pMap[nIndex].nValue == nValue)
128                        {
129                                sIdentifier = CString(g_pMap[nIndex].pszName);
130                                return TRUE;
131                        }
132                return FALSE;
133        }
134        static BOOL IsMfResult(HRESULT nResult, CString* psMessage = NULL)
135        {
136                if(HRESULT_FACILITY(nResult) != FACILITY_MF)
137                        return FALSE;
138                const CString sMessage = AtlFormatSystemMessage(CDataLibraryMap::LoadLibrary(_T("mferror.dll")), nResult);
139                if(sMessage.IsEmpty())
140                        return FALSE;
141                if(psMessage)
142                        *psMessage = sMessage;
143                return TRUE;
144        }
145        static BOOL LookupMfIdentifier(HRESULT nValue, CString& sIdentifier)
146        {
147                static const struct { HRESULT nValue; LPCSTR pszName; } g_pMap[] = 
148                {
149                        #define A(x) { x, #x },
150                        #include "MfIdentifier.inc"
151                        #undef A
152                };
153                for(SIZE_T nIndex = 0; nIndex < DIM(g_pMap); nIndex++)
154                        if(g_pMap[nIndex].nValue == nValue)
155                        {
156                                sIdentifier = CString(g_pMap[nIndex].pszName);
157                                return TRUE;
158                        }
159                return FALSE;
160        }
161        static BOOL IsWs2Result(HRESULT nResult, CString* psMessage = NULL)
162        {
163                if(HRESULT_FACILITY(nResult) != FACILITY_WIN32)
164                        return FALSE;
165                const SCODE nCode = HRESULT_CODE(nResult);
166                if(nCode < 10000 || nCode >= 11150) // WSABASEERR, ...
167                        return FALSE;
168                const CString sMessage = AtlFormatSystemMessage(CDataLibraryMap::LoadLibrary(_T("ws2_32.dll")), nResult);
169                if(sMessage.IsEmpty())
170                        return FALSE;
171                if(psMessage)
172                        *psMessage = sMessage;
173                return TRUE;
174        }
175        static BOOL IsWinHttpResult(HRESULT nResult, CString* psMessage = NULL)
176        {
177                if(HRESULT_FACILITY(nResult) != FACILITY_WIN32)
178                        return FALSE;
179                const LONG nCode = HRESULT_CODE(nResult);
180                if(nCode < 12000 || nCode >= 12200) // WINHTTP_ERROR_BASE, WINHTTP_ERROR_LAST
181                        return FALSE;
182                const CString sMessage = AtlFormatSystemMessage(CDataLibraryMap::LoadLibrary(_T("winhttp.dll")), nResult);
183                if(sMessage.IsEmpty())
184                        return FALSE;
185                if(psMessage)
186                        *psMessage = sMessage;
187                return TRUE;
188        }
189        static BOOL IsWinInetResult(HRESULT nResult, CString* psMessage = NULL)
190        {
191                if(HRESULT_FACILITY(nResult) != FACILITY_WIN32)
192                        return FALSE;
193                const LONG nCode = HRESULT_CODE(nResult);
194                if(nCode < 12000 || nCode >= 12200) // INTERNET_ERROR_BASE, INTERNET_ERROR_LAST
195                        return FALSE;
196                const CString sMessage = AtlFormatSystemMessage(CDataLibraryMap::LoadLibrary(_T("wininet.dll")), nResult);
197                if(sMessage.IsEmpty())
198                        return FALSE;
199                if(psMessage)
200                        *psMessage = sMessage;
201                return TRUE;
202        }
203        static BOOL LookupSystemIdentifier(HRESULT nValue, CString& sIdentifier)
204        {
205                if(HRESULT_SEVERITY(nValue) == SEVERITY_ERROR && HRESULT_FACILITY(nValue) == FACILITY_WIN32)
206                        nValue = HRESULT_CODE(nValue);
207                if(nValue < 0 || nValue >= 16384)
208                        return FALSE;
209                static const struct { HRESULT nValue; LPCSTR pszName; } g_pMap[] = 
210                {
211                        #define A(x) { x, #x },
212                        #include "SystemIdentifier.inc"
213                        #undef A
214                };
215                for(SIZE_T nIndex = 0; nIndex < DIM(g_pMap); nIndex++)
216                        if(g_pMap[nIndex].nValue == nValue)
217                        {
218                                sIdentifier = CString(g_pMap[nIndex].pszName);
219                                return TRUE;
220                        }
221                return FALSE;
222        }
223        static BOOL LookupHresultSystemIdentifier(HRESULT nValue, CString& sIdentifier)
224        {
225                //if(HRESULT_SEVERITY(nValue) != SEVERITY_ERROR)
226                //      return _T("");
227                if((UINT) HRESULT_FACILITY(nValue) >= 0x40)
228                        return FALSE;
229                static const struct { HRESULT nValue; LPCSTR pszName; } g_pMap[] = 
230                {
231                        #define A(x) { x, #x },
232                        #include "SystemHresultIdentifier.inc"
233                        #undef A
234                };
235                for(SIZE_T nIndex = 0; nIndex < DIM(g_pMap); nIndex++)
236                        if(g_pMap[nIndex].nValue == nValue)
237                        {
238                                sIdentifier = CString(g_pMap[nIndex].pszName);
239                                return TRUE;
240                        }
241                return FALSE;
242        }
243        static BOOL IsD3dResult(HRESULT nResult, CString* psMessage = NULL)
244        {
245                if(HRESULT_FACILITY(nResult) != _FACD3D)
246                        return FALSE;
247                psMessage;
248                CString sIdentifier;
249                return LookupD3dIdentifier(nResult, sIdentifier);
250        }
251        static BOOL LookupD3dIdentifier(HRESULT nValue, CString& sIdentifier)
252        {
253                static const struct { HRESULT nValue; LPCSTR pszName; } g_pMap[] = 
254                {
255                        #define A(x) { x, #x },
256                        #include "D3dIdentifier.inc"
257                        #undef A
258                };
259                for(SIZE_T nIndex = 0; nIndex < DIM(g_pMap); nIndex++)
260                        if(g_pMap[nIndex].nValue == nValue)
261                        {
262                                sIdentifier = CString(g_pMap[nIndex].pszName);
263                                return TRUE;
264                        }
265                return FALSE;
266        }
267        static BOOL IsD2dResult(HRESULT nResult, CString* psMessage = NULL)
268        {
269                if(HRESULT_FACILITY(nResult) != FACILITY_D2D)
270                        return FALSE;
271                psMessage;
272                CString sIdentifier;
273                return LookupD2dIdentifier(nResult, sIdentifier);
274        }
275        static BOOL LookupD2dIdentifier(HRESULT nValue, CString& sIdentifier)
276        {
277                static const struct { HRESULT nValue; LPCSTR pszName; } g_pMap[] = 
278                {
279                        #define A(x) { x, #x },
280                        #include "D2dIdentifier.inc"
281                        #undef A
282                };
283                for(SIZE_T nIndex = 0; nIndex < DIM(g_pMap); nIndex++)
284                        if(g_pMap[nIndex].nValue == nValue)
285                        {
286                                sIdentifier = CString(g_pMap[nIndex].pszName);
287                                return TRUE;
288                        }
289                return FALSE;
290        }
291        static BOOL IsWicResult(HRESULT nResult, CString* psMessage = NULL)
292        {
293                if(HRESULT_FACILITY(nResult) != FACILITY_WINCODEC_ERR)
294                        return FALSE;
295                psMessage;
296                CString sIdentifier;
297                return LookupWicIdentifier(nResult, sIdentifier);
298        }
299        static BOOL LookupWicIdentifier(HRESULT nValue, CString& sIdentifier)
300        {
301                static const struct { HRESULT nValue; LPCSTR pszName; } g_pMap[] = 
302                {
303                        #define A(x) { x, #x },
304                        #include "WicIdentifier.inc"
305                        #undef A
306                };
307                for(SIZE_T nIndex = 0; nIndex < DIM(g_pMap); nIndex++)
308                        if(g_pMap[nIndex].nValue == nValue)
309                        {
310                                sIdentifier = CString(g_pMap[nIndex].pszName);
311                                return TRUE;
312                        }
313                return FALSE;
314        }
315        static BOOL IsWiaResult(HRESULT nResult, CString* psMessage = NULL)
316        {
317                if(HRESULT_FACILITY(nResult) != FACILITY_WIA)
318                        return FALSE;
319                psMessage;
320                CString sIdentifier;
321                return LookupWiaIdentifier(nResult, sIdentifier);
322        }
323        static BOOL LookupWiaIdentifier(HRESULT nValue, CString& sIdentifier)
324        {
325                static const struct { HRESULT nValue; LPCSTR pszName; } g_pMap[] = 
326                {
327                        #define A(x) { x, #x },
328                        #include "WiaIdentifier.inc"
329                        #undef A
330                };
331                for(SIZE_T nIndex = 0; nIndex < DIM(g_pMap); nIndex++)
332                        if(g_pMap[nIndex].nValue == nValue)
333                        {
334                                sIdentifier = CString(g_pMap[nIndex].pszName);
335                                return TRUE;
336                        }
337                return FALSE;
338        }
339        static BOOL IsUrtResult(HRESULT nResult, CString* psMessage = NULL)
340        {
341                if(HRESULT_FACILITY(nResult) != FACILITY_URT)
342                        return FALSE;
343                psMessage;
344                CString sIdentifier;
345                return LookupUrtIdentifier(nResult, sIdentifier);
346        }
347        static BOOL LookupUrtIdentifier(HRESULT nValue, CString& sIdentifier)
348        {
349                static const struct { HRESULT nValue; LPCSTR pszName; } g_pMap[] = 
350                {
351                        #define A(x) { x, #x },
352                        #include "UrtIdentifier.inc"
353                        #undef A
354                };
355                for(SIZE_T nIndex = 0; nIndex < DIM(g_pMap); nIndex++)
356                        if(g_pMap[nIndex].nValue == nValue)
357                        {
358                                sIdentifier = CString(g_pMap[nIndex].pszName);
359                                return TRUE;
360                        }
361                return FALSE;
362        }
363
364public:
365// CNotifyIconWindow
366        static ULONGLONG GetCommonControlsVersion() throw()
367        {
368                DWORD nMajorVersion, nMinorVersion;
369                _W(SUCCEEDED(AtlGetCommCtrlVersion(&nMajorVersion, &nMinorVersion)));
370                return (ULONGLONG) ((nMajorVersion << 16) + nMinorVersion) << 32;
371        }
372        static ULONGLONG GetShellVersion() throw()
373        {
374                DWORD nMajorVersion, nMinorVersion;
375                _W(SUCCEEDED(AtlGetShellVersion(&nMajorVersion, &nMinorVersion)));
376                return (ULONGLONG) ((nMajorVersion << 16) + nMinorVersion) << 32;
377        }
378        CNotifyIconWindow()
379        {
380        }
381        HWND Create()
382        {
383                return __super::Create(NULL, rcDefault, _T("AlaxInfo.ShowHresult.NotifyIconWindow"));
384        }
385        static CString GetDefaultInfoTitle()
386        {
387                return AtlLoadString(IDS_NOTIFYICON_DEFAULTTIPTITLE);
388        }
389        static DWORD GetDefaultInfoFlags() throw()
390        {
391                return NIIF_NONE;
392        }
393        VOID SetBalloonToolTip(DWORD nFlags, LPCTSTR pszTitle, LPCTSTR pszText, UINT nTimeout = 30 * 1000)
394        {
395                if(IsWindow() && m_NotifyIconData.uFlags & NIF_INFO)
396                {
397                        m_NotifyIconData.uTimeout = nTimeout;
398                        m_NotifyIconData.dwInfoFlags = nFlags;
399                        _tcsncpy_s(m_NotifyIconData.szInfoTitle, pszTitle, _TRUNCATE);
400                        _tcsncpy_s(m_NotifyIconData.szInfo, pszText, _TRUNCATE);
401                        _W(Shell_NotifyIcon(NIM_MODIFY, &m_NotifyIconData));
402                }
403        }
404        static BOOL IsDecimal(TCHAR nCharacter) throw()
405        {
406                return _tcschr(_T("0123456789"), nCharacter) != NULL;
407        }
408        static BOOL IsDecimal(const TCHAR* pszCharacters, SIZE_T nCount) throw()
409        {
410                for(SIZE_T nIndex = 0; nIndex < nCount; nIndex++)
411                        if(!IsDecimal(pszCharacters[nIndex]))
412                                return FALSE;
413                return TRUE;
414        }
415        static BOOL IsDecimal(const TCHAR* pszCharacters) throw()
416        {
417                return IsDecimal(pszCharacters, _tcslen(pszCharacters));
418        }
419        static BOOL IsHexadecimal(TCHAR nCharacter) throw()
420        {
421                return _tcschr(_T("0123456789ABCDEFabcdef"), nCharacter) != NULL;
422        }
423        static BOOL IsHexadecimal(const TCHAR* pszCharacters, SIZE_T nCount) throw()
424        {
425                for(SIZE_T nIndex = 0; nIndex < nCount; nIndex++)
426                        if(!IsHexadecimal(pszCharacters[nIndex]))
427                                return FALSE;
428                return TRUE;
429        }
430        static BOOL IsHexadecimal(const TCHAR* pszCharacters) throw()
431        {
432                return IsHexadecimal(pszCharacters, _tcslen(pszCharacters));
433        }
434        BOOL Process(LPCTSTR pszText)
435        {
436                #pragma region Parse
437                if(_tcslen(pszText) > 24)
438                        return FALSE;
439                CString sText = pszText;
440                sText.Trim();
441                #pragma region Delete Trailing L (as in 0x00000000L)
442                if(!sText.IsEmpty() && _tcschr(_T("Ll"), sText[sText.GetLength() - 1]))
443                        sText.Delete(sText.GetLength() - 1);
444                #pragma endregion
445                #pragma region Unquote Single Quote
446                if(sText.GetLength() > 2 && sText[0] == _T('\'') && sText[sText.GetLength() - 1] == _T('\''))
447                {
448                        sText = sText.Mid(1, sText.GetLength() - 2);
449                        sText.Trim();
450                }
451                #pragma endregion
452                if(sText.IsEmpty())
453                        return FALSE;
454                LONGLONG nLongLongResult;
455                #pragma region Hexadecimal
456                if(_tcsnicmp(sText, _T("0x"), 2) == 0)
457                {
458                        if(!IsHexadecimal((LPCTSTR) sText + 2))
459                                return FALSE;
460                        if(!StrToInt64Ex(sText, STIF_SUPPORT_HEX, &nLongLongResult))
461                                return FALSE; 
462                } else
463                #pragma endregion
464                #pragma region Integer
465                {
466                        SIZE_T nIndex = 0;
467                        if(sText[0] == _T('-'))
468                                nIndex++;
469                        if(!IsDecimal((LPCTSTR) sText + nIndex))
470                        {
471                                #pragma region Eight Characater Long Hexadecimal without Prefix
472                                if(sText.GetLength() == nIndex + 8 && IsHexadecimal((LPCTSTR) sText + nIndex))
473                                {
474                                        sText.Insert(0, _T("0x"));
475                                        if(!StrToInt64Ex(sText, STIF_SUPPORT_HEX, &nLongLongResult))
476                                                return FALSE; 
477                                } else
478                                #pragma endregion
479                                        return FALSE;
480                        } else
481                                nLongLongResult = _ttoi64(sText);
482                        if(nIndex)
483                                nLongLongResult = -nLongLongResult;
484                }
485                #pragma endregion
486                const LONG nHighLongLongResult = (LONG) (nLongLongResult >> 32);
487                if(!nLongLongResult || nHighLongLongResult > 0 || nHighLongLongResult < -1)
488                        return FALSE;
489                HRESULT nResult = (LONG) nLongLongResult;
490                #pragma endregion
491                #pragma region Lookup
492                // NOTE: Include file regular expression replacement: ^.+?\#define +([^ ]+?) +MAKE_D3D.+\r?$ -> A($1)
493                CString sTitle, sMessage, sIdentifier;
494                if(IsQuartzResult(nResult, &sMessage))// || LookupQuartzIdentifier(nResult, sIdentifier))
495                {
496                        LookupQuartzIdentifier(nResult, sIdentifier);
497                        sTitle = _T("DirectShow");
498                } else if(IsWmResult(nResult, &sMessage))
499                {
500                        LookupWmIdentifier(nResult, sIdentifier);
501                        sTitle = _T("Windows Media");
502                } else if(IsMfResult(nResult, &sMessage) || LookupMfIdentifier(nResult, sIdentifier))
503                        sTitle = _T("Media Foundation");
504                ////////////////////////////////////////////////////
505                // NOTE: These are perhaps useless in Windows 7, but I am under impression they are helpful in earlier systems
506                else if(IsWs2Result(nResult, &sMessage))
507                        sTitle = _T("Sockets");
508                else if(IsWinHttpResult(nResult, &sMessage))
509                        sTitle = _T("WinHTTP");
510                else if(IsWinInetResult(nResult, &sMessage))
511                        sTitle = _T("WinInet");
512                ////////////////////////////////////////////////////
513                else if(IsD3dResult(nResult, &sMessage) || LookupD3dIdentifier(nResult, sIdentifier))
514                        sTitle = _T("DirectDraw/Direct3D");
515                else if(IsD2dResult(nResult, &sMessage) || LookupD2dIdentifier(nResult, sIdentifier))
516                        sTitle = _T("Direct2D");
517                else if(IsWicResult(nResult, &sMessage) || LookupWicIdentifier(nResult, sIdentifier))
518                        sTitle = _T("WinCodec");
519                else if(IsWiaResult(nResult, &sMessage) || LookupWiaIdentifier(nResult, sIdentifier))
520                        sTitle = _T("WIA");
521                else if(IsUrtResult(nResult, &sMessage) || LookupUrtIdentifier(nResult, sIdentifier))
522                        sTitle = _T(".NET");
523                else 
524                {
525                        sMessage = AtlFormatSystemMessage(nResult);
526                        if(!LookupSystemIdentifier(nResult, sIdentifier))
527                                 LookupHresultSystemIdentifier(nResult, sIdentifier);
528                        if(!sMessage.IsEmpty() || !sIdentifier.IsEmpty())
529                                sTitle = _T("System");
530                }
531                if(sMessage.IsEmpty() && sTitle.IsEmpty())
532                        return FALSE;
533                #pragma endregion
534                #pragma region Present
535                _A(!sTitle.IsEmpty());
536                m_sQuery = AtlFormatString(_T("0x%08X"), nResult) + _T(" ") + sIdentifier + _T(" ") + sMessage;
537                CRoArrayT<CString> TitleArray;
538                _W(TitleArray.Add(AtlFormatString(_T("0x%08X"), nResult)) >= 0);
539                if(!sIdentifier.IsEmpty())
540                        _W(TitleArray.Add(sIdentifier) >= 0);
541                if(!sTitle.IsEmpty())
542                        _W(TitleArray.Add(sTitle) >= 0);
543                if(sMessage.IsEmpty())
544                        sMessage = _T("(no message found)");
545                SetBalloonToolTip(NIIF_INFO, _StringHelper::Join(TitleArray, _T(" - ")), sMessage);
546                #pragma endregion
547                return TRUE;
548        }
549
550// Window message handelrs
551        LRESULT OnCreate(CREATESTRUCT*)
552        {
553                m_nCommonControlsVersion = GetCommonControlsVersion();
554                m_nShellVersion = GetShellVersion();
555                SetIcon(AtlLoadIconImage(IDI_MODULE, LR_DEFAULTCOLOR, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON)), TRUE);
556                SetIcon(AtlLoadIconImage(IDI_MODULE, LR_DEFAULTCOLOR, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON)), FALSE);
557                CIcon Icon = AtlLoadIconImage(IDI_MODULE, LR_DEFAULTCOLOR, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
558                CString sTip = AtlLoadString(IDS_PROJNAME);
559                ZeroMemory(&m_NotifyIconData, sizeof m_NotifyIconData);
560                m_NotifyIconData.hWnd = m_hWnd;
561                m_NotifyIconData.uID = 1;
562                m_NotifyIconData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP | NIF_STATE;
563                m_NotifyIconData.uCallbackMessage = WM_NOTIFYICON;
564                m_NotifyIconData.hIcon = Icon;
565                _tcsncpy_s(m_NotifyIconData.szTip, sTip,  _TRUNCATE);
566                if(m_nShellVersion >= 0x0005000000000000) // 5.0
567                {
568                        m_NotifyIconData.cbSize = sizeof m_NotifyIconData; 
569                        m_NotifyIconData.uFlags |= NIF_INFO;
570                        _tcsncpy_s(m_NotifyIconData.szInfoTitle, GetDefaultInfoTitle(), _TRUNCATE);
571                        m_NotifyIconData.dwInfoFlags = GetDefaultInfoFlags();
572                } else
573                        m_NotifyIconData.cbSize = NOTIFYICONDATA_V1_SIZE; 
574                _W(Shell_NotifyIcon(NIM_ADD, &m_NotifyIconData));
575                m_NotifyIconData.uFlags &= ~(NIF_ICON | NIF_STATE);
576                m_NextClipboardViewerWindow = SetClipboardViewer();
577                _pAtlModule->Lock();
578                #if defined(_DEBUG)
579                //Process(AtlFormatString(_T("%d"), 0x80040227)); // VFW_E_WRONG_STATE
580                //Process(_T("0xC00D36B9")); // MF_E_NO_MORE_TYPES)
581                //Process(AtlFormatString(_T("0x%x"), HRESULT_FROM_WIN32(WSAEADDRINUSE))); // WSAEADDRINUSE
582                //Process(AtlFormatString(_T("0x%x"), HRESULT_FROM_WIN32(ERROR_WINHTTP_AUTODETECTION_FAILED))); // ERROR_WINHTTP_AUTODETECTION_FAILED
583                //Process(_T("0x80290208L"));
584                Process(_T("10053"));
585                #endif // defined(_DEBUG)
586                return TRUE;
587        }
588        LRESULT OnDestroy() throw()
589        {
590                _pAtlModule->Unlock();
591                _W(ChangeClipboardChain(m_NextClipboardViewerWindow));
592                _W(Shell_NotifyIcon(NIM_DELETE, &m_NotifyIconData));
593                m_NotifyIconData.hWnd = NULL;
594                return 0;
595        }
596        LRESULT OnChangeCbChain(CWindow RemovedWindow, CWindow NextWindow)
597        {
598                if(m_NextClipboardViewerWindow == RemovedWindow)
599                        m_NextClipboardViewerWindow = NextWindow;
600                else if(m_NextClipboardViewerWindow)
601                        m_NextClipboardViewerWindow.SendMessage(WM_CHANGECBCHAIN, (WPARAM) (HWND) RemovedWindow, (LPARAM) (HWND) NextWindow);
602                return 0;
603        }
604        LRESULT OnDrawClipboard()
605        {
606                if(OpenClipboard()) 
607                {
608                        _ATLTRY
609                        {
610                                if(IsClipboardFormatAvailable(CF_UNICODETEXT))
611                                {
612                                        CGlobalMemoryWeakHandle DataHandle = (HGLOBAL) GetClipboardData(CF_UNICODETEXT);
613                                        __E(DataHandle);
614                                        CGlobalMemoryHandle::CLockT<WCHAR> pszData(DataHandle);
615                                        Process(CW2CT(pszData));
616                                }
617                        }
618                        _ATLCATCHALL()
619                        {
620                                _W(CloseClipboard());
621                                _ATLRETHROW;
622                        }
623                        _W(CloseClipboard());
624                }
625                return 0;
626        }
627        LRESULT OnMouseMove(UINT, CPoint)
628        {
629                return 0;
630        }
631        LRESULT OnLButtonDblClk(UINT, CPoint)
632        {
633                if(!IsWindowEnabled())
634                        return 0;
635                INT nDefaultCommandIdentifier = ID_APP_ABOUT;
636                //CWaitCursor WaitCursor;
637                if(nDefaultCommandIdentifier)
638                        _W(PostMessage(WM_COMMAND, nDefaultCommandIdentifier));
639                return 0;
640        }
641        LRESULT OnRButtonUp(UINT, CPoint)
642        {
643                CMenu ContainerMenu;
644                _W(ContainerMenu.LoadMenu(IDR_NOTIFYICON));
645                CMenuHandle Menu;
646                INT nDefaultCommandIdentifier = ID_APP_ABOUT;
647                Menu = ContainerMenu.GetSubMenu(0);
648                //CWaitCursor WaitCursor;
649                _A(Menu);
650                if(nDefaultCommandIdentifier)
651                        _W(Menu.SetMenuDefaultItem(nDefaultCommandIdentifier));
652                CPoint Position;
653                GetCursorPos(&Position);
654                _W(SetForegroundWindow(m_hWnd));
655                _W(Menu.TrackPopupMenu(TPM_LEFTALIGN | TPM_TOPALIGN | TPM_VERTICAL, Position.x, Position.y, m_hWnd));
656                return 0;
657        }
658        LRESULT OnNotifyIconBalloonUserClick(UINT, WPARAM, LPARAM)
659        {
660                if(m_sQuery.IsEmpty())
661                        return 0;
662                CWinHttpRequestIdentifier Identifier(_T("http://google.com/search"));
663                Identifier.AddSearchValue(_T("q"), m_sQuery);
664                CWaitCursor WaitCursor;
665                ShellExecute(m_hWnd, NULL, CW2CT(Identifier.GetValue()), NULL, NULL, SW_SHOWDEFAULT);
666                return 0;
667        }
668        LRESULT OnNotifyIcon(UINT uMsg, WPARAM wParam, LPARAM lParam) throw()
669        {
670                _A(wParam == m_NotifyIconData.uID);
671                return SendMessage((UINT) lParam);
672        }
673        LRESULT OnApplicationAbout(UINT, INT, HWND) throw()
674        {
675                CAboutDialog Dialog;
676                EnableWindow(FALSE);
677                Dialog.DoModal();
678                EnableWindow(TRUE);
679                return 0;
680        }
681        LRESULT OnApplicationExit(UINT = 0, INT = ID_APP_EXIT, HWND = NULL) throw()
682        {
683                PostQuitMessage(0);
684                return 0;
685        }
686};
Note: See TracBrowser for help on using the repository browser.