#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;
}
用PrintWindow将当前窗体打印复制到剪切板中
最新推荐文章于 2024-06-15 06:54:38 发布