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
|
/* 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);
HINSTANCE layer_instance;
HWND layer_window;
HDC layer_dc;
HBITMAP layer_bitmap;
void *layer_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);
}
int layer_window_new(int x, int y, int w, int h)
{
RECT rect;
DWORD style;
BITMAPINFO_GDI bitmap_info;
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(WS_EX_LAYERED,
"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;
}
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.biXPelsPerMeter = 0;
bitmap_info.bih.biYPelsPerMeter = 0;
bitmap_info.bih.biClrUsed = 0;
bitmap_info.bih.biClrImportant = 0;
bitmap_info.bih.biBitCount = 32;
bitmap_info.bih.biCompression = BI_RGB;
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);
ShowWindow(layer_window, SW_SHOWNORMAL);
UpdateWindow(layer_window);
return 1;
}
void
layer_window_free()
{
DeleteObject(layer_bitmap);
ReleaseDC(layer_window, layer_dc);
DestroyWindow(layer_window);
}
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++)
*tmp = 0x000000ff;
dc = CreateCompatibleDC(layer_dc);
if (!dc)
return;
SelectObject(dc, layer_bitmap);
/* POINT dest; */
/* POINT src = {0, 0}; */
/* SIZE client = { 200, 200 }; */
/* BLENDFUNCTION bf = {AC_SRC_OVER, 0, 255, AC_SRC_ALPHA}; */
/* ClientToScreen(layer_window, &dest); */
/* UpdateLayeredWindow(layer_window, layer_dc, &dest, &client, */
/* dc, &src, 0, &bf, ULW_ALPHA); */
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;
}
/* 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_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;
/* 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