PrintWindow function makes mistake in notepad.exe
See the question and my original answer on StackOverflowYou only get the client area. This code gets the whole window:
HWND hwnd = FindWindow(programName, nullptr);
if (hwnd == nullptr)
return -1;
WINDOWINFO wi;
wi.cbSize = sizeof(WINDOWINFO);
GetWindowInfo(hwnd, &wi);
const LONG w = wi.rcWindow.right - wi.rcWindow.left;
const LONG h = wi.rcWindow.bottom - wi.rcWindow.top;
HDC hdcScreen = GetDC(nullptr);
HDC hdc = CreateCompatibleDC(hdcScreen);
HBITMAP hbmp = CreateCompatibleBitmap(hdcScreen, w, h);
SelectObject(hdc, hbmp);
PrintWindow(hwnd, hdc, 0); // or use PW_RENDERFULLCONTENT
BitBlt(hdc, 0, 0, w, h, hdc, 0, 0, SRCCOPY);
OpenClipboard(nullptr);
EmptyClipboard();
SetClipboardData(CF_BITMAP, hbmp);
CloseClipboard();
DeleteDC(hdc);
DeleteObject(hbmp);
ReleaseDC(nullptr, hdcScreen);
return 0;