/
tasktrayicon.cpp
executable file
·147 lines (124 loc) · 2.58 KB
/
tasktrayicon.cpp
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
#include <windows.h>
#include "tasktrayicon.h"
CTaskTrayIcon::CTaskTrayIcon( HWND hwnd, UINT id, UINT msg, HICON icon, LPCTSTR tip )
{
m_restartMsg = 0;
ZeroMemory( &m_data, sizeof( NOTIFYICONDATA ) );
m_data.cbSize = sizeof( NOTIFYICONDATA );
m_show = FALSE;
if( hwnd != NULL )
{
Create( hwnd, id, msg, icon, tip );
Add();
}
}
CTaskTrayIcon::~CTaskTrayIcon()
{
if( m_show )
Delete();
}
bool
CTaskTrayIcon::Create( HWND hwnd, UINT id, UINT msg, HICON icon, LPCTSTR tip )
{
if( m_show )
Delete();
m_data.hWnd = hwnd;
m_data.uID = id;
m_data.uFlags = ((msg != 0) ? NIF_MESSAGE : 0) | ((icon != NULL) ? NIF_ICON : 0) | ((tip != NULL) ? NIF_TIP : 0);
m_data.uCallbackMessage = msg;
m_data.hIcon = icon;
if( tip != NULL )
strcpy_s( m_data.szTip, tip );
else
strcpy_s( m_data.szTip, "" );
m_restartMsg = RegisterWindowMessage( "TaskTrayCreated" );
return true;
}
bool
CTaskTrayIcon::Restore()
{
Delete();
return Add();
}
bool
CTaskTrayIcon::Add()
{
if( m_show )
return Modify();
if( !Shell_NotifyIcon( NIM_ADD, &m_data ) )
return false;
m_show = true;
return true;
}
bool
CTaskTrayIcon::Delete()
{
m_show = FALSE;
return Shell_NotifyIcon( NIM_DELETE, &m_data ) != FALSE;
}
bool
CTaskTrayIcon::Modify()
{
if( m_show )
return Shell_NotifyIcon( NIM_MODIFY, &m_data ) != FALSE;
else
return Add();
}
bool
CTaskTrayIcon::SetMessage( UINT msg, BOOL refresh )
{
if( msg == 0 )
m_data.uFlags = m_data.uFlags & ( !NIF_MESSAGE );
else
{
m_data.uFlags |= NIF_MESSAGE;
m_data.uCallbackMessage = msg;
}
if( refresh )
Modify();
return true;
}
HICON
CTaskTrayIcon::SetIcon( HICON icon,BOOL refresh )
{
HICON old = m_data.hIcon;
if( icon == NULL )
m_data.uFlags = m_data.uFlags & ( !NIF_ICON );
else
{
m_data.uFlags |= NIF_ICON;
m_data.hIcon = icon;
}
if( refresh )
Modify();
return old;
}
bool
CTaskTrayIcon::SetTip(LPCTSTR tip,BOOL refresh)
{
if( tip == NULL )
m_data.uFlags = m_data.uFlags & ( !NIF_TIP );
else
{
m_data.uFlags |= NIF_TIP;
lstrcpy( m_data.szTip, tip );
}
if( refresh )
Modify();
return true;
}
void
CTaskTrayIcon::ExplorerRestartCheck( UINT message )
{
if( message == GetRestartMessage() )
Restore();
}
///////////////////////////////////////////////////////////////////////////
// helper
void
HideMainWnd( HWND hwnd )
{
LONG exstyle = GetWindowLong( hwnd, GWL_EXSTYLE );
exstyle = ( exstyle & ~WS_EX_APPWINDOW ) | WS_EX_TOOLWINDOW;
SetWindowLong( hwnd, GWL_EXSTYLE, exstyle );
}