69 #define WIN32_API_ERROR(str) \
70 av_log(s1, AV_LOG_ERROR, str " (error %li)\n", GetLastError())
72 #define REGION_WND_BORDER 3
94 hdc = BeginPaint(hwnd, &ps);
96 GetClientRect(hwnd, &rect);
97 FrameRect(hdc, &rect, GetStockObject(BLACK_BRUSH));
99 rect.left++; rect.top++; rect.right--; rect.bottom--;
100 FrameRect(hdc, &rect, GetStockObject(WHITE_BRUSH));
102 rect.left++; rect.top++; rect.right--; rect.bottom--;
103 FrameRect(hdc, &rect, GetStockObject(BLACK_BRUSH));
108 return DefWindowProc(hwnd, msg, wparam, lparam);
126 HRGN region_interior = NULL;
128 DWORD style = WS_POPUP | WS_VISIBLE;
129 DWORD ex = WS_EX_TOOLWINDOW | WS_EX_TOPMOST | WS_EX_TRANSPARENT;
134 AdjustWindowRectEx(&rect, style,
FALSE, ex);
138 hwnd = CreateWindowEx(ex, WC_DIALOG, NULL, style, rect.left, rect.top,
139 rect.right - rect.left, rect.bottom - rect.top,
140 NULL, NULL, NULL, NULL);
147 GetClientRect(hwnd, &rect);
148 region = CreateRectRgn(0, 0,
149 rect.right - rect.left, rect.bottom - rect.top);
153 CombineRgn(region, region, region_interior, RGN_DIFF);
154 if (!SetWindowRgn(hwnd, region,
FALSE)) {
160 DeleteObject(region_interior);
164 ShowWindow(hwnd, SW_SHOW);
172 DeleteObject(region);
174 DeleteObject(region_interior);
210 while (PeekMessage(&msg, hwnd, 0, 0, PM_REMOVE)) {
211 DispatchMessage(&msg);
233 const char *filename = s1->
filename;
234 const char *
name = NULL;
243 if (!strncmp(filename,
"title=", 6)) {
245 hwnd = FindWindow(NULL, name);
248 "Can't find window '%s', aborting.\n", name);
254 "Can't show region when grabbing a window.\n");
257 }
else if (!strcmp(filename,
"desktop")) {
261 "Please use \"desktop\" or \"title=<windowname>\" to specify your target.\n");
267 GetClientRect(hwnd, &virtual_rect);
269 virtual_rect.left = GetSystemMetrics(SM_XVIRTUALSCREEN);
270 virtual_rect.top = GetSystemMetrics(SM_YVIRTUALSCREEN);
271 virtual_rect.right = virtual_rect.left + GetSystemMetrics(SM_CXVIRTUALSCREEN);
272 virtual_rect.bottom = virtual_rect.top + GetSystemMetrics(SM_CYVIRTUALSCREEN);
277 clip_rect.left = virtual_rect.left;
278 clip_rect.top = virtual_rect.top;
279 clip_rect.right = virtual_rect.right;
280 clip_rect.bottom = virtual_rect.bottom;
288 if (clip_rect.left < virtual_rect.left ||
289 clip_rect.top < virtual_rect.top ||
290 clip_rect.right > virtual_rect.right ||
291 clip_rect.bottom > virtual_rect.bottom) {
293 "Capture area (%li,%li),(%li,%li) extends outside window area (%li,%li),(%li,%li)",
294 clip_rect.left, clip_rect.top,
295 clip_rect.right, clip_rect.bottom,
296 virtual_rect.left, virtual_rect.top,
297 virtual_rect.right, virtual_rect.bottom);
304 source_hdc = GetDC(hwnd);
310 bpp = GetDeviceCaps(source_hdc, BITSPIXEL);
314 "Found window %s, capturing %lix%lix%i at (%li,%li)\n",
316 clip_rect.right - clip_rect.left,
317 clip_rect.bottom - clip_rect.top,
318 bpp, clip_rect.left, clip_rect.top);
321 "Capturing whole desktop as %lix%lix%i at (%li,%li)\n",
322 clip_rect.right - clip_rect.left,
323 clip_rect.bottom - clip_rect.top,
324 bpp, clip_rect.left, clip_rect.top);
327 if (clip_rect.right - clip_rect.left <= 0 ||
328 clip_rect.bottom - clip_rect.top <= 0 || bpp%8) {
334 dest_hdc = CreateCompatibleDC(source_hdc);
342 bmi.bmiHeader.biSize =
sizeof(BITMAPINFOHEADER);
343 bmi.bmiHeader.biWidth = clip_rect.right - clip_rect.left;
344 bmi.bmiHeader.biHeight = -(clip_rect.bottom - clip_rect.top);
345 bmi.bmiHeader.biPlanes = 1;
346 bmi.bmiHeader.biBitCount = bpp;
347 bmi.bmiHeader.biCompression = BI_RGB;
348 bmi.bmiHeader.biSizeImage = 0;
349 bmi.bmiHeader.biXPelsPerMeter = 0;
350 bmi.bmiHeader.biYPelsPerMeter = 0;
351 bmi.bmiHeader.biClrUsed = 0;
352 bmi.bmiHeader.biClrImportant = 0;
353 hbmp = CreateDIBSection(dest_hdc, &bmi, DIB_RGB_COLORS,
361 if (!SelectObject(dest_hdc, hbmp)) {
368 GetObject(hbmp,
sizeof(BITMAP), &bmp);
377 gdigrab->
frame_size = bmp.bmWidthBytes * bmp.bmHeight * bmp.bmPlanes;
378 gdigrab->
header_size =
sizeof(BITMAPFILEHEADER) +
sizeof(BITMAPINFOHEADER) +
379 (bpp <= 8 ? (1 << bpp) : 0) *
sizeof(RGBQUAD) ;
409 ReleaseDC(hwnd, source_hdc);
415 DeleteDC(source_hdc);
429 #define CURSOR_ERROR(str) \
430 if (!gdigrab->cursor_error_printed) { \
431 WIN32_API_ERROR(str); \
432 gdigrab->cursor_error_printed = 1; \
435 ci.cbSize =
sizeof(ci);
437 if (GetCursorInfo(&ci)) {
438 HCURSOR icon = CopyCursor(ci.hCursor);
444 if (ci.flags != CURSOR_SHOWING)
451 icon = CopyCursor(LoadCursor(NULL, IDC_ARROW));
454 if (!GetIconInfo(icon, &info)) {
459 pos.x = ci.ptScreenPos.x - clip_rect.left - info.xHotspot;
460 pos.y = ci.ptScreenPos.y - clip_rect.top - info.yHotspot;
465 if (GetWindowRect(hwnd, &rect)) {
475 ci.ptScreenPos.x, ci.ptScreenPos.y, pos.x, pos.y);
477 if (pos.x >= 0 && pos.x <= clip_rect.right - clip_rect.left &&
478 pos.y >= 0 && pos.y <= clip_rect.bottom - clip_rect.top) {
479 if (!DrawIcon(gdigrab->
dest_hdc, pos.x, pos.y, icon))
508 BITMAPFILEHEADER bfh;
511 int64_t curtime, delay;
514 time_frame += INT64_C(1000000);
523 delay = time_frame *
av_q2d(time_base) - curtime;
525 if (delay < INT64_C(-1000000) *
av_q2d(time_base)) {
526 time_frame += INT64_C(1000000);
542 if (!BitBlt(dest_hdc, 0, 0,
543 clip_rect.right - clip_rect.left,
544 clip_rect.bottom - clip_rect.top,
546 clip_rect.left, clip_rect.top, SRCCOPY | CAPTUREBLT)) {
556 bfh.bfSize = file_size;
561 memcpy(pkt->
data, &bfh,
sizeof(bfh));
563 memcpy(pkt->
data +
sizeof(bfh), &gdigrab->
bmi.bmiHeader,
sizeof(gdigrab->
bmi.bmiHeader));
565 if (gdigrab->
bmi.bmiHeader.biBitCount <= 8)
566 GetDIBColorTable(dest_hdc, 0, 1 << gdigrab->
bmi.bmiHeader.biBitCount,
567 (RGBQUAD *) (pkt->
data +
sizeof(bfh) +
sizeof(gdigrab->
bmi.bmiHeader)));
594 DeleteObject(s->
hbmp);
601 #define OFFSET(x) offsetof(struct gdigrab, x)
602 #define DEC AV_OPT_FLAG_DECODING_PARAM
624 .priv_data_size =
sizeof(
struct gdigrab),