forked from derekmcloughlin/gvimfullscreen_win32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gvimfullscreen.cpp
175 lines (152 loc) · 6.1 KB
/
gvimfullscreen.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
/*
cl /LD gvimfullscreen.c user32.lib
------------------------------
:call libcallnr("gvimfullscreen.dll", "EnableFullScreen", 1)
*/
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
BOOL CALLBACK FindWindowProc(HWND hwnd, LPARAM lParam);
void FixBackgroundColor(HWND hwndTextArea);
struct WindowInfo;
void ReadWindowInfo(char* serialized_state, WindowInfo* window_info);
void WriteWindowInfo(WindowInfo* window_info);
struct WindowInfo {
BOOL maximized;
BOOL fullscreen;
LONG style;
LONG exstyle;
RECT rc;
};
// string buffer to return back to vim with the state information to persist
char g_serialized_window_info[2048] = "";
extern "C"
__declspec(dllexport)
char* ToggleFullScreen(char* prev_state) {
HWND hwnd = NULL;
EnumThreadWindows(GetCurrentThreadId(), FindWindowProc, (LPARAM)&hwnd);
HWND hwndTextArea = FindWindowEx(hwnd, NULL, "VimTextArea",
"Vim text area");
WindowInfo window_info;
ReadWindowInfo(prev_state, &window_info);
if (hwnd) {
if (!window_info.fullscreen) {
FixBackgroundColor(hwndTextArea);
// Save original window state, position, and size
window_info.maximized = IsZoomed(hwnd);
window_info.style = GetWindowLong(hwnd, GWL_STYLE);
window_info.exstyle = GetWindowLong(hwnd, GWL_EXSTYLE);
GetWindowRect(hwnd, &window_info.rc);
// Get the monitor it is on
MONITORINFO mi;
mi.cbSize = sizeof(MONITORINFO);
GetMonitorInfo(MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST),
&mi);
// Fullscreen it
#if 0 // appears to not be a problem on Windows 10
if (window_info.maximized) {
// Restore the window if it was maximized first, because
// Windows seems to have trouble hiding the taskbar when
// changing the size of a maximized window.
SendMessage(hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
}
#endif
SetWindowLong(hwnd, GWL_STYLE,
window_info.style & ~(WS_CAPTION |
WS_THICKFRAME));
SetWindowLong(hwnd, GWL_EXSTYLE,
window_info.exstyle & ~(WS_EX_DLGMODALFRAME |
WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE |
WS_EX_STATICEDGE));
SetWindowPos(hwnd, NULL,
mi.rcMonitor.left,
mi.rcMonitor.top,
mi.rcMonitor.right - mi.rcMonitor.left,
mi.rcMonitor.bottom - mi.rcMonitor.top,
SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
window_info.fullscreen = TRUE;
} else {
// Already full screen, so restore all the previous styles
SetWindowLong(hwnd, GWL_STYLE, window_info.style);
SetWindowLong(hwnd, GWL_EXSTYLE, window_info.exstyle);
SetWindowPos(hwnd, NULL,
window_info.rc.left,
window_info.rc.top,
window_info.rc.right - window_info.rc.left,
window_info.rc.bottom - window_info.rc.top,
SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
#if 0 // again appears to not be a problem on Windows 10
if (window_info.maximized) {
SendMessage(hwnd, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
}
#endif
window_info.fullscreen = FALSE;
}
}
WriteWindowInfo(&window_info);
return g_serialized_window_info;
}
BOOL CALLBACK FindWindowProc(HWND hwnd, LPARAM lParam) {
HWND* pphWnd = (HWND*)lParam;
if (GetParent(hwnd)) {
*pphWnd = NULL;
return TRUE;
}
*pphWnd = hwnd;
return FALSE;
}
void FixBackgroundColor(HWND hwndTextArea) {
// Get lower right corner color and use that as stock brush color
// for the background color
if (hwndTextArea != NULL) {
COLORREF rgb = RGB(255,0,255);
HDC hdc = GetDC(hwndTextArea);
if (hdc != NULL) {
RECT rc;
GetWindowRect(hwndTextArea, &rc);
rgb = GetPixel(hdc, rc.right - rc.left - 2,
rc.bottom - rc.top - 2);
if (rgb != CLR_INVALID) {
SetDCBrushColor(hdc, rgb);
}
ReleaseDC(hwndTextArea, hdc);
}
SetClassLongPtr(hwndTextArea, GCLP_HBRBACKGROUND,
(LONG_PTR)GetStockObject(DC_BRUSH));
// Setting the parent background removes flicker when resizing the
// window up to fullscreen. However it doesn't appear to work for
// the first toggle. Not sure how to remedy that.
HWND hwnd = GetParent(hwndTextArea);
HBRUSH hbr = CreateSolidBrush(rgb);
SetClassLongPtr(hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)hbr);
}
}
void ReadWindowInfo(char* serialized_state, WindowInfo* window_info) {
ZeroMemory(window_info, sizeof(WindowInfo));
if (!serialized_state || !serialized_state[0]) {
window_info->fullscreen = FALSE;
return;
}
sscanf_s(serialized_state,
"%d,%d,%d,%d,%d,%d,%d,%d",
&window_info->fullscreen,
&window_info->maximized,
&window_info->style,
&window_info->exstyle,
&window_info->rc.left,
&window_info->rc.top,
&window_info->rc.right,
&window_info->rc.bottom);
}
void WriteWindowInfo(WindowInfo* window_info) {
sprintf_s(g_serialized_window_info,
"%d,%d,%d,%d,%d,%d,%d,%d",
window_info->fullscreen,
window_info->maximized,
window_info->style,
window_info->exstyle,
window_info->rc.left,
window_info->rc.top,
window_info->rc.right,
window_info->rc.bottom);
}