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

Last change on this file since 129 was 129, checked in by roman, 11 years ago
  • Property svn:keywords set to Id
File size: 22.2 KB
Line 
1////////////////////////////////////////////////////////////
2// Copyright (C) Roman Ryltsov, 2008-2011
3// Created by Roman Ryltsov roman@alax.info
4//
5// $Id: NotifyIconWindow.h 129 2012-10-04 13:00:02Z 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, CString* psIdentifier = NULL)
244        {
245                if(HRESULT_FACILITY(nResult) != _FACD3D)
246                        return FALSE;
247                psMessage;
248                CString sIdentifier;
249                if(!LookupD3dIdentifier(nResult, sIdentifier))
250                        return FALSE;
251                if(psIdentifier)
252                        *psIdentifier = sIdentifier;
253                return TRUE;
254        }
255        static BOOL LookupD3dIdentifier(HRESULT nValue, CString& sIdentifier)
256        {
257                static const struct { HRESULT nValue; LPCSTR pszName; } g_pMap[] = 
258                {
259                        #define A(x) { x, #x },
260                        #include "D3dIdentifier.inc"
261                        #undef A
262                };
263                for(SIZE_T nIndex = 0; nIndex < DIM(g_pMap); nIndex++)
264                        if(g_pMap[nIndex].nValue == nValue)
265                        {
266                                sIdentifier = CString(g_pMap[nIndex].pszName);
267                                return TRUE;
268                        }
269                return FALSE;
270        }
271        static BOOL IsD2dResult(HRESULT nResult, CString* psMessage = NULL, CString* psIdentifier = NULL)
272        {
273                if(HRESULT_FACILITY(nResult) != FACILITY_D2D)
274                        return FALSE;
275                psMessage;
276                CString sIdentifier;
277                if(!LookupD2dIdentifier(nResult, sIdentifier))
278                        return FALSE;
279                if(psIdentifier)
280                        *psIdentifier = sIdentifier;
281                return TRUE;
282        }
283        static BOOL LookupD2dIdentifier(HRESULT nValue, CString& sIdentifier)
284        {
285                static const struct { HRESULT nValue; LPCSTR pszName; } g_pMap[] = 
286                {
287                        #define A(x) { x, #x },
288                        #include "D2dIdentifier.inc"
289                        #undef A
290                };
291                for(SIZE_T nIndex = 0; nIndex < DIM(g_pMap); nIndex++)
292                        if(g_pMap[nIndex].nValue == nValue)
293                        {
294                                sIdentifier = CString(g_pMap[nIndex].pszName);
295                                return TRUE;
296                        }
297                return FALSE;
298        }
299        static BOOL IsWicResult(HRESULT nResult, CString* psMessage = NULL, CString* psIdentifier = NULL)
300        {
301                if(HRESULT_FACILITY(nResult) != FACILITY_WINCODEC_ERR)
302                        return FALSE;
303                psMessage;
304                CString sIdentifier;
305                if(!LookupWicIdentifier(nResult, sIdentifier))
306                        return FALSE;
307                if(psIdentifier)
308                        *psIdentifier = sIdentifier;
309                return TRUE;
310        }
311        static BOOL LookupWicIdentifier(HRESULT nValue, CString& sIdentifier)
312        {
313                static const struct { HRESULT nValue; LPCSTR pszName; } g_pMap[] = 
314                {
315                        #define A(x) { x, #x },
316                        #include "WicIdentifier.inc"
317                        #undef A
318                };
319                for(SIZE_T nIndex = 0; nIndex < DIM(g_pMap); nIndex++)
320                        if(g_pMap[nIndex].nValue == nValue)
321                        {
322                                sIdentifier = CString(g_pMap[nIndex].pszName);
323                                return TRUE;
324                        }
325                return FALSE;
326        }
327        static BOOL IsWiaResult(HRESULT nResult, CString* psMessage = NULL, CString* psIdentifier = NULL)
328        {
329                if(HRESULT_FACILITY(nResult) != FACILITY_WIA)
330                        return FALSE;
331                psMessage;
332                CString sIdentifier;
333                if(!LookupWiaIdentifier(nResult, sIdentifier))
334                        return FALSE;
335                if(psIdentifier)
336                        *psIdentifier = sIdentifier;
337                return TRUE;
338        }
339        static BOOL LookupWiaIdentifier(HRESULT nValue, CString& sIdentifier)
340        {
341                static const struct { HRESULT nValue; LPCSTR pszName; } g_pMap[] = 
342                {
343                        #define A(x) { x, #x },
344                        #include "WiaIdentifier.inc"
345                        #undef A
346                };
347                for(SIZE_T nIndex = 0; nIndex < DIM(g_pMap); nIndex++)
348                        if(g_pMap[nIndex].nValue == nValue)
349                        {
350                                sIdentifier = CString(g_pMap[nIndex].pszName);
351                                return TRUE;
352                        }
353                return FALSE;
354        }
355        static BOOL IsUrtResult(HRESULT nResult, CString* psMessage = NULL, CString* psIdentifier = NULL)
356        {
357                if(HRESULT_FACILITY(nResult) != FACILITY_URT)
358                        return FALSE;
359                psMessage;
360                CString sIdentifier;
361                if(!LookupUrtIdentifier(nResult, sIdentifier))
362                        return FALSE;
363                if(psIdentifier)
364                        *psIdentifier = sIdentifier;
365                return TRUE;
366        }
367        static BOOL LookupUrtIdentifier(HRESULT nValue, CString& sIdentifier)
368        {
369                static const struct { HRESULT nValue; LPCSTR pszName; } g_pMap[] = 
370                {
371                        #define A(x) { x, #x },
372                        #include "UrtIdentifier.inc"
373                        #undef A
374                };
375                for(SIZE_T nIndex = 0; nIndex < DIM(g_pMap); nIndex++)
376                        if(g_pMap[nIndex].nValue == nValue)
377                        {
378                                sIdentifier = CString(g_pMap[nIndex].pszName);
379                                return TRUE;
380                        }
381                return FALSE;
382        }
383
384public:
385// CNotifyIconWindow
386        static ULONGLONG GetCommonControlsVersion() throw()
387        {
388                DWORD nMajorVersion, nMinorVersion;
389                _W(SUCCEEDED(AtlGetCommCtrlVersion(&nMajorVersion, &nMinorVersion)));
390                return (ULONGLONG) ((nMajorVersion << 16) + nMinorVersion) << 32;
391        }
392        static ULONGLONG GetShellVersion() throw()
393        {
394                DWORD nMajorVersion, nMinorVersion;
395                _W(SUCCEEDED(AtlGetShellVersion(&nMajorVersion, &nMinorVersion)));
396                return (ULONGLONG) ((nMajorVersion << 16) + nMinorVersion) << 32;
397        }
398        CNotifyIconWindow()
399        {
400        }
401        HWND Create()
402        {
403                return __super::Create(NULL, rcDefault, _T("AlaxInfo.ShowHresult.NotifyIconWindow"));
404        }
405        static CString GetDefaultInfoTitle()
406        {
407                return AtlLoadString(IDS_NOTIFYICON_DEFAULTTIPTITLE);
408        }
409        static DWORD GetDefaultInfoFlags() throw()
410        {
411                return NIIF_NONE;
412        }
413        VOID SetBalloonToolTip(DWORD nFlags, LPCTSTR pszTitle, LPCTSTR pszText, UINT nTimeout = 30 * 1000)
414        {
415                if(IsWindow() && m_NotifyIconData.uFlags & NIF_INFO)
416                {
417                        m_NotifyIconData.uTimeout = nTimeout;
418                        m_NotifyIconData.dwInfoFlags = nFlags;
419                        _tcsncpy_s(m_NotifyIconData.szInfoTitle, pszTitle, _TRUNCATE);
420                        _tcsncpy_s(m_NotifyIconData.szInfo, pszText, _TRUNCATE);
421                        _W(Shell_NotifyIcon(NIM_MODIFY, &m_NotifyIconData));
422                }
423        }
424        static BOOL IsDecimal(TCHAR nCharacter) throw()
425        {
426                return _tcschr(_T("0123456789"), nCharacter) != NULL;
427        }
428        static BOOL IsDecimal(const TCHAR* pszCharacters, SIZE_T nCount) throw()
429        {
430                for(SIZE_T nIndex = 0; nIndex < nCount; nIndex++)
431                        if(!IsDecimal(pszCharacters[nIndex]))
432                                return FALSE;
433                return TRUE;
434        }
435        static BOOL IsDecimal(const TCHAR* pszCharacters) throw()
436        {
437                return IsDecimal(pszCharacters, _tcslen(pszCharacters));
438        }
439        static BOOL IsHexadecimal(TCHAR nCharacter) throw()
440        {
441                return _tcschr(_T("0123456789ABCDEFabcdef"), nCharacter) != NULL;
442        }
443        static BOOL IsHexadecimal(const TCHAR* pszCharacters, SIZE_T nCount) throw()
444        {
445                for(SIZE_T nIndex = 0; nIndex < nCount; nIndex++)
446                        if(!IsHexadecimal(pszCharacters[nIndex]))
447                                return FALSE;
448                return TRUE;
449        }
450        static BOOL IsHexadecimal(const TCHAR* pszCharacters) throw()
451        {
452                return IsHexadecimal(pszCharacters, _tcslen(pszCharacters));
453        }
454        BOOL Process(LPCTSTR pszText)
455        {
456                #pragma region Parse
457                if(_tcslen(pszText) > 24)
458                        return FALSE;
459                CString sText = pszText;
460                sText.Trim();
461                #pragma region Delete Trailing L (as in 0x00000000L)
462                if(!sText.IsEmpty() && _tcschr(_T("Ll"), sText[sText.GetLength() - 1]))
463                        sText.Delete(sText.GetLength() - 1);
464                #pragma endregion
465                #pragma region Unquote Single Quote
466                if(sText.GetLength() > 2 && sText[0] == _T('\'') && sText[sText.GetLength() - 1] == _T('\''))
467                {
468                        sText = sText.Mid(1, sText.GetLength() - 2);
469                        sText.Trim();
470                }
471                #pragma endregion
472                if(sText.IsEmpty())
473                        return FALSE;
474                LONGLONG nLongLongResult;
475                #pragma region Hexadecimal
476                if(_tcsnicmp(sText, _T("0x"), 2) == 0)
477                {
478                        if(!IsHexadecimal((LPCTSTR) sText + 2))
479                                return FALSE;
480                        if(!StrToInt64Ex(sText, STIF_SUPPORT_HEX, &nLongLongResult))
481                                return FALSE; 
482                } else
483                #pragma endregion
484                #pragma region Integer
485                {
486                        SIZE_T nIndex = 0;
487                        if(sText[0] == _T('-'))
488                                nIndex++;
489                        if(!IsDecimal((LPCTSTR) sText + nIndex))
490                        {
491                                #pragma region Eight Characater Long Hexadecimal without Prefix
492                                if(sText.GetLength() == nIndex + 8 && IsHexadecimal((LPCTSTR) sText + nIndex))
493                                {
494                                        sText.Insert(0, _T("0x"));
495                                        if(!StrToInt64Ex(sText, STIF_SUPPORT_HEX, &nLongLongResult))
496                                                return FALSE; 
497                                } else
498                                #pragma endregion
499                                        return FALSE;
500                        } else
501                                nLongLongResult = _ttoi64((LPCTSTR) sText + nIndex);
502                        if(nIndex)
503                                nLongLongResult = -nLongLongResult;
504                }
505                #pragma endregion
506                const LONG nHighLongLongResult = (LONG) (nLongLongResult >> 32);
507                if(!nLongLongResult || nHighLongLongResult > 0 || nHighLongLongResult < -1)
508                        return FALSE;
509                HRESULT nResult = (LONG) nLongLongResult;
510                #pragma endregion
511                #pragma region Lookup
512                // NOTE: Include file regular expression replacement: ^.+?\#define +([^ ]+?) +MAKE_D3D.+\r?$ -> A($1)
513                CString sTitle, sMessage, sIdentifier;
514                if(IsQuartzResult(nResult, &sMessage))// || LookupQuartzIdentifier(nResult, sIdentifier))
515                {
516                        LookupQuartzIdentifier(nResult, sIdentifier);
517                        sTitle = _T("DirectShow");
518                } else if(IsWmResult(nResult, &sMessage))
519                {
520                        LookupWmIdentifier(nResult, sIdentifier);
521                        sTitle = _T("Windows Media");
522                } else if(IsMfResult(nResult, &sMessage))
523                {
524                        LookupMfIdentifier(nResult, sIdentifier);
525                        sTitle = _T("Media Foundation");
526                }
527                ////////////////////////////////////////////////////
528                // NOTE: These are perhaps useless in Windows 7, but I am under impression they are helpful in earlier systems
529                else if(IsWs2Result(nResult, &sMessage))
530                        sTitle = _T("Sockets");
531                else if(IsWinHttpResult(nResult, &sMessage))
532                        sTitle = _T("WinHTTP");
533                else if(IsWinInetResult(nResult, &sMessage))
534                        sTitle = _T("WinInet");
535                ////////////////////////////////////////////////////
536                else if(IsD3dResult(nResult, &sMessage, &sIdentifier) || LookupD3dIdentifier(nResult, sIdentifier))
537                        sTitle = _T("DirectDraw/Direct3D");
538                else if(IsD2dResult(nResult, &sMessage, &sIdentifier) || LookupD2dIdentifier(nResult, sIdentifier))
539                        sTitle = _T("Direct2D");
540                else if(IsWicResult(nResult, &sMessage, &sIdentifier) || LookupWicIdentifier(nResult, sIdentifier))
541                        sTitle = _T("WinCodec");
542                else if(IsWiaResult(nResult, &sMessage, &sIdentifier) || LookupWiaIdentifier(nResult, sIdentifier))
543                        sTitle = _T("WIA");
544                else if(IsUrtResult(nResult, &sMessage, &sIdentifier) || LookupUrtIdentifier(nResult, sIdentifier))
545                        sTitle = _T(".NET");
546                else 
547                {
548                        sMessage = AtlFormatSystemMessage(nResult);
549                        if(!LookupSystemIdentifier(nResult, sIdentifier))
550                                 LookupHresultSystemIdentifier(nResult, sIdentifier);
551                        if(!sMessage.IsEmpty() || !sIdentifier.IsEmpty())
552                                sTitle = _T("System");
553                }
554                if(sMessage.IsEmpty() && sTitle.IsEmpty())
555                        return FALSE;
556                #pragma endregion
557                #pragma region Present
558                _A(!sTitle.IsEmpty());
559                m_sQuery = AtlFormatString(_T("0x%08X"), nResult) + _T(" ") + sIdentifier + _T(" ") + sMessage;
560                CRoArrayT<CString> TitleArray;
561                _W(TitleArray.Add(AtlFormatString(_T("0x%08X"), nResult)) >= 0);
562                if(!sIdentifier.IsEmpty())
563                        _W(TitleArray.Add(sIdentifier) >= 0);
564                if(!sTitle.IsEmpty())
565                        _W(TitleArray.Add(sTitle) >= 0);
566                if(sMessage.IsEmpty())
567                        sMessage = _T("(no message found)");
568                SetBalloonToolTip(NIIF_INFO, _StringHelper::Join(TitleArray, _T(" - ")), sMessage);
569                #pragma endregion
570                return TRUE;
571        }
572
573// Window message handelrs
574        LRESULT OnCreate(CREATESTRUCT*)
575        {
576                m_nCommonControlsVersion = GetCommonControlsVersion();
577                m_nShellVersion = GetShellVersion();
578                SetIcon(AtlLoadIconImage(IDI_MODULE, LR_DEFAULTCOLOR, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON)), TRUE);
579                SetIcon(AtlLoadIconImage(IDI_MODULE, LR_DEFAULTCOLOR, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON)), FALSE);
580                CIcon Icon = AtlLoadIconImage(IDI_MODULE, LR_DEFAULTCOLOR, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
581                CString sTip = AtlLoadString(IDS_PROJNAME);
582                ZeroMemory(&m_NotifyIconData, sizeof m_NotifyIconData);
583                m_NotifyIconData.hWnd = m_hWnd;
584                m_NotifyIconData.uID = 1;
585                m_NotifyIconData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP | NIF_STATE;
586                m_NotifyIconData.uCallbackMessage = WM_NOTIFYICON;
587                m_NotifyIconData.hIcon = Icon;
588                _tcsncpy_s(m_NotifyIconData.szTip, sTip,  _TRUNCATE);
589                if(m_nShellVersion >= 0x0005000000000000) // 5.0
590                {
591                        m_NotifyIconData.cbSize = sizeof m_NotifyIconData; 
592                        m_NotifyIconData.uFlags |= NIF_INFO;
593                        _tcsncpy_s(m_NotifyIconData.szInfoTitle, GetDefaultInfoTitle(), _TRUNCATE);
594                        m_NotifyIconData.dwInfoFlags = GetDefaultInfoFlags();
595                } else
596                        m_NotifyIconData.cbSize = NOTIFYICONDATA_V1_SIZE; 
597                _W(Shell_NotifyIcon(NIM_ADD, &m_NotifyIconData));
598                m_NotifyIconData.uFlags &= ~(NIF_ICON | NIF_STATE);
599                m_NextClipboardViewerWindow = SetClipboardViewer();
600                _pAtlModule->Lock();
601                #if defined(_DEBUG)
602                //Process(AtlFormatString(_T("%d"), 0x80040227)); // VFW_E_WRONG_STATE
603                //Process(_T("0xC00D36B9")); // MF_E_NO_MORE_TYPES)
604                //Process(AtlFormatString(_T("0x%x"), HRESULT_FROM_WIN32(WSAEADDRINUSE))); // WSAEADDRINUSE
605                //Process(AtlFormatString(_T("0x%x"), HRESULT_FROM_WIN32(ERROR_WINHTTP_AUTODETECTION_FAILED))); // ERROR_WINHTTP_AUTODETECTION_FAILED
606                Process(_T("0x80131029L"));
607                //Process(_T("-2147312566"));
608                #endif // defined(_DEBUG)
609                return TRUE;
610        }
611        LRESULT OnDestroy() throw()
612        {
613                _pAtlModule->Unlock();
614                _W(ChangeClipboardChain(m_NextClipboardViewerWindow));
615                _W(Shell_NotifyIcon(NIM_DELETE, &m_NotifyIconData));
616                m_NotifyIconData.hWnd = NULL;
617                return 0;
618        }
619        LRESULT OnChangeCbChain(CWindow RemovedWindow, CWindow NextWindow)
620        {
621                if(m_NextClipboardViewerWindow == RemovedWindow)
622                        m_NextClipboardViewerWindow = NextWindow;
623                else if(m_NextClipboardViewerWindow)
624                        m_NextClipboardViewerWindow.SendMessage(WM_CHANGECBCHAIN, (WPARAM) (HWND) RemovedWindow, (LPARAM) (HWND) NextWindow);
625                return 0;
626        }
627        LRESULT OnDrawClipboard()
628        {
629                if(OpenClipboard()) 
630                {
631                        _ATLTRY
632                        {
633                                if(IsClipboardFormatAvailable(CF_UNICODETEXT))
634                                {
635                                        CGlobalMemoryWeakHandle DataHandle = (HGLOBAL) GetClipboardData(CF_UNICODETEXT);
636                                        __E(DataHandle);
637                                        CGlobalMemoryHandle::CLockT<WCHAR> pszData(DataHandle);
638                                        Process(CW2CT(pszData));
639                                }
640                        }
641                        _ATLCATCHALL()
642                        {
643                                _W(CloseClipboard());
644                                _ATLRETHROW;
645                        }
646                        _W(CloseClipboard());
647                }
648                return 0;
649        }
650        LRESULT OnMouseMove(UINT, CPoint)
651        {
652                return 0;
653        }
654        LRESULT OnLButtonDblClk(UINT, CPoint)
655        {
656                if(!IsWindowEnabled())
657                        return 0;
658                INT nDefaultCommandIdentifier = ID_APP_ABOUT;
659                //CWaitCursor WaitCursor;
660                if(nDefaultCommandIdentifier)
661                        _W(PostMessage(WM_COMMAND, nDefaultCommandIdentifier));
662                return 0;
663        }
664        LRESULT OnRButtonUp(UINT, CPoint)
665        {
666                CMenu ContainerMenu;
667                _W(ContainerMenu.LoadMenu(IDR_NOTIFYICON));
668                CMenuHandle Menu;
669                INT nDefaultCommandIdentifier = ID_APP_ABOUT;
670                Menu = ContainerMenu.GetSubMenu(0);
671                //CWaitCursor WaitCursor;
672                _A(Menu);
673                if(nDefaultCommandIdentifier)
674                        _W(Menu.SetMenuDefaultItem(nDefaultCommandIdentifier));
675                CPoint Position;
676                GetCursorPos(&Position);
677                _W(SetForegroundWindow(m_hWnd));
678                _W(Menu.TrackPopupMenu(TPM_LEFTALIGN | TPM_TOPALIGN | TPM_VERTICAL, Position.x, Position.y, m_hWnd));
679                return 0;
680        }
681        LRESULT OnNotifyIconBalloonUserClick(UINT, WPARAM, LPARAM)
682        {
683                if(m_sQuery.IsEmpty())
684                        return 0;
685                CWinHttpRequestIdentifier Identifier(_T("http://google.com/search"));
686                Identifier.AddSearchValue(_T("q"), m_sQuery);
687                CWaitCursor WaitCursor;
688                ShellExecute(m_hWnd, NULL, CW2CT(Identifier.GetValue()), NULL, NULL, SW_SHOWDEFAULT);
689                return 0;
690        }
691        LRESULT OnNotifyIcon(UINT uMsg, WPARAM wParam, LPARAM lParam) throw()
692        {
693                _A(wParam == m_NotifyIconData.uID);
694                return SendMessage((UINT) lParam);
695        }
696        LRESULT OnApplicationAbout(UINT, INT, HWND) throw()
697        {
698                CAboutDialog Dialog;
699                EnableWindow(FALSE);
700                Dialog.DoModal();
701                EnableWindow(TRUE);
702                return 0;
703        }
704        LRESULT OnApplicationExit(UINT = 0, INT = ID_APP_EXIT, HWND = NULL) throw()
705        {
706                PostQuitMessage(0);
707                return 0;
708        }
709};
Note: See TracBrowser for help on using the repository browser.