调用WindowsApi,使用Com组件,我也是借鉴加修改的,因为网上直接复制下来的并不能直接调用(HBITMAP这种东西就比较奇怪),以后当成工具调用就可以了,写了部分注释,就不一句句讲解了。
GetThumbImage()是用来调用截图的ScreenCapture类的,可以指定保存的文件名,截图的范围。
TBitmap* __fastcall TForm3::GetThumbImage()
{
TImage *img = NULL;
TBitmap *bitmap = new TBitmap();
HBITMAP hBitmap = NULL;
LPSTR addr = "ScreenCapture.png";
hBitmap = ScreenCapture(addr, 32, NULL);
::GetObject(hBitmap, sizeof(bitmap), (LPSTR)&bitmap);
bitmap->LoadFromFile(addr);
return bitmap;
}
HBITMAP TForm3::ScreenCapture(LPSTR filename ,WORD BitCount,LPRECT lpRect)
{
HBITMAP hBitmap;
// 显示器屏幕DC
HDC hScreenDC = CreateDC(L"DISPLAY", NULL, NULL, NULL);
HDC hmemDC = CreateCompatibleDC(hScreenDC);
// 显示器屏幕的宽和高
int ScreenWidth = GetDeviceCaps(hScreenDC, HORZRES);
int ScreenHeight = GetDeviceCaps(hScreenDC, VERTRES);
// 旧的BITMAP,用于与所需截取的位置交换
HBITMAP hOl