-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathComboBoxExtList.cpp
189 lines (163 loc) · 5.22 KB
/
ComboBoxExtList.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
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
188
189
// ComboBoxExtList.cpp : implementation file
//
#include "pch.h"
#include "ComboBoxExt.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CComboBoxExtList
CComboBoxExtList::CComboBoxExtList()
:m_bShowTooltip(TRUE)
,m_bTooltipOnInfo(FALSE)
,m_bShowListTooltipOverItem(FALSE)
,m_bToolActive(FALSE)
,m_nPrevItem(-1)
,m_pComboBox(NULL)
,m_hWndToolTip(NULL)
{
memset(&m_ToolInfo, 0, sizeof(m_ToolInfo));
}
CComboBoxExtList::~CComboBoxExtList()
{
}
BEGIN_MESSAGE_MAP(CComboBoxExtList, CListBox)
//{{AFX_MSG_MAP(CComboBoxExtList)
ON_WM_MOUSEMOVE()
ON_WM_TIMER()
//}}AFX_MSG_MAP
ON_MESSAGE(LB_FINDSTRING, CComboBoxExtList::OnLbFindString)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CComboBoxExtList message handlers
void CComboBoxExtList::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base class
// create tooltip
m_hWndToolTip = ::CreateWindowEx(WS_EX_TOPMOST,
TOOLTIPS_CLASS,
NULL,
TTS_NOPREFIX | TTS_ALWAYSTIP,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
NULL,
NULL);
if (NULL != m_hWndToolTip) // initialize toolinfo struct
{
memset(&m_ToolInfo, 0, sizeof(m_ToolInfo));
#if (NTDDI_VERSION >= NTDDI_WINXP)
m_ToolInfo.cbSize = sizeof(TTTOOLINFO) - sizeof(void*);
#else
m_ToolInfo.cbSize = sizeof(TTTOOLINFO);
#endif
m_ToolInfo.uFlags = TTF_TRACK | TTF_TRANSPARENT;
m_ToolInfo.hwnd = m_hWnd;
::SendMessage(m_hWndToolTip, TTM_SETMAXTIPWIDTH, (WPARAM)0, (LPARAM)SHRT_MAX);
::SendMessage(m_hWndToolTip, TTM_ADDTOOL, (WPARAM)0, (LPARAM)(LPTOOLINFO)&m_ToolInfo);
}
CListBox::PreSubclassWindow();
}
LRESULT CComboBoxExtList::OnLbFindString(WPARAM wParam, LPARAM lParam)
{
return SendMessage(LB_FINDSTRINGEXACT, wParam, lParam);
}
void CComboBoxExtList::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CListBox::OnMouseMove(nFlags, point);
if (NULL == m_pComboBox || ! m_bShowTooltip)
return;
CRect rectClient;
GetClientRect(&rectClient);
if (rectClient.PtInRect(point))
{
CPoint pointScreen;
::GetCursorPos(&pointScreen);
BOOL bOutside = FALSE;
int nItem = ItemFromPoint(point, bOutside); // calculate listbox item number (if any)
if (! bOutside && nItem >= 0 && m_nPrevItem != nItem)
{
m_nPrevItem = nItem;
m_bToolActive = FALSE;
CString sText;
if (! m_bTooltipOnInfo)
GetText(nItem, sText);
else
m_pComboBox->GetLBInfo(nItem, sText);
m_ToolInfo.lpszText = sText.GetBuffer(0);
sText.ReleaseBuffer();
CRect rect;
GetItemRect(nItem, &rect);
ClientToScreen(&rect);
if (! m_bTooltipOnInfo)
{
SIZE size;
HDC hDC = ::GetDC(m_hWnd);
CFont* pFont = GetFont();
HFONT hOldFont = (HFONT)::SelectObject(hDC, (HFONT)*pFont);
::GetTextExtentPoint32(hDC, sText, sText.GetLength(), &size);
::SelectObject(hDC, hOldFont);
::ReleaseDC(m_hWnd, hDC);
if (! m_bToolActive && size.cx > rect.Width() - 3)
{
::SendMessage(m_hWndToolTip, TTM_UPDATETIPTEXT, (WPARAM)0, (LPARAM)&m_ToolInfo);
::SendMessage(m_hWndToolTip, TTM_TRACKPOSITION, (WPARAM)0, (LPARAM)MAKELONG(rect.left, rect.top));
::SendMessage(m_hWndToolTip, TTM_TRACKACTIVATE, (WPARAM)TRUE, (LPARAM)(LPTOOLINFO)&m_ToolInfo);
m_bToolActive = TRUE;
}
else
{
::SendMessage(m_hWndToolTip, TTM_TRACKACTIVATE, (WPARAM)FALSE, (LPARAM)(LPTOOLINFO)&m_ToolInfo);
m_bToolActive = FALSE;
SetTimer(1, 80, NULL);
}
}
else // tooltip text is retrieved from additional info
{
if (! m_bToolActive && m_pComboBox->GetLBShowItemTooltipState(nItem) && ! sText.IsEmpty())
{
::SendMessage(m_hWndToolTip, TTM_UPDATETIPTEXT, (WPARAM)0, (LPARAM)&m_ToolInfo);
::SendMessage(m_hWndToolTip, TTM_TRACKPOSITION, (WPARAM)0, (LPARAM)MAKELONG(m_bShowListTooltipOverItem ? rect.left : rect.right + 1, rect.top));
::SendMessage(m_hWndToolTip, TTM_TRACKACTIVATE, (WPARAM)TRUE, (LPARAM)(LPTOOLINFO)&m_ToolInfo);
m_bToolActive = TRUE;
}
else
{
::SendMessage(m_hWndToolTip, TTM_TRACKACTIVATE, (WPARAM)FALSE, (LPARAM)(LPTOOLINFO)&m_ToolInfo);
m_bToolActive = FALSE;
SetTimer(1, 80, NULL);
}
}
}
}
else
{
::SendMessage(m_hWndToolTip, TTM_TRACKACTIVATE, (WPARAM)FALSE, (LPARAM)(LPTOOLINFO)&m_ToolInfo);
m_bToolActive = FALSE;
m_nPrevItem = -1;
SetTimer(1, 80, NULL);
}
}
void CComboBoxExtList::OnTimer(UINT_PTR nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CListBox::OnTimer(nIDEvent);
CPoint point;
::GetCursorPos(&point);
ScreenToClient(&point);
CRect rectClient;
GetClientRect(&rectClient);
if (! rectClient.PtInRect(point) || 0 == (GetStyle() & WS_VISIBLE))
{
KillTimer(nIDEvent);
::SendMessage(m_hWndToolTip, TTM_TRACKACTIVATE, (WPARAM)FALSE, (LPARAM)(LPTOOLINFO)&m_ToolInfo);
m_bToolActive = FALSE;
m_nPrevItem = -1;
}
}