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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值