用PrintWindow将当前窗体打印复制到剪切板中

#define _WIN32_WINNT    0x0501        //仅XP或以上系统有效
#include <windows.h>

int main()

    HWND hwnd = FindWindow(TEXT("Notepad"), NULL);    //注意窗口不能最小化
    if (hwnd == NULL)
    {
        cout << "找不到记事本窗口" << endl;
        return 0;
    }

    //获取窗口信息
    WINDOWINFO wi;
    wi.cbSize = sizeof(WINDOWINFO);
    GetWindowInfo(hwnd, &wi);

    //计算客户区矩形
    RECT rc = {
        wi.rcClient.left - wi.rcWindow.left, 
        wi.rcClient.top - wi.rcWindow.top, 
        wi.rcClient.right - wi.rcWindow.left, 
        wi.rcClient.bottom - wi.rcWindow.top
    };

    //创建
    HDC hdcScreen = GetDC(NULL);
    HDC hdc = CreateCompatibleDC(hdcScreen);
    HBITMAP hbmp = CreateCompatibleBitmap(hdcScreen, 
        wi.rcWindow.right - wi.rcWindow.left, 
        wi.rcWindow.bottom - wi.rcWindow.top);
    SelectObject(hdc, hbmp);

    HBITMAP hbmp2 = CreateCompatibleBitmap(hdcScreen, 
        wi.rcClient.right - wi.rcClient.left, 
        wi.rcClient.bottom - wi.rcClient.top);
    HDC hdc2 = CreateCompatibleDC(hdcScreen);
    SelectObject(hdc2, hbmp2);

    //复制整个窗口
    PrintWindow(hwnd, hdc, 0);

    //复制客户区
    BitBlt(hdc2, 
        0, 0, rc.right - rc.left, rc.bottom - rc.top, 
        hdc, 
        rc.left, rc.top, 
        SRCCOPY);

    //复制到粘贴板
    OpenClipboard(NULL);
    EmptyClipboard();
    SetClipboardData(CF_BITMAP, hbmp2);
    CloseClipboard();
    
    //释放
    DeleteDC(hdc);
    DeleteObject(hbmp);

    DeleteDC(hdc2);
    DeleteObject(hbmp2);

    ReleaseDC(NULL, hdcScreen);

    cout << "成功把记事本窗口复制到粘贴板,请粘贴到Windows画图工具" << endl;
    
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值