code fragment-Windows截图

来源:http://bbs.pediy.com/showthread.php?p=1212276#post1212276


using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{RECT rc;
   HWND hwnd = FindWindow(TEXT("Notepad"), NULL); //注意窗口不能最小化  
   if (hwnd == NULL)
   { 
      cout << "找不到记事本窗口" << endl;
      return 0;
   }
   GetClientRect(hwnd, &rc);
 
  //创建 
   HDC hdcScreen = GetDC(NULL);
   HDC hdc = CreateCompatibleDC(hdcScreen);
   HBITMAP hbmp = CreateCompatibleBitmap(hdcScreen, rc.right - rc.left, rc.bottom - rc.top);   
   SelectObject(hdc, hbmp); 
 
   //复制 
   PrintWindow(hwnd, hdc, PW_CLIENTONLY);
  //PW_CLIENTONLY:Only the client area of the window is copied to hdcBlt. 
  //By default, the entire window is copied.   
  //PW_CLIENTONLY表示仅仅拷贝窗口的客户区域,而默认情况下,执行printwindow会拷贝整个窗口 
 
  //复制到粘贴板  
   OpenClipboard(NULL);
   EmptyClipboard(); 
   SetClipboardData(CF_BITMAP, hbmp); 
   CloseClipboard(); 
 
  //释放 
   DeleteDC(hdc); 
   DeleteObject(hbmp); 
   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、付费专栏及课程。

余额充值