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;
}
};