【Demo 0048】截取窗体背景及控件界面
本例将提炼了Demo0049显示控件中的方法,单独演示如何截取窗体的背景以及仅截取窗体的控件界面图;
一、代码演示
仅截取窗体中控件的效果图
SendMessage(hWnd, WM_PRINT, (WPARAM)hDCMem, PRF_CHILDREN|PRF_CLIENT|PRF_ERASEBKGND|PRF_NONCLIENT|PRF_OWNED);
截取窗体上所有元素(背影和控件)的效果图
BitBlt(hDCMem, 0, 0, rtWnd.right - rtWnd.left, rtWnd.bottom - rtWnd.top, hDC, 0, 0, SRCCOPY);
//////////////////////////////////////////////////////////////////////////
HBITMAP _CaptureWindow(HWND hWnd, bool bCaptureCtrl)
{
RECT rtWnd;
GetWindowRect(hWnd, &rtWnd);
HDC hDC = GetDC(hWnd);
HDC hDCMem = CreateCompatibleDC(hDC);
HBITMAP hBitmap = CreateCompatibleBitmap(hDC, rtWnd.right - rtWnd.left, rtWnd.bottom - rtWnd.top);
HGDIOBJ hOld = SelectObject(hDCMem, hBitmap);
if (bCaptureCtrl)
{
SendMessage(hWnd, WM_PRINT, (WPARAM)hDCMem, PRF_CHILDREN|PRF_CLIENT|PRF_ERASEBKGND|PRF_NONCLIENT|PRF_OWNED);
} else {
BitBlt(hDCMem, 0, 0, rtWnd.right - rtWnd.left, rtWnd.bottom - rtWnd.top, hDC, 0, 0, SRCCOPY);
}
SelectObject(hDCMem, hOld);
DeleteObject(hDCMem);
ReleaseDC(hWnd, hDC);
return hBitmap;
}
HBITMAP _CaptureWindow(HWND hWnd, bool bCaptureCtrl)
{
RECT rtWnd;
GetWindowRect(hWnd, &rtWnd);
HDC hDC = GetDC(hWnd);
HDC hDCMem = CreateCompatibleDC(hDC);
HBITMAP hBitmap = CreateCompatibleBitmap(hDC, rtWnd.right - rtWnd.left, rtWnd.bottom - rtWnd.top);
HGDIOBJ hOld = SelectObject(hDCMem, hBitmap);
if (bCaptureCtrl)
{
SendMessage(hWnd, WM_PRINT, (WPARAM)hDCMem, PRF_CHILDREN|PRF_CLIENT|PRF_ERASEBKGND|PRF_NONCLIENT|PRF_OWNED);
} else {
BitBlt(hDCMem, 0, 0, rtWnd.right - rtWnd.left, rtWnd.bottom - rtWnd.top, hDC, 0, 0, SRCCOPY);
}
SelectObject(hDCMem, hOld);
DeleteObject(hDCMem);
ReleaseDC(hWnd, hDC);
return hBitmap;
}
二、效果
左图为显示窗体, 右图为原窗体(仅截取窗体中控件)
左图为显示窗体, 右图为原窗体(截取窗体中控件以及窗体的背影)