LEFT | RIGHT |
1 #include <Windows.h> | 1 #include <Windows.h> |
2 #include <CommCtrl.h> | 2 #include <CommCtrl.h> |
3 | 3 |
4 #include "NotificationMessage.h" | 4 #include "NotificationMessage.h" |
5 | 5 |
6 NotificationMessage::NotificationMessage() | 6 NotificationMessage::NotificationMessage(HWND parent): parentWindow(parent) |
7 { | |
8 CommonControlsInitialize(); | |
9 } | |
10 | |
11 NotificationMessage::NotificationMessage(HWND parent) | |
12 { | 7 { |
13 parentWindow = parent; | 8 toolTipWindow = 0; |
14 CommonControlsInitialize(); | 9 InitializeCommonControls(); |
15 }; | 10 }; |
16 | 11 |
17 bool NotificationMessage::commonControlsInitialized(false); | 12 bool NotificationMessage::commonControlsInitialized(false); |
18 | 13 |
19 void NotificationMessage::InitializeCommonControls() | 14 void NotificationMessage::InitializeCommonControls() |
20 { | 15 { |
21 if (!commonControlsInitialized) | 16 if (!commonControlsInitialized) |
22 { | 17 { |
23 INITCOMMONCONTROLSEX commControls; | 18 INITCOMMONCONTROLSEX commControls; |
24 commControls.dwSize = sizeof(INITCOMMONCONTROLSEX); | 19 commControls.dwSize = sizeof(INITCOMMONCONTROLSEX); |
25 commControls.dwICC = ICC_BAR_CLASSES; | 20 commControls.dwICC = ICC_BAR_CLASSES; |
26 InitCommonControlsEx(&commControls); | 21 InitCommonControlsEx(&commControls); |
27 commonControlsInitialized = true; | 22 commonControlsInitialized = true; |
28 } | 23 } |
29 } | 24 } |
30 | 25 |
31 bool NotificationMessage::Show(std::wstring message, std::wstring title, int ico
n) | 26 bool NotificationMessage::Show(std::wstring message, std::wstring title, int ico
n) |
32 { | 27 { |
| 28 if (toolTipWindow != 0) |
| 29 { |
| 30 Hide(); |
| 31 } |
33 toolTipWindow = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS, NULL, | 32 toolTipWindow = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS, NULL, |
34 TTS_NOPREFIX | TTS_BALLOON | TTS_CLOSE, | 33 TTS_NOPREFIX | TTS_BALLOON | TTS_CLOSE, |
35 0, 0, | 34 0, 0, |
36 0, 0, | 35 0, 0, |
37 parentWindow, NULL, NULL, | 36 parentWindow, NULL, NULL, |
38 NULL); | 37 NULL); |
39 | 38 |
40 SetWindowPos(toolTipWindow, HWND_TOPMOST,0, 0, 0, 0, | 39 SetWindowPos(toolTipWindow, HWND_TOPMOST,0, 0, 0, 0, |
41 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE); | 40 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE); |
42 TOOLINFOW ti; | 41 TOOLINFOW ti = {}; |
43 ti.cbSize = sizeof(TOOLINFOW); | 42 ti.cbSize = sizeof(TOOLINFOW); |
44 ti.uFlags = TTF_IDISHWND | TTF_TRACK | TTF_TRANSPARENT; | 43 ti.uFlags = TTF_IDISHWND | TTF_TRACK | TTF_TRANSPARENT; |
45 ti.hwnd = toolTipWindow; | 44 ti.hwnd = toolTipWindow; |
46 ti.hinst = NULL; | 45 ti.hinst = NULL; |
47 ti.uId = (UINT_PTR)parentWindow; | 46 ti.uId = (UINT_PTR)parentWindow; |
48 ti.lpszText = const_cast<LPWSTR>(message.c_str()); | 47 ti.lpszText = const_cast<LPWSTR>(message.c_str()); |
49 GetClientRect(parentWindow, &ti.rect); | 48 GetClientRect(parentWindow, &ti.rect); |
50 | 49 |
51 LRESULT res = ::SendMessage(toolTipWindow, TTM_ADDTOOL, 0, (LPARAM)&ti); | 50 LRESULT res = ::SendMessage(toolTipWindow, TTM_ADDTOOL, 0, (LPARAM)&ti); |
52 | 51 |
53 RECT rect; | 52 RECT rect; |
54 GetWindowRect(parentWindow, &rect); | 53 GetWindowRect(parentWindow, &rect); |
55 Move(rect.left + (rect.right - rect.left) / 2, rect.top + (rect.bottom - rect.
top) / 2); | 54 Move(rect.left + (rect.right - rect.left) / 2, rect.top + (rect.bottom - rect.
top) / 2); |
56 | 55 |
57 res = ::SendMessage(toolTipWindow, TTM_SETTITLE, icon, (LPARAM)title.c_str()); | 56 SetTextAndIcon(message, title, icon); |
58 res = ::SendMessage(toolTipWindow, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti); | 57 res = ::SendMessage(toolTipWindow, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti); |
59 | 58 |
60 return true; | 59 return true; |
61 } | 60 } |
62 | 61 |
63 bool NotificationMessage::Hide() | 62 void NotificationMessage::Hide() |
64 { | 63 { |
65 if (IsVisible()) | 64 if (toolTipWindow != 0) |
66 { | 65 { |
67 DestroyWindow(toolTipWindow); | 66 DestroyWindow(toolTipWindow); |
68 toolTipWindow = 0; | 67 toolTipWindow = 0; |
69 return true; | |
70 } | |
71 else | |
72 { | |
73 return false; | |
74 } | 68 } |
75 } | 69 } |
76 | 70 |
77 void NotificationMessage::Move(short x, short y) | 71 void NotificationMessage::Move(short x, short y) |
78 { | 72 { |
79 ::SendMessage(toolTipWindow, TTM_TRACKPOSITION, 0, MAKELONG(x, y)); | 73 ::SendMessage(toolTipWindow, TTM_TRACKPOSITION, 0, MAKELONG(x, y)); |
80 return; | 74 return; |
81 } | 75 } |
82 | 76 |
83 bool NotificationMessage::SetTextAndIcon(std::wstring text, std::wstring title,
int icon) | 77 bool NotificationMessage::SetTextAndIcon(std::wstring text, std::wstring title,
int icon) |
84 { | 78 { |
85 TOOLINFOW ti; | 79 TOOLINFOW ti = {}; |
86 ti.cbSize = sizeof(TOOLINFOW); | 80 ti.cbSize = sizeof(TOOLINFOW); |
87 ti.uFlags = TTF_IDISHWND | TTF_TRACK | TTF_TRANSPARENT; | |
88 ti.hwnd = toolTipWindow; | 81 ti.hwnd = toolTipWindow; |
89 ti.hinst = NULL; | 82 ti.hinst = NULL; |
90 ti.uId = (UINT_PTR)parentWindow; | 83 ti.uId = (UINT_PTR)parentWindow; |
91 ti.lpszText = const_cast<LPWSTR>(text.c_str()); | 84 ti.lpszText = const_cast<LPWSTR>(text.c_str()); |
92 GetClientRect(parentWindow, &ti.rect); | |
93 LRESULT res = ::SendMessage(toolTipWindow, TTM_SETTITLE, icon, (LPARAM)title.c
_str()); | 85 LRESULT res = ::SendMessage(toolTipWindow, TTM_SETTITLE, icon, (LPARAM)title.c
_str()); |
94 res = ::SendMessage(toolTipWindow, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti); | 86 res = ::SendMessage(toolTipWindow, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti); |
95 return res == TRUE; | 87 return res == TRUE; |
96 } | 88 } |
97 | 89 |
98 void NotificationMessage::SetParent(HWND parent) | 90 void NotificationMessage::SetParent(HWND parent) |
99 { | 91 { |
100 parentWindow = parent; | 92 parentWindow = parent; |
101 } | 93 } |
102 | 94 |
103 bool NotificationMessage::IsVisible() | 95 bool NotificationMessage::IsVisible() |
104 { | 96 { |
105 if (toolTipWindow == 0) | 97 if (toolTipWindow == 0) |
106 return false; | 98 return false; |
107 return IsWindowVisible(toolTipWindow); | 99 return IsWindowVisible(toolTipWindow); |
108 } | 100 } |
LEFT | RIGHT |