Left: | ||
Right: |
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::CommonControlsInitialize() | 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 TOOLINFO ti; | 41 TOOLINFOW ti = {}; |
Wladimir Palant
2013/09/19 08:59:16
We should declare this as TOOLINFOW explicitly.
| |
43 » ti.cbSize = sizeof(TOOLINFO); | 42 ti.cbSize = sizeof(TOOLINFOW); |
Wladimir Palant
2013/09/19 08:59:16
Indentation is off, convert tabs to spaces?
| |
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 = (LPWSTR)message.c_str();» » | 47 ti.lpszText = const_cast<LPWSTR>(message.c_str()); |
Wladimir Palant
2013/09/19 08:59:16
I think that the conversion to LPWSTR is unnecessa
Oleksandr
2013/09/25 10:03:14
We need to remove const here.
Wladimir Palant
2013/09/25 10:27:14
Please use const_cast to indicate that. It's a pit
| |
49 GetClientRect(parentWindow, &ti.rect); | 48 GetClientRect(parentWindow, &ti.rect); |
50 | 49 |
51 » LRESULT res = ::SendMessage(toolTipWindow, TTM_ADDTOOL, 0, (LPARAM) (LPT OOLINFO) &ti); | 50 LRESULT res = ::SendMessage(toolTipWindow, TTM_ADDTOOL, 0, (LPARAM)&ti); |
Wladimir Palant
2013/09/19 08:59:16
The conversion to LPTOOLINFO is unnecessary - &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_s tr()); | 56 SetTextAndIcon(message, title, icon); |
58 » res = ::SendMessage(toolTipWindow, TTM_TRACKACTIVATE, TRUE, (LPARAM)(LPT OOLINFO) &ti); | 57 res = ::SendMessage(toolTipWindow, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti); |
Wladimir Palant
2013/09/19 08:59:16
Same here, the conversion to LPTOOLINFO is unneces
| |
59 | 58 |
60 return true; | 59 return true; |
61 } | 60 } |
62 | 61 |
63 bool NotificationMessage::Hide() | 62 void NotificationMessage::Hide() |
64 { | 63 { |
65 DestroyWindow(toolTipWindow); | 64 if (toolTipWindow != 0) |
66 toolTipWindow = 0; | 65 { |
67 return true; | 66 DestroyWindow(toolTipWindow); |
67 toolTipWindow = 0; | |
68 } | |
68 } | 69 } |
69 | 70 |
70 void NotificationMessage::Move(short x, short y) | 71 void NotificationMessage::Move(short x, short y) |
71 { | 72 { |
72 » ::SendMessage(toolTipWindow, TTM_TRACKPOSITION, 0, (LPARAM)(LPTOOLINFO)M AKELONG(x, y)); | 73 ::SendMessage(toolTipWindow, TTM_TRACKPOSITION, 0, MAKELONG(x, y)); |
Wladimir Palant
2013/09/19 08:59:16
The conversion to LPTOOLINFO is wrong here, that's
| |
73 return; | 74 return; |
74 } | 75 } |
75 | 76 |
76 bool NotificationMessage::SetTextAndIcon(std::wstring text, std::wstring title, int icon) | 77 bool NotificationMessage::SetTextAndIcon(std::wstring text, std::wstring title, int icon) |
77 { | 78 { |
78 TOOLINFO ti; | 79 TOOLINFOW ti = {}; |
79 » ti.cbSize = sizeof(TOOLINFO); | 80 ti.cbSize = sizeof(TOOLINFOW); |
Wladimir Palant
2013/09/19 08:59:16
As above, this should be TOOLINFOW.
| |
80 ti.uFlags = TTF_IDISHWND | TTF_TRACK | TTF_TRANSPARENT; | 81 ti.hwnd = toolTipWindow; |
81 » ti.hwnd = toolTipWindow; | 82 ti.hinst = NULL; |
82 » ti.hinst = NULL; | 83 ti.uId = (UINT_PTR)parentWindow; |
83 » ti.uId = (UINT_PTR)parentWindow; | 84 ti.lpszText = const_cast<LPWSTR>(text.c_str()); |
84 » ti.lpszText = (LPWSTR)text.c_str();» » | 85 LRESULT res = ::SendMessage(toolTipWindow, TTM_SETTITLE, icon, (LPARAM)title.c _str()); |
Wladimir Palant
2013/09/19 08:59:16
As above, the conversion to LPWSTR seems unnecessa
| |
85 GetClientRect(parentWindow, &ti.rect); | 86 res = ::SendMessage(toolTipWindow, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti); |
86 » LRESULT res = ::SendMessage(toolTipWindow, TTM_SETTITLE, icon, (LPARAM)t itle.c_str()); | |
87 » res = ::SendMessage(toolTipWindow, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti); | |
88 return res == TRUE; | 87 return res == TRUE; |
89 } | 88 } |
90 | 89 |
91 void NotificationMessage::SetParent(HWND parent) | 90 void NotificationMessage::SetParent(HWND parent) |
92 { | 91 { |
93 parentWindow = parent; | 92 parentWindow = parent; |
94 } | 93 } |
94 | |
95 bool NotificationMessage::IsVisible() | 95 bool NotificationMessage::IsVisible() |
Wladimir Palant
2013/09/19 08:59:16
Nit: Empty line before this function.
| |
96 { | 96 { |
97 if (toolTipWindow == 0) | 97 if (toolTipWindow == 0) |
98 return false; | 98 return false; |
99 return IsWindowVisible(toolTipWindow); | 99 return IsWindowVisible(toolTipWindow); |
Wladimir Palant
2013/09/19 08:59:16
Given that we never hide the window but always des
Oleksandr
2013/09/25 10:03:14
The window may become hidden without us hiding it.
Wladimir Palant
2013/09/25 10:27:14
I see. We could react to WM_WINDOWPOSCHANGED then
Oleksandr
2013/09/25 12:37:45
Did that. Also if we do miss the NotificationMessa
| |
100 } | 100 } |
LEFT | RIGHT |