-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhoverBox.c
More file actions
149 lines (127 loc) · 3.74 KB
/
hoverBox.c
File metadata and controls
149 lines (127 loc) · 3.74 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
138
139
140
141
142
143
144
145
146
147
148
149
#include "hoverBox.h"
#include <windows.h>
BOOL isHoverBoxClassInit = FALSE;
LRESULT CALLBACK fnWinProcHoverBox(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
HWND fnCreateHoverBox(HWND parent, int x, int y, LPCSTR string)
{
TCHAR szHoverBoxClass[] = _TEXT("Hover Hint Box");
HWND hWnd;
WNDCLASS wndClass;
if (!isHoverBoxClassInit) {
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = sizeof(LPCSTR);
wndClass.hbrBackground = NULL;
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hIcon = NULL;
wndClass.hInstance = GetModuleHandle(NULL);
wndClass.lpfnWndProc = fnWinProcHoverBox;
wndClass.lpszClassName = szHoverBoxClass;
wndClass.lpszMenuName = NULL;
wndClass.style = CS_HREDRAW | CS_VREDRAW;
if (!RegisterClass(&wndClass)) {
MessageBox(parent, _TEXT("Cannot register hover box class!"), _TEXT("ERROR"), MB_ICONERROR);
return NULL;
}
isHoverBoxClassInit = TRUE;
}
HDC hdc = GetDC(NULL);
HFONT hFont = CreateFontA(
-16,
0,
0,
0,
FW_MEDIUM,
FALSE,
FALSE,
FALSE,
DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
CLEARTYPE_QUALITY,
DEFAULT_PITCH|FF_SWISS,
"Segoe UI"
);
HFONT hOldFont = (HFONT)SelectObject(hdc, hFont);
SIZE sz;
GetTextExtentPoint32A(hdc, string, lstrlenA(string), &sz);
SelectObject(hdc, hOldFont);
DeleteObject(hFont);
ReleaseDC(NULL, hdc);
int paddingX = 12;
int paddingY = 6;
int width = sz.cx + 2 * paddingX;
int height = sz.cy + 2 * paddingY;
hWnd = CreateWindowEx(
WS_EX_TOOLWINDOW | WS_EX_TOPMOST | WS_EX_NOACTIVATE,
szHoverBoxClass,
NULL,
WS_POPUP,
x, y,
width, height,
NULL,
NULL,
GetModuleHandle(NULL),
(LPVOID)string
);
HRGN hRgn = CreateRoundRectRgn(0, 0, width, height, 12, 12);
SetWindowRgn(hWnd, hRgn, TRUE);
ShowWindow(hWnd, SW_SHOWNOACTIVATE);
UpdateWindow(hWnd);
return hWnd;
}
LRESULT CALLBACK fnWinProcHoverBox(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg) {
case WM_CREATE: {
CREATESTRUCT* cs = (CREATESTRUCT*)lParam;
LPCSTR text = (LPCSTR)cs->lpCreateParams;
SetWindowLongPtr(hWnd, 0, (LONG_PTR)text);
return 0;
}
case WM_PAINT: {
LPCSTR text = (LPCSTR)GetWindowLongPtr(hWnd, 0);
if (!text) break;
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
RECT rc;
GetClientRect(hWnd, &rc);
HBRUSH br = CreateSolidBrush(RGB(20,20,20));
FillRect(hdc, &rc, br);
DeleteObject(br);
HFONT hFont = CreateFontA(
-16,
0,
0,
0,
FW_MEDIUM,
FALSE,
FALSE,
FALSE,
DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
CLEARTYPE_QUALITY,
DEFAULT_PITCH|FF_SWISS,
"Segoe UI"
);
HFONT hOldFont = (HFONT)SelectObject(hdc, hFont);
SetBkMode(hdc, TRANSPARENT);
SetTextColor(hdc, RGB(255,255,255));
int paddingX = 12;
int paddingY = 6;
RECT textRect = rc;
textRect.left += paddingX;
textRect.right -= paddingX;
textRect.top += paddingY;
textRect.bottom -= paddingY;
DrawTextA(hdc, text, -1, &textRect, DT_SINGLELINE | DT_LEFT | DT_VCENTER);
SelectObject(hdc, hOldFont);
DeleteObject(hFont);
EndPaint(hWnd, &ps);
return 0;
}
default:
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}