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 cdc = CreateDC("DISPLAY", NULL, NULL, NULL);
43 orig_x = GetSystemMetrics(SM_XVIRTUALSCREEN);
44 orig_y = GetSystemMetrics(SM_YVIRTUALSCREEN);
45 window_width = GetSystemMetrics(SM_CXVIRTUALSCREEN);
46 window_height = GetSystemMetrics(SM_CYVIRTUALSCREEN);
52 if (EnumDisplayDevices(NULL, display, &dd, 0)) {
53 if (dd.StateFlags & DISPLAY_DEVICE_ACTIVE) {
54 cdc = CreateDC(dd.DeviceName, dd.DeviceName, NULL, NULL);
57 i_push_errorf(0, "Display device %d not active", display);
62 i_push_errorf(0, "Cannot enumerate device %d: %ld", display, (long)GetLastError());
66 window_width = GetDeviceCaps(cdc, HORZRES);
67 window_height = GetDeviceCaps(cdc, VERTRES);
73 /* adjust negative/zero values to window size */
79 right += window_width;
81 bottom += window_height;
86 if (right > window_width)
90 if (bottom > window_height)
91 bottom = window_height;
94 if (right <= left || bottom <= top) {
95 i_push_error(0, "image would be empty");
102 width = right - left;
103 height = bottom - top;
105 work_bmp = CreateCompatibleBitmap(wdc, width, height);
106 bmdc = CreateCompatibleDC(wdc);
107 old_dc_bmp = SelectObject(bmdc, work_bmp);
108 BitBlt(bmdc, 0, 0, width, height, wdc, orig_x + left, orig_y + top, SRCCOPY);
111 memset(&bmi, 0, sizeof(bmi));
112 bmi.bmiHeader.biSize = sizeof(bmi);
113 bmi.bmiHeader.biWidth = width;
114 bmi.bmiHeader.biHeight = -height;
115 bmi.bmiHeader.biPlanes = 1;
116 bmi.bmiHeader.biBitCount = 32;
117 bmi.bmiHeader.biCompression = BI_RGB;
119 di_bits = mymalloc(4 * width * height);
120 if (GetDIBits(bmdc, work_bmp, 0, height, di_bits, &bmi, DIB_RGB_COLORS)) {
121 result = i_img_8_new(width, height, 3);
124 i_color *line = mymalloc(sizeof(i_color) * width);
127 unsigned char *ch_pp = di_bits;
128 for (y = 0; y < height; ++y) {
130 for (x = 0; x < width; ++x) {
131 cp->rgb.b = *ch_pp++;
132 cp->rgb.g = *ch_pp++;
133 cp->rgb.r = *ch_pp++;
137 i_plin(result, 0, width, y, line);
143 i_tags_setn(&result->tags, "ss_window_width", window_width);
144 i_tags_setn(&result->tags, "ss_window_height", window_height);
145 i_tags_set(&result->tags, "ss_type", "Win32", 5);
146 i_tags_setn(&result->tags, "ss_left", left);
147 i_tags_setn(&result->tags, "ss_top", top);
151 SelectObject(bmdc, old_dc_bmp);
153 DeleteObject(work_bmp);
159 ReleaseDC(hwnd, wdc);