您现在的位置:首页 >> 图形媒体 >> 图形媒体 >> 内容

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

时间:2011/9/3 15:01:37 点击:

  核心提示:过程名:CreateBMPFromWindow功能:获得一个句柄为Hwnd的窗口的图象参数:Hwnd:窗体句柄 PicBitNum:图象位数(15,16,24,32)返回值:TBitmapfuncti...

过程名:    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;

作者:不得闲 来源:转载
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
本类推荐
  • 没有
本类固顶
  • 没有
  • 盒子文章(www.2ccc.com) © 2024 版权所有 All Rights Reserved.
  • 沪ICP备05001939号