#include <Windows.h> #include <gdiplus.h> #include <stdio.h> const Gdiplus::GdiplusStartupInput kGdiplusStartupInput; const CLSID kPngEncoderId = { 0x557CF406, 0x1A04, 0x11D3, { 0x9A, 0x73, 0x00, 0x00, 0xF8, 0x1E, 0xF3, 0x2E } }; int main() { ULONG_PTR hGdiplus; if (Gdiplus::GdiplusStartup(&hGdiplus, &kGdiplusStartupInput, nullptr) != Gdiplus::Status::Ok) { puts("GdiplusStartup() fail."); exit(1); } HWND hwd; scanf("%p", &hwd); if (!SetForegroundWindow(hwd)) { puts("SetForegroundWindow() fail."); exit(1); } HDC hdc = GetDC(hwd); if (!hdc) { puts("GetDC() fail."); exit(1); } HDC hdd = CreateCompatibleDC(hdc); if (!hdd) { puts("CreateCompatibleDC() fail."); exit(1); } RECT rt; if (!GetWindowRect(hwd, &rt)) { puts("GetWindowRect() fail."); exit(1); } int cx = rt.right - rt.left; int cy = rt.bottom - rt.top; HBITMAP hbm = CreateCompatibleBitmap(hdc, cx, cy); if (!hbm) { puts("CreateCompatibleBitmap() fail."); exit(1); } SelectObject(hdd, hbm); if (!PrintWindow(hwd, hdd, 0)) { puts("PrintWindow() fail."); exit(1); } Gdiplus::Bitmap bitmap(hbm, nullptr); if (bitmap.Save(L"Screen Shot.png", &kPngEncoderId) != Gdiplus::Status::Ok) { puts("Bitmap::Save() fail."); exit(1); } DeleteDC(hdd); DeleteDC(hdc); Gdiplus::GdiplusShutdown(hGdiplus); }