获取指定窗口界面到图片 同时 保存到剪切板



class YGetWndToCompatibleBitmap
{
public:
YGetWndToCompatibleBitmap()
{
m_hWndBitmap = NULL;
}


~YGetWndToCompatibleBitmap()
{
if ( m_hWndBitmap )
{
DeleteObject( m_hWndBitmap );
m_hWndBitmap = 0;
}
}


public:
HBITMAP m_hWndBitmap;


HBITMAP  getWndBitmap( HWND hWnd, CRect rectWnd )
{
m_hWndBitmap = getWndToCompatibleBitmap( hWnd, rectWnd );


return m_hWndBitmap;

 

private: 

HBITMAP & getWndToCompatibleBitmap( HWND hWnd, CRect rectWnd )
{
//msdn:: Capturing an Image;
// Remarks
// A memory DC exists only in memory. When the memory DC is created, its display surface is exactly one monochrome pixel wide and one monochrome pixel high. Before an application can use a memory DC for drawing operations, it must select a bitmap of the correct width and height into the DC. To select a bitmap into a DC, use the CreateCompatibleBitmap function, specifying the height, width, and color organization required. 
// 
// When a memory DC is created, all attributes are set to normal default values. The memory DC can be used as a normal DC. You can set the attributes; obtain the current settings of its attributes; and select pens, brushes, and regions.
// 
// The CreateCompatibleDC function can only be used with devices that support raster operations. An application can determine whether a device supports these operations by calling the GetDeviceCaps function. 
// 
// When you no longer need the memory DC, call the DeleteDC function. 
// Remarks
// The color format of the bitmap created by the CreateCompatibleBitmap function matches the color format of the device identified by the hdc parameter. This bitmap can be selected into any memory device context that is compatible with the original device. 
// 
// Because memory device contexts allow both color and monochrome bitmaps, the format of the bitmap returned by the CreateCompatibleBitmap function differs when the specified device context is a memory device context. However, a compatible bitmap that was created for a nonmemory device context always possesses the same color format and uses the same color palette as the specified device context. 
// 
// Note: When a memory device context is created, it initially has a 1-by-1 monochrome bitmap selected into it. If this memory device context is used in CreateCompatibleBitmap, the bitmap that is created is a monochrome bitmap. To create a color bitmap, use the hDC that was used to create the memory device context, as shown in the following code:
// 
// HDC memDC = CreateCompatibleDC ( hDC );
// HBITMAP memBM = CreateCompatibleBitmap ( hDC );
// SelectObject ( memDC, memBM );
// If an application sets the nWidth or nHeight parameters to zero, CreateCompatibleBitmap returns the handle to a 1-by-1 pixel, monochrome bitmap.
// 
// If a DIB section, which is a bitmap created by the CreateDIBSection function, is selected into the device context identified by the hdc parameter, CreateCompatibleBitmap creates a DIB section.
// 
// When you no longer need the bitmap, call the DeleteObject function to delete it. 
// 
 

HWND hWndTemp = NULL;
CRect rtCutting(0,0,0,0); 


if ( hWnd ){ hWndTemp = hWnd; }else{ hWndTemp = ::GetDesktopWindow(); } 
 
if ( rectWnd.Width() > 0 && rectWnd.Height() > 0 ){ rtCutting = rectWnd; }else{ ::GetClientRect( hWndTemp, &rtCutting ); }   


//capture wnd;
HDC hDC = ::GetDC( hWndTemp ) ;  //hWnd : [in] Handle to the window whose DC is to be retrieved. If this value is NULL, GetDC retrieves the DC for the entire screen. 


static HBITMAP hBitmap = NULL;


{
HDC hMemDC = CreateCompatibleDC(hDC); 


hBitmap = CreateCompatibleBitmap(hDC, rtCutting.Width(), rtCutting.Height());


/*HBITMAP hOldBitmap = */(HBITMAP)SelectObject(hMemDC, hBitmap);


StretchBlt( hMemDC, 0, 0, rtCutting.Width(), rtCutting.Height(), hDC, rtCutting.left , rtCutting.top , rtCutting.Width(), rtCutting.Height(), SRCCOPY );  


DeleteDC(hMemDC); 




if( 0 && hBitmap != NULL && OpenClipboard(hWnd))
{
EmptyClipboard();
SetClipboardData(CF_BITMAP,  hBitmap);
CloseClipboard();
}

 
::ReleaseDC( hWndTemp, hDC );


return hBitmap;


}
 


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chinabinlang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值