How to get a bitmap from a invisible source window?  
Author Message
Jakein2006





PostPosted: Visual C++ General, How to get a bitmap from a invisible source window? Top

I use the following function to get a bitmap from a invisible source window, but everytime it seems that the bitmap I get from that window is not repaint completely, please tell me how to make the following code better to get a refreshing bitmap of a invisible source window:

I appreciate your help.

CopyToClipboard(HWND hSrcWnd)
{
int nResult = FALSE;

if (hSrcWnd )
{
// can we do this with 1 bitmap
HDC hDCMem = ::CreateCompatibleDC(NULL);
HDC hDCMem2 = ::CreateCompatibleDC(NULL);

if ( hDCMem && hDCMem2)
{
CRect rect;
nResult = ::GetClientRect( hSrcWnd, &rect);
HDC hDC = ::GetDC(hSrcWnd);

if ( hDC)
{
HBITMAP hBmp = NULL;
HBITMAP hBmp2 = NULL;
hBmp = ::Create24BPPDIBSection( hDC, rect.Width(), rect.Height());
hBmp2 = ::Create24BPPDIBSection( hDC, rect.Width() - TOTAL_OFFSETX, rect.Height() - TOTAL_OFFSETY);

if ( hBmp && hBmp2)
{
HGDIOBJ hOld = ::SelectObject( hDCMem, hBmp);
HGDIOBJ hOld2 = ::SelectObject( hDCMem2, hBmp2);
nResult = ::SendMessage(hSrcWnd, WM_PRINT, (WPARAM) hDCMem, PRF_CHILDREN | PRF_CLIENT | PRF_ERASEBKGND );
nResult = ::BitBlt( hDCMem2, 0,0, rect.Width() - TOTAL_OFFSETX, rect.Height() - TOTAL_OFFSETY,
hDCMem, TOPLEFT_OFFSETX, TOPLEFT_OFFSETY, SRCCOPY);




::SelectObject( hDCMem, hOld);
::DeleteObject( hDCMem);
::SelectObject( hDCMem2, hOld2);
::DeleteObject( hDCMem2);

nResult = ::OpenClipboard( hSrcWnd);
if ( nResult)
{
nResult = ::EmptyClipboard();
HANDLE hData = ::SetClipboardData( CF_BITMAP, hBmp2);
nResult = ::CloseClipboard();
nResult = hData TRUE : FALSE;
}
::DeleteObject( hBmp);

} // if ( hBmp && hBmp2)
::ReleaseDC(hSrcWnd, hDC);
} // if ( hDC)
} // if ( hDCMem && hDCMem2)
}
return nResult;

}




Visual C++6  
 
 
einaros





PostPosted: Visual C++ General, How to get a bitmap from a invisible source window? Top

I use the following function to get a bitmap from a invisible source window, but everytime it seems that the bitmap I get from that window is not repaint completely, please tell me how to make the following code better to get a refreshing bitmap of a invisible source window:

AFAIK there's no guarantee that an invisible window contains a fully upated bitmap. To force the drawing of invisible content you can use some trickery involving PrintWindow, but do know that it isn't trivial if the target window doesn't handle wm_print. I recall an article being available on the matter at http://www.fengyuan.com/article/wmprint.html. Have a look at that.

Note that UI an GDI questions aren't at home in this forum. While many are being helped, it's generally considered off topic. For further similar queries, visit e.g. the GDI newsgroup at http://msdn.microsoft.com/newsgroups/default.aspx dg=microsoft.public.win32.programmer.gdi&lang=en&cr=US.