将窗口内容显示为位图

时间:2014-07-15 04:11:36

标签: c++ winapi gdi+

我想拍摄窗口内容的图像并在该窗口中将其显示为较小的位图...我按照这篇文章:http://msdn.microsoft.com/en-us/library/dd183402(v=vs.85).aspx当我想要获取整个桌面的截图时 - 它的工作原理很好......问题是当我试图仅获取窗口内容的位图时。我有什么想法吗?

这是我的代码:

HDC hDC;
HDC hDCMemDC = NULL;
HBITMAP hbmWindow = NULL;
BITMAP bmpWindow;

hDC = GetDC(hWnd);

hDCMemDC = CreateCompatibleDC(hDC);

RECT clientRect;
GetClientRect(hWnd, &clientRect);

hbmWindow = CreateCompatibleBitmap(hDC, clientRect.right - clientRect.left, clientRect.bottom - clientRect.top);

SelectObject(hDCMemDC, hbmWindow);

BitBlt(hDCMemDC, 0, 0, 100, 100, hDC, 0, 0, SRCCOPY);

由于

2 个答案:

答案 0 :(得分:1)

void DrawSelf(HDC Context, RECT Area, RECT NewArea)
{
    uint32_t W = Area.right - Area.left;
    uint32_t H = Area.bottom - Area.top;
    uint32_t NW = NewArea.right - NewArea.left;
    uint32_t NH = NewArea.bottom - NewArea.top;

    StretchBlt(Context, NewArea.left, NewArea.top, NW, NH, Context, Area.left, Area.top, W, H, SRCCOPY);
}

然后你可以这样做:

RECT Area;
RECT Area2;
HDC DC = GetDC(hwnd);  //Gets the client area only.. Use GetWindowDC for the whole window including the title-bar.
GetClientRect(hwnd, &Area); //client area only.
GetClientRect(hwnd, &Area2);

//Smaller area in which to draw.
Area2.left += 5;
Area2.right -= 5;
Area2.top += 5;
Area2.bottom -= 5;


DrawSelf(DC, Area, Area2);

ReleaseDC(hwnd, dc);

答案 1 :(得分:0)

使用GetWindowDC而不是GetDC来获取整个窗口区域。