Windows取得屏幕指定位置和尺寸的图像并绘制到任何位置

来源:互联网 发布:线性回归算法求解 编辑:程序博客网 时间:2024/05/18 12:40

//Windows取得屏幕指定位置和尺寸的图像并绘制到任何位置

#include "stdafx.h"
#include <windows.h>

BOOL MyDrawBitmap(HDC hdc,HBITMAP hBitmap,int x,int y)
{
BOOL bResult = FALSE;

HDC hMemDC;
HBITMAP hOldBitmap;
BITMAP bm;

GetObject(hBitmap,sizeof(BITMAP),&bm);

hMemDC = CreateCompatibleDC(hdc);
hOldBitmap = (HBITMAP)SelectObject(hMemDC,hBitmap);

bResult = BitBlt(hdc,x,y,bm.bmWidth,bm.bmHeight,hMemDC,0,0,SRCCOPY);

SelectObject(hMemDC,hOldBitmap);
DeleteDC(hMemDC);

return bResult;
}

HBITMAP GetScreenBitmap(const PRECT rect)
{
HDC hdc = GetDC(NULL);

HDC hMemDC = CreateCompatibleDC(hdc);
HBITMAP hMemBmp = CreateCompatibleBitmap(hdc,rect->right-rect->left,rect->bottom-rect->top);
HBITMAP hOldMemBmp = (HBITMAP)SelectObject(hMemDC,hMemBmp);

BitBlt(hMemDC,0,0,rect->right-rect->left,rect->bottom-rect->top,hdc,rect->left,rect->top,SRCCOPY);

SelectObject(hMemDC,hOldMemBmp);
DeleteDC(hMemDC);

ReleaseDC(NULL,hdc);

return hMemBmp;
}

int main()
{
RECT rect = {0,0,200,200};
HBITMAP hBitmap = GetScreenBitmap(&rect);
HDC hdc = GetDC(NULL);
MyDrawBitmap(hdc,hBitmap,300,300);
ReleaseDC(NULL,hdc);
DeleteObject(hBitmap);

    return 0;
}

原创粉丝点击