您的位置:首页 > 编程语言 > Delphi

Delphi中一个获得窗口图象的函数

2010-09-15 17:35 375 查看
过程名: CreateBMPFromWindow
功能: 获得一个句柄为Hwnd的窗口的图象
参数: Hwnd: 窗体句柄
PicBitNum: 图象位数(15,16,24,32)
返回值: TBitmap

function CreateBMPFromWindow(Hwnd: THandle;PicBitNum: Byte): TBitmap;
{创建一个空的Bitmap}
function CreateDibBMP(Dc: HDC;width,height: Integer): HBITMAP;
type
DibRec = record
bi: BITMAPINFOHEADER;
ct: array[0..255] of DWORD;
end;
var
Dib: DibRec;
lpBits: pointer;
temp: TBitmapInfo;
begin
dib.bi.biSize := sizeof(BITMAPINFOHEADER);
dib.bi.biWidth := width;
dib.bi.biHeight := height;
dib.bi.biBitCount := PicBitNum;
dib.bi.biPlanes := 1;
dib.bi.biCompression := 0;
dib.bi.biSizeImage := 0;
dib.bi.biClrUsed := 0;

if PicBitNum = 15 then
Dib.bi.biBitCount := 16
else if PicBitNum = 16 then
begin
Dib.bi.biCompression := BI_BITFIELDS;
dib.ct[0] := $F800;
dib.ct[1] := $07E0;
dib.ct[2] := $001F;
end;
Move(dib,temp,SizeOf(dib));
result := CreateDIBSection(Dc,temp,DIB_RGB_COLORS,lpBits,0,0);
end;
var
rc: TRect;
x,y,cx,cy: integer;
hdcScreen,hdcMemory: HDC;
begin
GetWindowRect(hwnd, rc);
x := rc.left;
y := rc.top;
cx := rc.right - rc.left;
cy := rc.bottom - rc.top;
hdcScreen := GetDC(0);
hdcMemory := CreateCompatibleDC(0);
result := TBitmap.Create;
Result.Handle := CreateDibBMP(hdcScreen, cx, cy);
SelectObject(hdcMemory, result.Handle);
BitBlt(hdcMemory, 0, 0, cx, cy, hdcScreen, x, y, SRCCOPY);
DeleteDC(hdcMemory);
ReleaseDC(0, hdcScreen);
end;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐