DELPHI 抓屏程序

方法一: 抓屏函数   

参数说明: 前面是个是坐标,横坐标,纵坐标,长,宽 后面是将抓到的屏幕转换为 BMP图片

procedure ScreenShot(x: integer; y: integer; Width: integer; Height: integer; bm: TBitMap);

var
  dc: HDC;
  lpPal: PLOGPALETTE;
begin
  // 检测所需抓屏的区域
  if ((Width = 0) or (Height = 0)) then
    exit;
  bm.Width := Width;
  bm.Height := Height;
  // 获取设备上下文
  dc := GetDc(0);
  if (dc = 0) then
    exit;
  { do we have a palette device? }
  if (GetDeviceCaps(dc, RASTERCAPS) AND RC_PALETTE = RC_PALETTE) then
  begin
    { allocate memory for a logical palette }
    GetMem(lpPal, sizeof(TLOGPALETTE) + (255 * sizeof(TPALETTEENTRY)));
    { zero it out to be neat }
    FillChar(lpPal^, sizeof(TLOGPALETTE) + (255 * sizeof(TPALETTEENTRY)), #0);
    { fill in the palette version }
    lpPal^.palVersion := $300;
    { grab the system palette entries }
    lpPal^.palNumEntries := GetSystemPaletteEntries(dc, 0, 256, lpPal^.palPalEntry);
    if (lpPal^.palNumEntries <> 0) then
    begin
      { create the palette }
      bm.Palette := CreatePalette(lpPal^);
    end;
    FreeMem(lpPal, sizeof(TLOGPALETTE) + (255 * sizeof(TPALETTEENTRY)));
  end;
  { copy from the screen to the bitmap }
  BitBlt(bm.Canvas.Handle, 0, 0, Width, Height, dc, x, y, SRCCOPY);
  { release the screen dc }
  ReleaseDc(0, dc);
end;

 

 

方法调用 

    try
    FBMP := TBitMap.Create;
    ScreenShot(0, 0, Screen.Width, Screen.Height, FBMP);
    FBMP.SaveToFile('E:\a.bmp');

    finally
    FBMP.FreeImage;
    FreeAndNil(FBMP);
    end;

---------------

 

 

 

方法二

var
  // FBMP: TBitMap;

  winHWND, hCur: integer;
  winDC: integer;
  rect: TRect;
  // AFormat : Word;
  // AData,APalette : THandle;
  pt: TPoint;
  fBitmap: TBitMap;
  timer11: integer;
begin

  timer11 := GetTickCount;
  hCur := GetCursor(); // 获 得 光 标 句 柄
  GetCursorPos(pt); // 记 录 光 标 位 置
  winHWND := GetDesktopWindow();
  winDC := GetDc(winHWND);
  GetWindowRect(winHWND, rect);
  fBitmap := TBitMap.Create;
  fBitmap.Width := rect.right - rect.left;
  fBitmap.Height := rect.bottom - rect.top;
  BitBlt(fBitmap.Canvas.Handle, 0, 0, fBitmap.Width, fBitmap.Height, winDC, 0, 0, SRCCOPY);
  DrawIcon(fBitmap.Canvas.Handle, pt.x, pt.y, hCur); // 画 光 标
  ReleaseDc(winHWND, winDC);
  fBitmap.SaveToFile('E:\a.bmp');
  fBitmap.Free;
  Caption := inttostr(GetTickCount - timer11);

 

 

 

 

 

posted @ 2010-06-23 23:36  坚持Delphi  阅读(2695)  评论(0)    收藏  举报