unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Timer1: TTimer;
Button1: TButton;
procedure Timer1Timer(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
function EnumWindowsProc(AhWnd:LongInt;AForm:TForm1):boolean;stdcall;
Function PrintWindow(SourceWindow: hwnd; Destination: hdc; nFlags: cardinal): bool; stdcall; external 'user32.dll' name 'PrintWindow';
implementation
{$R *.dfm}
function EnumWindowsProc(AhWnd:LongInt;AForm:TForm1):boolean;
var
lpszWindowText:array[0..254] of char;
Bitmap: TBitmap;
rec : TRect;
//x, y : Integer;
begin
GetWindowText(AhWnd,lpszWindowText,254);
if pos('百度搜索',StrPas(lpszWindowText))>0 then
begin
GetWindowRect(AhWnd, rec); //获取到窗口的矩形
Bitmap := TBitmap.Create;
try
Bitmap.Width := rec.Right - rec.Left;
Bitmap.Height := rec.Bottom - rec.Top;
Bitmap.PixelFormat := pf24bit;
PrintWindow(AhWnd, Bitmap.Canvas.Handle, 0);
Bitmap.SaveToFile(FormatDatetime('yyyymmddhhnnss', Now())+'led.bmp');
finally
Bitmap.Free;
end;
end;
Result:=True;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
EnumWindows(@EnumWindowsProc,LongInt(self));
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Timer1Timer(nil);
end;
end.
定时截图,为什么有些图片会出现下半截黑色。甚至会有全部黑色的。如何解决?请各位高手帮忙解决。谢谢。
------解决方案--------------------------------------------------------
timer定时多少,是不是一个间隔没执行完,后一个又来了
------解决方案--------------------------------------------------------
图片没来得及处理完而已,把定时加长,不建议用timer来做,可以用线程加临界区,另外bmp比较大用jpg存比较好
------解决方案--------------------------------------------------------
既然能打开部分图像,那么截图代码应该是没有问题的,应该时间设置不够导致图片不能加载完成