Delphi活动窗口截图 [英] Delphi Active Window Screenshot
问题描述
我正在尝试使用此代码添加活动窗口的捕获屏幕快照
I am trying to add capture screenshot of active window using this code
procedure ScreenShot(activeWindow: bool; destBitmap : TBitmap) ;
var
w,h : integer;
DC : HDC;
hWin : Cardinal;
r : TRect;
begin
if activeWindow then
begin
hWin := GetForegroundWindow;
dc := GetWindowDC(hWin) ;
GetWindowRect(hWin,r) ;
w := r.Right - r.Left;
h := r.Bottom - r.Top;
end
else
begin
hWin := GetForegroundWindow;
dc := GetDC(hWin) ;
w := GetDeviceCaps (DC, HORZRES) ;
h := GetDeviceCaps (DC, VERTRES) ;
end;
try
destBitmap.Width := w;
destBitmap.Height := h;
BitBlt(destBitmap.Canvas.Handle,
0,
0,
destBitmap.Width,
destBitmap.Height,
DC,
0,
0,
SRCCOPY) ;
finally
ReleaseDC(hWin, DC) ;
end;
end;
在Button1中,我使用:
And in Button1 i use :
var
path:string;
b:TBitmap;
begin
path:= ExtractFilePath(Application.ExeName) + '/Screenshot/';
b := TBitmap.Create;
try
ScreenShot(TRUE, b) ;
b.SaveToFile(path + 'Screenshot_1.png');
finally
b.FreeImage;
FreeAndNil(b) ;
end;
end;
这很好,唯一的问题是它没有捕获标题栏:(
it works good only problem is it not capture title bar :(
这里是完全活动的窗口视图:
Here is Full Active window view :
这是我从该代码中获得的信息
And Here is what i get from that code
我在哪里做错了???
Where am i doing wrong ??
推荐答案
我已经测试并获得了相同的结果。
I have tested and got the same result.
< img src = https://i.stack.imgur.com/TM2O9.jpg alt =在此处输入图片描述>
带有边框的原始图片
但是如果您设置
sSkinProvider1.AllowExtBorders:=False;
您将获得不带透明圆角边框的屏幕截图。
you get a screenshot without the transparent roundet border.
然后退后
sSkinProvider1.AllowExtBorders:=True;
此后无需再做
Form1.Repaint;
您只会看到一个短暂的切换。
You will see only a short switch.
procedure TForm1.BitBtn1Click(Sender: TObject);
var
path:string;
b:TBitmap;
begin
sSkinProvider1.AllowExtBorders:=False;
Form1.Repaint;
path:= ExtractFilePath(Application.ExeName) + 'Screenshot\';
b := TBitmap.Create;
try
ScreenShot(TRUE, b) ;
b.SaveToFile(path + 'Screenshot_1.png');
finally
b.FreeImage;
FreeAndNil(b) ;
sSkinProvider1.AllowExtBorders:=True;
[...]
btw。不要将路径设置为
btw. do not set the path like
path:= ExtractFilePath(Application.ExeName) + '/Screenshot/';
使用Windows样式的反斜杠,并且仅一个
use windows style backslash and only one
path:= ExtractFilePath(Application.ExeName) + 'Screenshot\';
经Delphi5测试
这篇关于Delphi活动窗口截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!