forked from stuerp/foo_vis_spectrum_analyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToolTips.cpp
More file actions
138 lines (101 loc) · 3.37 KB
/
ToolTips.cpp
File metadata and controls
138 lines (101 loc) · 3.37 KB
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
/** $VER: ToolTips.cpp (2024.03.26) P. Stuer **/
#include "framework.h"
#include "UIElement.h"
#pragma hdrstop
/// <summary>
/// Creates the ToolTip control.
/// </summary>
void UIElement::CreateToolTipControl() noexcept
{
if (_ToolTipControl.Create(m_hWnd, nullptr, nullptr, TTS_ALWAYSTIP | TTS_NOANIMATE) == NULL)
return;
_ToolTipControl.SetMaxTipWidth(100);
::SetWindowTheme(_ToolTipControl, _DarkMode ? L"DarkMode_Explorer" : nullptr, nullptr);
}
/// <summary>
/// Deletes the current tracking tooltip.
/// </summary>
void UIElement::DeleteTrackingToolTip() noexcept
{
if (!_ToolTipControl.IsWindow())
return;
_ToolTipControl.TrackActivate(&_TrackingToolInfo, FALSE);
_TrackingGraph = nullptr;
}
/// <summary>
/// Handles mouse move messages.
/// </summary>
void UIElement::OnMouseMove(UINT, CPoint pt)
{
if (!_ToolTipControl.IsWindow())
return;
if (_TrackingGraph == nullptr)
{
_TrackingGraph = GetGraph(pt);
if (_TrackingGraph == nullptr)
return;
// Tell Windows we want to know when the mouse leaves this window.
{
TRACKMOUSEEVENT tme = { sizeof(TRACKMOUSEEVENT) };
tme.dwFlags = TME_LEAVE;
tme.hwndTrack = m_hWnd;
::TrackMouseEvent(&tme);
}
_LastMousePos = pt;
_LastIndex = ~0U;
FLOAT ScaledX = (FLOAT) ::MulDiv((int) pt.x, USER_DEFAULT_SCREEN_DPI, (int) _DPI);
FLOAT ScaledY = (FLOAT) ::MulDiv((int) pt.y, USER_DEFAULT_SCREEN_DPI, (int) _DPI);
std::wstring Text;
size_t Index;
if (_TrackingGraph->GetToolTipText(ScaledX, ScaledY, Text, Index))
{
_TrackingGraph->InitToolInfo(m_hWnd, _TrackingToolInfo);
_ToolTipControl.TrackActivate(&_TrackingToolInfo, TRUE);
}
}
else
{
if (pt != _LastMousePos)
{
_LastMousePos = pt;
FLOAT ScaledX = (FLOAT) ::MulDiv((int) pt.x, USER_DEFAULT_SCREEN_DPI, (int) _DPI);
FLOAT ScaledY = (FLOAT) ::MulDiv((int) pt.y, USER_DEFAULT_SCREEN_DPI, (int) _DPI);
std::wstring Text;
size_t Index;
if (_TrackingGraph->GetToolTipText(ScaledX, ScaledY, Text, Index))
{
if (Index != _LastIndex)
{
_TrackingToolInfo.lpszText = (LPWSTR) Text.c_str();
_ToolTipControl.UpdateTipText(&_TrackingToolInfo);
_LastIndex = Index;
}
// Reposition the tooltip.
::ClientToScreen(m_hWnd, &pt);
RECT tr;
_ToolTipControl.GetClientRect(&tr);
int x = pt.x + 4;
int y = pt.y - 4 - tr.bottom;
RECT wr;
GetWindowRect(&wr);
if (x + tr.right >= wr.right)
x = pt.x - 4 - tr.right;
if (y <= wr.top)
y = pt.y + 4;
_ToolTipControl.TrackPosition(x, y);
}
else
{
_ToolTipControl.TrackActivate(&_TrackingToolInfo, FALSE);
_TrackingGraph = nullptr;
}
}
}
}
/// <summary>
/// Turns off the tracking tooltip when the mouse leaves the window.
/// </summary>
void UIElement::OnMouseLeave()
{
DeleteTrackingToolTip();
}