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

Last change on this file since 136 was 136, checked in by roman, 10 years ago

Added missing forwarding for WM_DRAWCLIPBOARD (thanks to David Ching)

  • Property svn:keywords set to Id
File size: 22.8 KB
Line 
1////////////////////////////////////////////////////////////
2// Copyright (C) Roman Ryltsov, 2008-2011
3// Created by Roman Ryltsov roman@alax.info
4//
5// $Id: NotifyIconWindow.h 136 2012-10-15 18:40:17Z 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                #pragma region Obsolete?
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                #pragma endregion
536                #pragma region Win32 Priority
537                else if(HRESULT_SEVERITY(nResult) == SEVERITY_ERROR && HRESULT_FACILITY(nResult) == FACILITY_WIN32 && (UINT) HRESULT_CODE(nResult) < 300)
538                {
539                        sMessage = AtlFormatSystemMessage(nResult);
540                        if(!LookupSystemIdentifier(nResult, sIdentifier))
541                                 LookupHresultSystemIdentifier(nResult, sIdentifier);
542                        if(!sMessage.IsEmpty() || !sIdentifier.IsEmpty())
543                                sTitle = _T("System");
544                }
545                #pragma endregion
546                else if(IsD3dResult(nResult, &sMessage, &sIdentifier) || LookupD3dIdentifier(nResult, sIdentifier))
547                        sTitle = _T("DirectDraw/Direct3D");
548                else if(IsD2dResult(nResult, &sMessage, &sIdentifier) || LookupD2dIdentifier(nResult, sIdentifier))
549                        sTitle = _T("Direct2D");
550                else if(IsWicResult(nResult, &sMessage, &sIdentifier) || LookupWicIdentifier(nResult, sIdentifier))
551                        sTitle = _T("WinCodec");
552                else if(IsWiaResult(nResult, &sMessage, &sIdentifier) || LookupWiaIdentifier(nResult, sIdentifier))
553                        sTitle = _T("WIA");
554                else if(IsUrtResult(nResult, &sMessage, &sIdentifier) || LookupUrtIdentifier(nResult, sIdentifier))
555                        sTitle = _T(".NET");
556                else 
557                {
558                        sMessage = AtlFormatSystemMessage(nResult);
559                        if(!LookupSystemIdentifier(nResult, sIdentifier))
560                                 LookupHresultSystemIdentifier(nResult, sIdentifier);
561                        if(!sMessage.IsEmpty() || !sIdentifier.IsEmpty())
562                                sTitle = _T("System");
563                }
564                if(sMessage.IsEmpty() && sTitle.IsEmpty())
565                        return FALSE;
566                #pragma endregion
567                #pragma region Present
568                _A(!sTitle.IsEmpty());
569                m_sQuery = AtlFormatString(_T("0x%08X"), nResult) + _T(" ") + sIdentifier + _T(" ") + sMessage;
570                CRoArrayT<CString> TitleArray;
571                _W(TitleArray.Add(AtlFormatString(_T("0x%08X"), nResult)) >= 0);
572                if(!sIdentifier.IsEmpty())
573                        _W(TitleArray.Add(sIdentifier) >= 0);
574                if(!sTitle.IsEmpty())
575                        _W(TitleArray.Add(sTitle) >= 0);
576                if(sMessage.IsEmpty())
577                        sMessage = _T("(no message found)");
578                SetBalloonToolTip(NIIF_INFO, _StringHelper::Join(TitleArray, _T(" - ")), sMessage);
579                #pragma endregion
580                return TRUE;
581        }
582
583// Window message handelrs
584        LRESULT OnCreate(CREATESTRUCT*)
585        {
586                m_nCommonControlsVersion = GetCommonControlsVersion();
587                m_nShellVersion = GetShellVersion();
588                SetIcon(AtlLoadIconImage(IDI_MODULE, LR_DEFAULTCOLOR, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON)), TRUE);
589                SetIcon(AtlLoadIconImage(IDI_MODULE, LR_DEFAULTCOLOR, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON)), FALSE);
590                CIcon Icon = AtlLoadIconImage(IDI_MODULE, LR_DEFAULTCOLOR, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
591                CString sTip = AtlLoadString(IDS_PROJNAME);
592                ZeroMemory(&m_NotifyIconData, sizeof m_NotifyIconData);
593                m_NotifyIconData.hWnd = m_hWnd;
594                m_NotifyIconData.uID = 1;
595                m_NotifyIconData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP | NIF_STATE;
596                m_NotifyIconData.uCallbackMessage = WM_NOTIFYICON;
597                m_NotifyIconData.hIcon = Icon;
598                _tcsncpy_s(m_NotifyIconData.szTip, sTip,  _TRUNCATE);
599                if(m_nShellVersion >= 0x0005000000000000) // 5.0
600                {
601                        m_NotifyIconData.cbSize = sizeof m_NotifyIconData; 
602                        m_NotifyIconData.uFlags |= NIF_INFO;
603                        _tcsncpy_s(m_NotifyIconData.szInfoTitle, GetDefaultInfoTitle(), _TRUNCATE);
604                        m_NotifyIconData.dwInfoFlags = GetDefaultInfoFlags();
605                } else
606                        m_NotifyIconData.cbSize = NOTIFYICONDATA_V1_SIZE; 
607                _W(Shell_NotifyIcon(NIM_ADD, &m_NotifyIconData));
608                m_NotifyIconData.uFlags &= ~(NIF_ICON | NIF_STATE);
609                m_NextClipboardViewerWindow = SetClipboardViewer();
610                _pAtlModule->Lock();
611                #if defined(_DEBUG)
612                //Process(AtlFormatString(_T("%d"), 0x80040227)); // VFW_E_WRONG_STATE
613                //Process(_T("0xC00D36B9")); // MF_E_NO_MORE_TYPES)
614                //Process(AtlFormatString(_T("0x%x"), HRESULT_FROM_WIN32(WSAEADDRINUSE))); // WSAEADDRINUSE
615                //Process(AtlFormatString(_T("0x%x"), HRESULT_FROM_WIN32(ERROR_WINHTTP_AUTODETECTION_FAILED))); // ERROR_WINHTTP_AUTODETECTION_FAILED
616                Process(_T("0x80131029L"));
617                //Process(_T("-2147312566"));
618                #endif // defined(_DEBUG)
619                return TRUE;
620        }
621        LRESULT OnDestroy() throw()
622        {
623                _pAtlModule->Unlock();
624                _W(ChangeClipboardChain(m_NextClipboardViewerWindow));
625                _W(Shell_NotifyIcon(NIM_DELETE, &m_NotifyIconData));
626                m_NotifyIconData.hWnd = NULL;
627                // NOTE: Safety double-posting
628                PostQuitMessage(0);
629                return 0;
630        }
631        LRESULT OnChangeCbChain(CWindow RemovedWindow, CWindow NextWindow)
632        {
633                if(m_NextClipboardViewerWindow == RemovedWindow)
634                        m_NextClipboardViewerWindow = NextWindow;
635                else if(m_NextClipboardViewerWindow)
636                        m_NextClipboardViewerWindow.SendMessage(WM_CHANGECBCHAIN, (WPARAM) (HWND) RemovedWindow, (LPARAM) (HWND) NextWindow);
637                return 0;
638        }
639        LRESULT OnDrawClipboard()
640        {
641                if(OpenClipboard()) 
642                {
643                        _ATLTRY
644                        {
645                                if(IsClipboardFormatAvailable(CF_UNICODETEXT))
646                                {
647                                        CGlobalMemoryWeakHandle DataHandle = (HGLOBAL) GetClipboardData(CF_UNICODETEXT);
648                                        __E(DataHandle);
649                                        CGlobalMemoryHandle::CLockT<WCHAR> pszData(DataHandle);
650                                        Process(CW2CT(pszData));
651                                }
652                        }
653                        _ATLCATCHALL()
654                        {
655                                _W(CloseClipboard());
656                                _ATLRETHROW;
657                        }
658                        _W(CloseClipboard());
659                }
660                if(m_NextClipboardViewerWindow)
661                        m_NextClipboardViewerWindow.SendMessage(WM_DRAWCLIPBOARD, m_pCurrentMsg->wParam, m_pCurrentMsg->lParam);
662                return 0;
663        }
664        LRESULT OnMouseMove(UINT, CPoint)
665        {
666                return 0;
667        }
668        LRESULT OnLButtonDblClk(UINT, CPoint)
669        {
670                if(!IsWindowEnabled())
671                        return 0;
672                INT nDefaultCommandIdentifier = ID_APP_ABOUT;
673                //CWaitCursor WaitCursor;
674                if(nDefaultCommandIdentifier)
675                        _W(PostMessage(WM_COMMAND, nDefaultCommandIdentifier));
676                return 0;
677        }
678        LRESULT OnRButtonUp(UINT, CPoint)
679        {
680                CMenu ContainerMenu;
681                _W(ContainerMenu.LoadMenu(IDR_NOTIFYICON));
682                CMenuHandle Menu;
683                INT nDefaultCommandIdentifier = ID_APP_ABOUT;
684                Menu = ContainerMenu.GetSubMenu(0);
685                //CWaitCursor WaitCursor;
686                _A(Menu);
687                if(nDefaultCommandIdentifier)
688                        _W(Menu.SetMenuDefaultItem(nDefaultCommandIdentifier));
689                CPoint Position;
690                GetCursorPos(&Position);
691                _W(SetForegroundWindow(m_hWnd));
692                _W(Menu.TrackPopupMenu(TPM_LEFTALIGN | TPM_TOPALIGN | TPM_VERTICAL, Position.x, Position.y, m_hWnd));
693                return 0;
694        }
695        LRESULT OnNotifyIconBalloonUserClick(UINT, WPARAM, LPARAM)
696        {
697                if(m_sQuery.IsEmpty())
698                        return 0;
699                CWinHttpRequestIdentifier Identifier(_T("http://google.com/search"));
700                Identifier.AddSearchValue(_T("q"), m_sQuery);
701                CWaitCursor WaitCursor;
702                ShellExecute(m_hWnd, NULL, CW2CT(Identifier.GetValue()), NULL, NULL, SW_SHOWDEFAULT);
703                return 0;
704        }
705        LRESULT OnNotifyIcon(UINT uMsg, WPARAM wParam, LPARAM lParam) throw()
706        {
707                _A(wParam == m_NotifyIconData.uID);
708                return SendMessage((UINT) lParam);
709        }
710        LRESULT OnApplicationAbout(UINT, INT, HWND) throw()
711        {
712                CAboutDialog Dialog;
713                EnableWindow(FALSE);
714                Dialog.DoModal();
715                EnableWindow(TRUE);
716                return 0;
717        }
718        LRESULT OnApplicationExit(UINT = 0, INT = ID_APP_EXIT, HWND = NULL) throw()
719        {
720                PostQuitMessage(0);
721                return 0;
722        }
723};
Note: See TracBrowser for help on using the repository browser.