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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
| /* gcc -g -Wall -o layer.exe layer.c -lgdi32 */
#define _WIN32_WINNT 0x0500
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
typedef struct BITMAPINFO_GDI BITMAPINFO_GDI;
struct BITMAPINFO_GDI
{
BITMAPINFOHEADER bih;
DWORD masks[3];
};
LRESULT CALLBACK _window_procedure(HWND window,
UINT message,
WPARAM window_param,
LPARAM data_param);
static int layered_on = 0;
HINSTANCE layer_instance;
HWND layer_window;
HDC layer_dc;
HBITMAP layer_bitmap;
static void *layer_data = NULL;
HDC mask_dc;
HBITMAP mask_bitmap;
void *mask_data;
int layer_init()
{
WNDCLASS wc;
layer_instance = GetModuleHandle(NULL);
if (!layer_instance)
return 0;
memset (&wc, 0, sizeof (WNDCLASS));
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = _window_procedure;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = layer_instance;
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(1 + COLOR_BTNFACE);
wc.lpszMenuName = NULL;
wc.lpszClassName = "Class";
if(!RegisterClass(&wc))
{
FreeLibrary(layer_instance);
return 0;
}
return 1;
}
void layer_shutdown()
{
FreeLibrary(layer_instance);
}
void layer_bitmap_window_new()
{
BITMAPINFO_GDI bitmap_info;
RECT rect;
int w, h;
GetClientRect(layer_window, &rect);
w = rect.right - rect.left;
h = rect.bottom - rect.top;
printf ("%dx%d\n", w, h);
ZeroMemory(&bitmap_info, sizeof(BITMAPINFOHEADER));
bitmap_info.bih.biSize = sizeof(BITMAPINFOHEADER);
bitmap_info.bih.biWidth = w;
bitmap_info.bih.biHeight = -h;
bitmap_info.bih.biPlanes = 1;
bitmap_info.bih.biSizeImage = 4 * w * h;
bitmap_info.bih.biBitCount = 32;
bitmap_info.bih.biCompression = BI_BITFIELDS;
bitmap_info.masks[0] = 0x00ff0000;
bitmap_info.masks[1] = 0x0000ff00;
bitmap_info.masks[2] = 0x000000ff;
layer_bitmap = CreateDIBSection(layer_dc,
(const BITMAPINFO *)&bitmap_info,
DIB_RGB_COLORS,
(void **)(&layer_data),
NULL,
0);
}
void layer_bitmap_window_free()
{
DeleteObject(layer_bitmap);
}
void layer_bitmap_mask_new()
{
BITMAPINFO_GDI bitmap_info;
RECT rect;
int w, h;
mask_dc = CreateCompatibleDC(layer_dc);
GetClientRect(layer_window, &rect);
w = rect.right - rect.left;
h = rect.bottom - rect.top;
ZeroMemory(&bitmap_info, sizeof(BITMAPINFOHEADER));
bitmap_info.bih.biSize = sizeof(BITMAPINFOHEADER);
bitmap_info.bih.biWidth = w;
bitmap_info.bih.biHeight = -h;
bitmap_info.bih.biPlanes = 1;
bitmap_info.bih.biSizeImage = 4 * w * h;
bitmap_info.bih.biBitCount = 32;
bitmap_info.bih.biCompression = BI_RGB;
mask_bitmap = CreateDIBSection(mask_dc,
(const BITMAPINFO *)&bitmap_info,
DIB_RGB_COLORS,
(void **)(&mask_data),
NULL,
0);
memset(mask_data, 0, bitmap_info.bih.biSizeImage);
GdiFlush();
}
void layer_bitmap_mask_free()
{
DeleteObject(mask_bitmap);
DeleteDC(mask_dc);
}
int layer_window_new(int x, int y, int w, int h)
{
RECT rect;
DWORD style;
style = WS_OVERLAPPEDWINDOW | WS_SIZEBOX;
rect.left = 0;
rect.top = 0;
rect.right = w;
rect.bottom = h;
if (!AdjustWindowRect(&rect, style, FALSE))
return 0;
layer_window = CreateWindowEx(0,
"Class", "Test",
style,
x, y,
rect.right - rect.left,
rect.bottom - rect.top,
NULL,
NULL, layer_instance, NULL);
if (!layer_window)
return 0;
layer_dc = GetDC(layer_window);
if (!layer_dc)
{
DestroyWindow(layer_window);
return 0;
}
return 1;
}
void
layer_window_free()
{
ReleaseDC(layer_window, layer_dc);
DestroyWindow(layer_window);
}
void
layer_window_show()
{
ShowWindow(layer_window, SW_SHOWNORMAL);
UpdateWindow(layer_window);
}
void layer_window_layered_set(int on)
{
DWORD style;
printf ("%s: %d\n", __FUNCTION__, on);
style = GetWindowLong(layer_window, GWL_EXSTYLE);
if (on)
style |= WS_EX_LAYERED;
else
style &= ~WS_EX_LAYERED;
SetWindowLongPtr(layer_window, GWL_EXSTYLE, style);
if (on)
{
RECT rect;
POINT client = {0,0};
POINT src = {0,0};
SIZE size;
int i;
int *tmp_window;
int *tmp_mask;
BLENDFUNCTION bf = {AC_SRC_OVER, 0, 255, AC_SRC_ALPHA};
HGDIOBJ prev_obj;
GetClientRect(layer_window, &rect);
ClientToScreen(layer_window, &client);
size.cx = rect.right - rect.left;
size.cy = rect.bottom - rect.top;
tmp_window = layer_data;
tmp_mask = mask_data;
for (i = 0; i < size.cx * size.cy; i++, tmp_window++, tmp_mask++)
{
if ((*tmp_window & 0xff000000) == 0x00000000)
*tmp_mask = 0;
else
{
*tmp_mask = 0xff000000 + *tmp_window;
}
}
prev_obj = SelectObject(mask_dc, mask_bitmap);
printf (" * %ldx%ld\n", client.x, client.y);
printf (" * %ldx%ld\n", size.cx, size.cy);
printf (" * %ldx%ld\n", src.x, src.y);
if (!UpdateLayeredWindow(layer_window, layer_dc,
&client, &size,
mask_dc, &src, 0, &bf, ULW_ALPHA))
printf ("error\n");
SelectObject(mask_dc, prev_obj);
}
else
{
/* not working */
SendMessage(layer_window, WM_PAINT, 0, 0);
}
}
void layer_paint()
{
int i, j, *tmp;
HDC dc;
tmp = layer_data;
for (j = 0; j < 200; j++)
for (i = 0; i < 200; i++, tmp++)
{
if (i > 150 && j > 150)
*tmp = 0x000000ff;
else
*tmp = 0xffff0000;
}
dc = CreateCompatibleDC(layer_dc);
if (!dc)
return;
SelectObject(dc, layer_bitmap);
BitBlt(layer_dc,
0, 0,
200, 200,
dc,
0, 0,
SRCCOPY);
DeleteDC(dc);
}
int main()
{
if (!layer_init())
return EXIT_FAILURE;
if (!layer_window_new(10, 10, 200, 200))
{
layer_shutdown();
return EXIT_FAILURE;
}
layer_bitmap_window_new();
layer_bitmap_mask_new();
layer_window_show();
/* msg loop */
while(1)
{
MSG msg;
BOOL ret;
ret = PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
if (ret)
{
do
{
if (msg.message == WM_QUIT)
goto beach;
TranslateMessage(&msg);
DispatchMessageW(&msg);
} while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE));
}
}
beach:
layer_bitmap_mask_free();
layer_bitmap_window_free();
layer_window_free();
layer_shutdown();
return EXIT_SUCCESS;
}
LRESULT CALLBACK
_window_procedure(HWND window,
UINT message,
WPARAM window_param,
LPARAM data_param)
{
switch (message)
{
case WM_CLOSE:
PostQuitMessage(0);
return 0;
case WM_KEYUP:
if (window_param == 'T')
{
layered_on = !layered_on;
layer_window_layered_set(layered_on);
}
if (window_param == 'Q')
{
PostQuitMessage(0);
}
return 0;
/* GDI notifications */
case WM_PAINT:
{
RECT rect;
printf("paint\n");
if (GetUpdateRect(window, &rect, FALSE))
{
PAINTSTRUCT ps;
HDC hdc;
hdc = BeginPaint(window, &ps);
layer_paint();
EndPaint(window, &ps);
}
return 0;
}
default:
return DefWindowProc(window, message, window_param, data_param);
}
} |
Partager