changes for mingw
[imager-screenshot.git] / scwin32.c
CommitLineData
a9735f1a
TC
1#include "imext.h"\r
2#include <windows.h>\r
3#include <string.h>\r
4\r
5i_img *\r
6imss_win32(unsigned hwnd_u, int include_decor) {\r
7 HWND hwnd = (HWND)hwnd_u;\r
8 HDC wdc, bmdc;\r
9 RECT rect;\r
10 HBITMAP work_bmp, old_dc_bmp;\r
11 int width, height;\r
12 BITMAPINFO bmi;\r
13 unsigned char *di_bits;\r
14 i_img *result = NULL;\r
15\r
16 i_clear_error();\r
17\r
18 if (!hwnd)\r
19 hwnd = GetDesktopWindow();\r
20\r
21 if (include_decor) {\r
22 wdc = GetWindowDC(hwnd);\r
23 GetWindowRect(hwnd, &rect);\r
24 }\r
25 else {\r
26 wdc = GetDC(hwnd);\r
27 GetClientRect(hwnd, &rect);\r
28 }\r
29 if (!wdc) {\r
30 i_push_error(0, "Cannot get window DC - invalid hwnd?");\r
31 return NULL;\r
32 }\r
33\r
34 width = rect.right - rect.left;\r
35 height = rect.bottom - rect.top;\r
36 work_bmp = CreateCompatibleBitmap(wdc, width, height);\r
37 bmdc = CreateCompatibleDC(wdc);\r
38 old_dc_bmp = SelectObject(bmdc, work_bmp);\r
39 BitBlt(bmdc, 0, 0, width, height, wdc, 0, 0, SRCCOPY);\r
40\r
41 /* make a dib */\r
42 memset(&bmi, 0, sizeof(bmi));\r
43 bmi.bmiHeader.biSize = sizeof(bmi);\r
44 bmi.bmiHeader.biWidth = width;\r
45 bmi.bmiHeader.biHeight = -height;\r
46 bmi.bmiHeader.biPlanes = 1;\r
47 bmi.bmiHeader.biBitCount = 32;\r
48 bmi.bmiHeader.biCompression = BI_RGB;\r
49\r
50 di_bits = mymalloc(4 * width * height);\r
51 if (GetDIBits(bmdc, work_bmp, 0, height, di_bits, &bmi, DIB_RGB_COLORS)) {\r
52 i_color *line = mymalloc(sizeof(i_color) * width);\r
53 i_color *cp;\r
54 int x, y;\r
55 unsigned char *ch_pp = di_bits;\r
56 result = i_img_8_new(width, height, 3);\r
57\r
58 for (y = 0; y < height; ++y) {\r
59 cp = line;\r
60 for (x = 0; x < width; ++x) {\r
61 cp->rgb.b = *ch_pp++;\r
62 cp->rgb.g = *ch_pp++;\r
63 cp->rgb.r = *ch_pp++;\r
64 ch_pp++;\r
65 cp++;\r
66 }\r
67 i_plin(result, 0, width, y, line);\r
68 }\r
69 myfree(line);\r
70 }\r
71\r
72 /* clean up */\r
73 myfree(di_bits);\r
74 SelectObject(bmdc, old_dc_bmp);\r
75 DeleteDC(bmdc);\r
76 DeleteObject(work_bmp);\r
77 ReleaseDC(hwnd, wdc);\r
78\r
79 return result;\r
80}\r