0

Here's my objective: a) capture the device context for the entire screen; b) capture a background window device context (including the title bar); c) draw the desktop content on a buffer device context; d) draw the captured window content on the same buffer device context; e) display the drawn device context on a top window, simulating that the desired window is on top.

The issue is that the title bar isn't drawn on the new window, even though the GetWindowRect function seems to be working fine. That's because some other stuff is drawn instead of the title bar (usually the content of windows on the background when the target window is created).

This is my code right now:

case __SourceDC:
{
    HDC desktop_dc = GetDC(NULL);
    HWND target_hwnd = FindWindowA(NULL, "Everything");
    HDC target_dc = GetWindowDC(target_hwnd);
    RECT rc{};
    GetWindowRect(target_hwnd, &rc);
    RECT clrc{};
    GetClientRect(target_hwnd, &clrc);
    int width = rc.right - rc.left;
    int height = rc.bottom - rc.top;
    int x_dst = ((double)rc.left / SCREEN_WIDTH) * m_area.width;
    int y_dst = ((double)rc.top / SCREEN_HEIGHT) * m_area.height;
    int w_dst = ((double)width / SCREEN_WIDTH) * m_area.width;
    int h_dst = ((double)height / SCREEN_HEIGHT) * m_area.height;
    SetStretchBltMode(hdc, HALFTONE);
    StretchBlt(hdc, 0, 0, m_area.width, m_area.height, desktop_dc, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, SRCCOPY);
    StretchBlt(hdc, x_dst, y_dst, w_dst, h_dst, target_dc, 0, 0, width, height, SRCCOPY);
    ReleaseDC(target_hwnd, target_dc);
    ReleaseDC(NULL, desktop_dc);
}
break;

All the desktop content and the window content is correctly drawn, except for the title bar (and apparently the borders as well).enter image description here

As you can see from the image, the myWnd window shows the desktop content, but the title bar is not displayed for the target window. Also, nothing changes when the target window is the foreground window.

What am I missing here? Is this behavior expected? Is StretchBlt working as intended by not capturing the content of the title bar?

Thanks in advance.

Emanah
  • 56
  • 4
  • We don't know what `m_area` is. Please show a [mcve]. – IInspectable Sep 15 '22 at 12:45
  • Oh, sorry. Didn't think that was relevant for the problem. `m_area` is just a struct that holds the info about the window where the content should be drawn (x position, y position, width and height). Also, `GetClientRect` is just there to compare the results with `GetWindowRect` when debugging, and that shows that both contain different values, as expected. – Emanah Sep 15 '22 at 12:51
  • You forgot to include the full source code to reproduce the problem. – mnistic Sep 15 '22 at 14:01
  • [It's expected](https://stackoverflow.com/a/72286374/15511041). Using [Windows.Graphics.Capture](https://github.com/microsoft/Windows.UI.Composition-Win32-Samples/tree/master/cpp/ScreenCaptureforHWND) if possible. – YangXiaoPo-MSFT Sep 16 '22 at 07:48

0 Answers0