• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

用Delphi实现抓屏

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
//调用方法
procedure TForm4.BitBtn1Click(Sender: TObject);
var
  tBM : TBitmap;
begin
  tBM :
= TBitmap.Create;
  ScreenShot(
0,0,Screen.Width,Screen.height,tBM);
  tBM.SaveToFile(
'.\ScreenShot.BMP');
  tBm.FreeImage;
  FreeAndNil(tBM);
  image1.Picture.LoadFromFile(
'.\screenshot.bmp');
end;

//抓屏函数
procedure TForm4.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;

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
delphi中的IntToHex()发布时间:2022-07-18
下一篇:
delphi程序热键发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap