-
Notifications
You must be signed in to change notification settings - Fork 0
/
WindowDisplay.cpp
179 lines (148 loc) · 4.39 KB
/
WindowDisplay.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
176
177
178
179
#include "WindowDisplay.h"
#include <Windows.h>
#include <iostream>
#include <thread>
#include <mutex>
#include <functional>
#include "GraphicsTypes.h"
#define CLASS_NAME L"MyWindowClass"
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
static std::function<Bitmap* ()> onDraw;
static bool manualRepaint = false;
static HWND window;
static Bitmap* lastBitmap = nullptr;
BOOL CALLBACK GetWorkerW(HWND hwnd, LPARAM lParam) {
HWND p = FindWindowEx(hwnd, NULL, L"SHELLDLL_DefView", NULL);
HWND* ret = (HWND*)lParam;
if (p && *ret == NULL)
{
*ret = FindWindowEx(NULL, hwnd, L"WorkerW", NULL);
}
return true;
}
int WindowHandler::init(InitMode initMode, bool pManualRepaint, std::function<Bitmap* ()> pOnDraw)
{
manualRepaint = pManualRepaint;
onDraw = pOnDraw;
auto t = std::thread([initMode](std::string message) {
HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(
GetConsoleWindow(),
GWLP_HINSTANCE);
WNDCLASS wc = {};
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
if (!RegisterClass(&wc))
{
std::cerr << "Failed to register window class\n";
return -1;
}
window = CreateWindowEx(
0,
CLASS_NAME,
L"My Window",
WS_POPUP | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
GetDesktopWindow(),
NULL,
hInstance,
NULL
);
if (!window)
{
std::cerr << "Failed to create window\n";
return -1;
}
if (initMode == InitMode::WALLPAPER)
{
HWND hDesktop = FindWindow(L"Progman", NULL);
if (!hDesktop)
{
return -1;
}
SendMessageTimeout(hDesktop, 0x052C, NULL, NULL, SMTO_NORMAL, 1000, NULL);
HWND hWorkerW = NULL;
EnumWindows(GetWorkerW, (LPARAM)&hWorkerW);
if (!hWorkerW)
return -1;
SetParent(window, hWorkerW);
}
ShowWindow(window, SW_SHOWMAXIMIZED);
SetWindowPos(window, HWND_TOP,
0, 0,
1920, 1080,
SWP_SHOWWINDOW);
if (!manualRepaint)
{
HDC hdc = GetDC(window);
int vrefresh = GetDeviceCaps(hdc, VREFRESH);
ReleaseDC(window, hdc);
SetTimer(window, 1, 1000 / vrefresh, NULL);
}
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}, "Hello");
t.detach();
return 0;
}
void WindowHandler::repaint()
{
InvalidateRect(window, NULL, FALSE);
UpdateWindow(window);
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
auto bitmap = onDraw();
if (bitmap != lastBitmap && lastBitmap != nullptr)
{
delete lastBitmap;
}
BITMAPINFO bmi;
ZeroMemory(&bmi, sizeof(BITMAPINFO));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = bitmap->width;
bmi.bmiHeader.biHeight = -bitmap->height;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = bitmap->getBitsPerPixel();
bmi.bmiHeader.biCompression = BI_RGB;
HBITMAP hbm = CreateDIBitmap(hdc, &bmi.bmiHeader, CBM_INIT, bitmap->pixels, &bmi, DIB_RGB_COLORS);
HDC hdcMem = CreateCompatibleDC(hdc);
HGDIOBJ oldbm = SelectObject(hdcMem, hbm);
BitBlt(hdc, 0, 0, bitmap->width, bitmap->height, hdcMem, 0, 0, SRCCOPY);
SelectObject(hdcMem, oldbm);
DeleteDC(hdcMem);
DeleteObject(hbm);
EndPaint(hwnd, &ps);
lastBitmap = bitmap;
} break;
case WM_DESTROY:
{
PostQuitMessage(0);
} return 0;
case WM_CREATE:
{
HDC hdc = GetDC(hwnd);
int vrefresh = GetDeviceCaps(hdc, VREFRESH);
SetTimer(hwnd, 1, 1000 / vrefresh, NULL);
} break;
case WM_TIMER:
{
WindowHandler::repaint();
} break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}
void WindowHandler::update()
{
}