-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.cpp
125 lines (108 loc) · 3.6 KB
/
main.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
#define OEMRESOURCE
#include <windows.h>
#include <strsafe.h>
#include "resource.h"
#define UNICODE
#define _UNICODE
HINSTANCE g_instance;
HCURSOR g_hc_ibeam;
UINT_PTR g_timer = NULL;
DWORD g_layout = 0;
NOTIFYICONDATAW g_notifyIconData;
HMENU g_hMenu;
void CALLBACK UpdateTimer(HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
HKL layout = GetKeyboardLayout(GetWindowThreadProcessId(GetForegroundWindow(), NULL));
int caps = GetKeyState(VK_CAPITAL) & 0xFFFF;
if (caps) {
HCURSOR hc_new = LoadCursor(g_instance, MAKEINTRESOURCE((reinterpret_cast<UINT_PTR>(layout) & 0xFFFF) * 10 + 2));
if (hc_new)
{
SetSystemCursor(hc_new, OCR_IBEAM);
}
else
{
SetSystemCursor(CopyCursor(g_hc_ibeam), OCR_IBEAM);
}
}
if (g_layout != reinterpret_cast<UINT_PTR>(layout) && !caps)
{
HCURSOR hc_new = LoadCursor(g_instance, MAKEINTRESOURCE(reinterpret_cast<UINT_PTR>(layout) & 0xFFFF));
if (hc_new)
{
SetSystemCursor(hc_new, OCR_IBEAM);
}
else
{
SetSystemCursor(CopyCursor(g_hc_ibeam), OCR_IBEAM);
}
}
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_COMMAND:
if (LOWORD(wParam) == ID_TRAY_EXIT)
{
PostQuitMessage(0);
}
break;
case WM_DESTROY:
Shell_NotifyIcon(NIM_DELETE, &g_notifyIconData);
PostQuitMessage(0);
break;
case WM_USER + 1:
if (lParam == WM_RBUTTONUP)
{
POINT pt;
GetCursorPos(&pt);
SetForegroundWindow(hWnd);
TrackPopupMenu(g_hMenu, TPM_RIGHTBUTTON, pt.x, pt.y, 0, hWnd, NULL);
}
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return 0;
}
int Main()
{
HANDLE mutex = CreateMutex(NULL, FALSE, L"Mova");
if (GetLastError() == ERROR_ALREADY_EXISTS || GetLastError() == ERROR_ACCESS_DENIED) return 1;
g_hc_ibeam = CopyCursor(LoadCursor(NULL, IDC_IBEAM));
if (!g_hc_ibeam) return 1;
g_instance = GetModuleHandle(NULL);
g_timer = SetTimer(NULL, g_timer, 200, UpdateTimer);
if (!g_timer) return 1;
WNDCLASSW wc = { 0 }; // Використовуємо WNDCLASSW для підтримки Unicode
wc.lpfnWndProc = WindowProc;
wc.hInstance = g_instance;
wc.hIcon = LoadIcon(g_instance, MAKEINTRESOURCE(IDI_APP_ICON));
wc.lpszClassName = L"LangCursorClass";
RegisterClassW(&wc);
HWND hWnd = CreateWindowW(L"LangCursorClass", L"Mova", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, g_instance, NULL);
if (!hWnd) return 1;
g_hMenu = CreatePopupMenu();
AppendMenuW(g_hMenu, MF_STRING, ID_TRAY_EXIT, L"Вихід");
g_notifyIconData.cbSize = sizeof(NOTIFYICONDATAW);
g_notifyIconData.hWnd = hWnd;
g_notifyIconData.uID = 1;
g_notifyIconData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
g_notifyIconData.uCallbackMessage = WM_USER + 1;
g_notifyIconData.hIcon = LoadIcon(g_instance, MAKEINTRESOURCE(IDI_TRAY_ICON));
StringCchCopyW(g_notifyIconData.szTip, ARRAYSIZE(g_notifyIconData.szTip), L"Mova");
Shell_NotifyIconW(NIM_ADD, &g_notifyIconData); // Використовуємо Shell_NotifyIconW для Unicode
MSG msg;
while (GetMessage(&msg, 0, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
DestroyCursor(g_hc_ibeam);
return 0;
}
EXTERN_C void WINAPI WinMainCRTStartup()
{
ExitProcess(Main());
}