/
main.cpp
123 lines (99 loc) · 2.86 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
#include <Windows.h>
#include <stdexcept>
#include <sstream>
#include <tchar.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
NOTIFYICONDATA g_TrayIcon;
HHOOK g_hMouse;
BOOL IsFullScreenAppRunning()
{
HWND hWnd = GetForegroundWindow();
RECT rc;
GetWindowRect(hWnd, &rc);
if (rc.right - rc.left == GetSystemMetrics(SM_CXSCREEN) && rc.bottom - rc.top == GetSystemMetrics(SM_CYSCREEN))
return TRUE;
return FALSE;
}
LRESULT CALLBACK LowLevelMouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
MSLLHOOKSTRUCT* pms = reinterpret_cast<MSLLHOOKSTRUCT*>(lParam);
if (wParam == WM_MOUSEMOVE)
{
if (pms->pt.x < 2) // check if mouse x position is close to the left edge
{
if ( !IsFullScreenAppRunning() )
{
HWND taskbar = FindWindow(TEXT("Shell_TrayWnd"), NULL);
SwitchToThisWindow(taskbar, TRUE);
}
}
}
return CallNextHookEx(g_hMouse, nCode, wParam, lParam);
}
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
try {
WNDCLASSEX wx = {};
LPCTSTR lpszClassName = TEXT("Win7TaskbarFix");
wx.cbSize = sizeof(wx);
wx.lpfnWndProc = WndProc;
wx.hInstance = hInstance;
wx.lpszClassName = lpszClassName;
int iResult = RegisterClassEx(&wx);
if (iResult == 0)
throw std::runtime_error("RegisterClassEx returned 0");
HWND hWnd = CreateWindowEx(0, lpszClassName, TEXT(""), 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, hInstance, NULL);
if (hWnd == 0)
throw std::runtime_error("CreateWindowEx returned 0");
g_TrayIcon.cbSize = sizeof(g_TrayIcon);
g_TrayIcon.hIcon = LoadIcon(NULL, IDI_WINLOGO);
g_TrayIcon.hWnd = hWnd;
g_TrayIcon.uCallbackMessage = WM_USER + 200;
g_TrayIcon.uFlags = NIF_ICON | NIF_TIP | NIF_SHOWTIP | NIF_MESSAGE;
g_TrayIcon.uID = 1;
_tcscpy_s(g_TrayIcon.szTip, TEXT("Taskbar autohide fix"));
iResult = Shell_NotifyIcon(NIM_ADD, &g_TrayIcon);
if(iResult == FALSE)
throw std::runtime_error("Shell_NotifyIcon returned FALSE");
// setting up mouse hook
g_hMouse = SetWindowsHookEx(WH_MOUSE_LL, LowLevelMouseProc, NULL, 0);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// exception handler
} catch (const std::exception& e) {
std::ostringstream ssErrorMsg;
ssErrorMsg << e.what() << "\n"
"GLE: " << GetLastError();
MessageBoxA(NULL, ssErrorMsg.str().c_str(), "Error in WinMain", MB_ICONERROR);
return -1;
}
// cleanup
Shell_NotifyIcon(NIM_DELETE, &g_TrayIcon);
UnhookWindowsHookEx(g_hMouse);
return 0;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_USER + 200:
{
switch (lParam)
{
case WM_LBUTTONDBLCLK:
PostQuitMessage(0); // exit WinMain message loop on double click allowing for proper cleanup
break;
default:
break;
}
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}