自己程序中某窗口,运行时将窗口界面保存到图片(100)

T

tdp

Unregistered / Unconfirmed
GUEST, unregistred user!
自己程序中某窗口,运行时将窗口界面保存到图片就是对这个窗口抓屏的类似操作,要用自己程序执行注:这个窗口不一定在屏幕最顶端
 
procedure Get_Form_Image(aform: TForm); //拷贝屏幕图像Var abitmap: Tbitmap; acanvas: Tcanvas; DC: hdc; L, T: Integer; isfull, isjpg, isopen: Boolean; apath, aopen: String; ajpg: TJpegimage;Begin isfull := False;//是否全屏 isjpg := False;//保存为JPG? isopen:= True;//立即打开 aopen := 'mspaint.exe'; //打开方式 apath := Format('c:/%s',[aform.Caption]); //保存路径 aform.Update; SetFocus(aform.Handle); //Form置顶后,才能截图。 aform.Update; abitmap := TBitmap.Create; acanvas := TCanvas.Create; DC := Windows.Getdc(0); With aform Do Try acanvas.Handle := DC; If Not isfull Then //不需要全屏,只是当前Form Begin abitmap.Width := Width; abitmap.Height := Height; L := left; T := top; abitmap.Canvas.CopyRect(rect(0, 0, Width, Height), acanvas, rect(L, T, Width + L, Height + T)); End Else //全屏 Begin abitmap.Width := screen.Width; abitmap.Height := screen.Height; abitmap.Canvas.CopyRect(rect(0, 0, screen.Width, screen.Height), acanvas, rect(0, 0, screen.Width, screen.Height)); End; If isjpg Then Begin //BMP压缩成JPG 再保存。 apath := ChangeFileExt(apath, '.jpg'); ajpg := TJpegimage.Create; Try ajpg.Assign(abitmap); {将BMP图象转成JPG格式,便于在互联网上传输} ajpg.CompressionQuality := 75; {JPG文件压缩百分比设置,数字越大图像月清晰,数据越大。默认=75} ajpg.SaveToFile(apath); Finally FreeAndNil(aJpg); End; End Else //BMP 直接保存。 Begin apath := ChangeFileExt(apath, '.bmp'); abitmap.savetofile(apath); End; apath := ExtractShortPathName(apath); {修改成短路径,防止出现带空格的路径} //打开文件 If isopen And FileExists(apath) And (Length(aopen) > 0) Then ShellExecute(aform.Handle, 'open', PChar(aopen), PChar(apath), '', SW_SHOW); Finally acanvas.Free; releasedc(0, DC); abitmap.Free; End;End;procedure TForm1.BitBtn1Click(Sender: TObject);begin Get_Form_Image(self);end;
 
hhjjhhjj,非常感谢,你的代码运行也非常好,不过我想是否可以在form不在最顶端时也可以获得截图
 
不是在顶端就没办法了。因为系统没画出来,你也没办法让系统画不在顶端的画面,所以此问题无解。
 
我感觉应该有办法,或是某些操作系统才支持。我记得当年试用vista时,好象在任务栏中可以预览某一程序的内容,即使不是最顶端。当然也有可能记错了。
 
如果非要画不在顶端的窗体只能使用苯办法,首先bringwindowtotop然后执行上面的代码,之后再将窗体放回去,用户看起来会感觉屏幕闪一下,看你的需求有多迫切了
 
这几天也查了不少资料,看来在2K或XP的环境下的确不能得到非顶端窗口的图像,也许在vista或WIN7中可以。谢谢大家
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
737
DelphiTeacher的专栏
D
顶部