-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMouseTracker.cpp
More file actions
135 lines (113 loc) · 3.57 KB
/
MouseTracker.cpp
File metadata and controls
135 lines (113 loc) · 3.57 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
#include <windows.h>
#include <stdio.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
char g_szClassName[] = "MouseTracker";
HINSTANCE g_hInst = NULL;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
g_hInst = hInstance;
WNDCLASSEXA wc;
wc.cbSize = sizeof(WNDCLASSEXA);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIconA(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursorA(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIconA(NULL, IDI_APPLICATION);
if(!RegisterClassExA(&wc))
{
return 0;
}
HWND hwnd = CreateWindowExA(
WS_EX_TOPMOST | WS_EX_TOOLWINDOW,
g_szClassName,
"Mouse Tracker",
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT,
120, 50,
NULL, NULL,
hInstance,
NULL
);
if(hwnd == NULL)
{
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
MSG msg;
while(GetMessageA(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessageA(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_CREATE:
{
SetTimer(hwnd, 1, 50, NULL);
return 0;
}
case WM_TIMER:
{
POINT pt;
GetCursorPos(&pt);
DEVMODE devMode;
devMode.dmSize = sizeof(devMode);
if (EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &devMode))
{
int screenWidth = devMode.dmPelsWidth;
int screenHeight = devMode.dmPelsHeight;
char text[50];
sprintf_s(text, sizeof(text), "X : %ld, Y : %ld",
pt.x * screenWidth / (GetSystemMetrics(SM_CXSCREEN) - 1),
pt.y * screenHeight / (GetSystemMetrics(SM_CYSCREEN) - 1));
PAINTSTRUCT ps;
HDC hdc = GetDC(hwnd);
RECT rect;
GetClientRect(hwnd, &rect);
FillRect(hdc, &rect, (HBRUSH)(COLOR_WINDOW + 1));
HFONT hFont = CreateFont(
15,
0,
0,
0,
FW_NORMAL,
FALSE,
FALSE,
FALSE,
DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH,
"Arial"
);
HFONT hOldFont = (HFONT)SelectObject(hdc, hFont);
DrawTextA(hdc, text, -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
// Clean up
SelectObject(hdc, hOldFont);
DeleteObject(hFont);
ReleaseDC(hwnd, hdc);
}
return 0;
}
case WM_CLOSE:
DestroyWindow(hwnd);
return 0;
case WM_DESTROY:
KillTimer(hwnd, 1);
PostQuitMessage(0);
return 0;
}
return DefWindowProcA(hwnd, uMsg, wParam, lParam);
}