一个抓屏的函数 delphi

本文介绍了一个用于屏幕截图的函数,该函数可以捕获指定区域的屏幕内容并保存为位图文件。通过两个按钮触发,一个用于截图并显示图片,另一个用于关闭程序。

 一个抓屏的函数,在其它Blog上看到的

ContractedBlock.gifExpandedBlockStart.gifCode
unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;

type
  TMainFrm 
= class(TForm)
    Button1: TButton;
    Button2: TButton;
    Image1: TImage;
    
procedure Button1Click(Sender: TObject);
    
procedure Button2Click(Sender: TObject);
  
private
    
{ Private declarations }
    
procedure ScreenShot(x : integer; y : integer; Width : integer; Height : integer; bm : TBitMap);
  
public
    
{ Public declarations }
  
end;

var
  MainFrm: TMainFrm;

implementation

{$R *.dfm}

//调用方法
procedure TMainFrm.Button1Click(Sender: TObject);
var
  tBM : TBitmap;
begin
  
try
    tBM :
= TBitmap.Create;
    ScreenShot(
0,0,Screen.Width,Screen.height,tBM);
    tBM.SaveToFile(
'c:\ScreenShot.BMP');
    Image1.Picture.Bitmap :
= tBM;
    
//image1.Picture.LoadFromFile('c:\screenshot.bmp');
  
finally
    tBm.FreeImage;
    FreeAndNil(tBM);
  
end;
end;

procedure TMainFrm.Button2Click(Sender: TObject);
begin
  Self.Close;
end;

procedure TMainFrm.ScreenShot(x : integer; y : integer; Width : integer; Height : integer; bm : TBitMap);
var
dc: HDC; lpPal : PLOGPALETTE;
begin
  
// 检测所需抓屏的区域
  
if ((Width = 0or (Height = 0)) then exit;
  bm.Width :
= Width;
  bm.Height :
= Height;
  
//获取设备上下文
  dc :
= GetDc(0);
  
if (dc = 0then 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 <> 0then
    
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;

end.

 

转载于:https://www.cnblogs.com/fubin/archive/2009/05/27/1491178.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值