7 imss_win32(unsigned hwnd_u, int include_decor, int left, int top,
8 int right, int bottom, int display) {
9 HWND hwnd = (HWND)hwnd_u;
10 HDC cdc = 0, wdc, bmdc;
11 HBITMAP work_bmp, old_dc_bmp;
14 int window_width, window_height;
16 unsigned char *di_bits;
25 wdc = GetWindowDC(hwnd);
26 GetWindowRect(hwnd, &rect);
30 GetClientRect(hwnd, &rect);
33 i_push_error(0, "Cannot get window DC - invalid hwnd?");
37 window_width = rect.right - rect.left;
38 window_height = rect.bottom - rect.top;
42 fprintf(stderr, "all desktops\n");
43 cdc = CreateDC("DISPLAY", NULL, NULL, NULL);
44 orig_x = GetSystemMetrics(SM_XVIRTUALSCREEN);
45 orig_y = GetSystemMetrics(SM_YVIRTUALSCREEN);
46 window_width = GetSystemMetrics(SM_CXVIRTUALSCREEN);
47 window_height = GetSystemMetrics(SM_CYVIRTUALSCREEN);
53 if (EnumDisplayDevices(NULL, display, &dd, 0)) {
54 fprintf(stderr, "found %d -> %s\n", display, dd.DeviceName);
55 cdc = CreateDC(dd.DeviceName, dd.DeviceName, NULL, NULL);
58 i_push_errorf(0, "Cannot enumerate device %d: %ld", display, (long)GetLastError());
62 window_width = GetDeviceCaps(cdc, HORZRES);
63 window_height = GetDeviceCaps(cdc, VERTRES);
69 /* adjust negative/zero values to window size */
75 right += window_width;
77 bottom += window_height;
82 if (right > window_width)
86 if (bottom > window_height)
87 bottom = window_height;
90 if (right <= left || bottom <= top) {
91 i_push_error(0, "image would be empty");
95 height = bottom - top;
97 work_bmp = CreateCompatibleBitmap(wdc, width, height);
98 bmdc = CreateCompatibleDC(wdc);
99 old_dc_bmp = SelectObject(bmdc, work_bmp);
100 BitBlt(bmdc, 0, 0, width, height, wdc, orig_x + left, orig_y + top, SRCCOPY);
103 memset(&bmi, 0, sizeof(bmi));
104 bmi.bmiHeader.biSize = sizeof(bmi);
105 bmi.bmiHeader.biWidth = width;
106 bmi.bmiHeader.biHeight = -height;
107 bmi.bmiHeader.biPlanes = 1;
108 bmi.bmiHeader.biBitCount = 32;
109 bmi.bmiHeader.biCompression = BI_RGB;
111 di_bits = mymalloc(4 * width * height);
112 if (GetDIBits(bmdc, work_bmp, 0, height, di_bits, &bmi, DIB_RGB_COLORS)) {
113 i_color *line = mymalloc(sizeof(i_color) * width);
116 unsigned char *ch_pp = di_bits;
117 result = i_img_8_new(width, height, 3);
119 for (y = 0; y < height; ++y) {
121 for (x = 0; x < width; ++x) {
122 cp->rgb.b = *ch_pp++;
123 cp->rgb.g = *ch_pp++;
124 cp->rgb.r = *ch_pp++;
128 i_plin(result, 0, width, y, line);
133 i_tags_setn(&result->tags, "ss_window_width", window_width);
134 i_tags_setn(&result->tags, "ss_window_height", window_height);
135 i_tags_set(&result->tags, "ss_type", "Win32", 5);
136 i_tags_setn(&result->tags, "ss_left", left);
137 i_tags_setn(&result->tags, "ss_top", top);
141 SelectObject(bmdc, old_dc_bmp);
143 DeleteObject(work_bmp);
149 ReleaseDC(hwnd, wdc);