1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include "stdafx.h"
#include "shell_icon_mgr.h"
#include "tier1/strtools.h"
#include "tier0/dbg.h"
#include "shellapi.h"
#define SHELLICONMGR_CLASSNAME "VMPI_ShellIconMgr"
CShellIconMgr::CShellIconMgr()
{
m_hWnd = NULL;
m_hWndClass = NULL;
m_uTaskbarRestart = (UINT)-1;
m_iCurIconResourceID = 0;
m_pHelper = NULL;
}
CShellIconMgr::~CShellIconMgr()
{
Term();
}
bool CShellIconMgr::Init(
IShellIconMgrHelper *pHelper,
const char *pToolTip,
int iCallbackMessage,
int iIconResourceID )
{
Term();
m_pHelper = pHelper;
m_iCallbackMessage = iCallbackMessage;
m_pToolTip = pToolTip;
// Create the window class.
WNDCLASS wndclass;
memset( &wndclass, 0, sizeof( wndclass ) );
wndclass.lpfnWndProc = &CShellIconMgr::StaticWindowProc;
wndclass.hInstance = (HINSTANCE)pHelper->GetHInstance();
wndclass.hIcon = NULL;
wndclass.hCursor = LoadCursor( NULL, IDC_ARROW );
wndclass.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = SHELLICONMGR_CLASSNAME;
m_hWndClass = RegisterClass( &wndclass );
if ( !m_hWndClass )
{
Assert( false );
Warning( "RegisterClass failed.\n" );
Term();
return false;
}
// Create the window.
m_hWnd = CreateWindow(
SHELLICONMGR_CLASSNAME,
SHELLICONMGR_CLASSNAME,
WS_DISABLED, // we only want shell notify messages
0, 0, 0, 0, // position
NULL, // parent
NULL, // menu
pHelper->GetHInstance(), // hInstance
this // lpParam
);
if ( !m_hWnd )
{
Assert( false );
Warning( "CreateWindow failed.\n" );
Term();
return false;
}
m_uTaskbarRestart = RegisterWindowMessage( TEXT( "TaskbarCreated" ) );
SetWindowLong( m_hWnd, GWL_USERDATA, (LONG)this );
UpdateWindow( m_hWnd );
// Don't handle errors here because the taskbar may not be created yet if we are a service.
m_iCurIconResourceID = iIconResourceID;
CreateTrayIcon();
return true;
}
void CShellIconMgr::Term()
{
if ( m_hWndClass )
{
UnregisterClass( SHELLICONMGR_CLASSNAME, m_pHelper->GetHInstance() );
m_hWndClass = NULL;
}
if ( m_hWnd )
{
// Remove the shell icon if there is one.
NOTIFYICONDATA data;
memset( &data, 0, sizeof( data ) );
data.cbSize = sizeof( data );
data.hWnd = m_hWnd;
Shell_NotifyIcon( NIM_DELETE, &data );
DestroyWindow( m_hWnd );
m_hWnd = NULL;
}
}
void CShellIconMgr::ChangeIcon( int iIconResourceID )
{
NOTIFYICONDATA data;
memset( &data, 0, sizeof( data ) );
data.cbSize = sizeof( data );
data.uFlags = NIF_ICON;
data.hWnd = m_hWnd;
data.hIcon = LoadIcon( m_pHelper->GetHInstance(), MAKEINTRESOURCE( iIconResourceID ) );
Shell_NotifyIcon( NIM_MODIFY, &data );
m_iCurIconResourceID = iIconResourceID;
}
void CShellIconMgr::CreateTrayIcon()
{
// Now create the shell icon.
NOTIFYICONDATA data;
memset( &data, 0, sizeof( data ) );
data.cbSize = sizeof( data );
data.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE;
data.uCallbackMessage = m_iCallbackMessage;
data.hWnd = m_hWnd;
data.hIcon = LoadIcon( m_pHelper->GetHInstance(), MAKEINTRESOURCE( m_iCurIconResourceID ) );
Q_strncpy( data.szTip, m_pToolTip, sizeof( data.szTip ) );
Shell_NotifyIcon( NIM_ADD, &data );
}
LRESULT CShellIconMgr::WindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
if ( hWnd != m_hWnd )
{
return DefWindowProc( hWnd, uMsg, wParam, lParam );
}
if ( uMsg == m_uTaskbarRestart )
{
CreateTrayIcon();
return 0;
}
return m_pHelper->WindowProc( hWnd, uMsg, wParam, lParam );
}
LRESULT CShellIconMgr::StaticWindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
CShellIconMgr *pMgr = (CShellIconMgr*)GetWindowLong( hwnd, GWL_USERDATA );
if ( pMgr )
{
return pMgr->WindowProc( hwnd, uMsg, wParam, lParam );
}
else
{
return DefWindowProc( hwnd, uMsg, wParam, lParam );
}
}
|