zoukankan      html  css  js  c++  java
  • Windows窗口截图

    #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);
    }
  • 相关阅读:
    枚举类型(C#)
    如何在Delphi中安装组件
    操作系统知识点总结
    Java内部类学习笔记
    计算机网络笔试面试常考考点
    电话号码分身问题
    最长下降/上升子序列问题
    LeetCode(162):Find Peak Element
    LeetCode(153):Find Minimum in Rotated Sorted Array
    LeetCode(75):Sort Colors
  • 原文地址:https://www.cnblogs.com/JebediahKerman/p/12731778.html
Copyright © 2011-2022 走看看