delphi中调用word或excel文件,打开一段时间后,在关闭此文件,如何进行关闭,解决有重谢!--在线等! ( 积分: 50 )

  • 主题发起人 主题发起人 goflay
  • 开始时间 开始时间
G

goflay

Unregistered / Unconfirmed
GUEST, unregistred user!
用ShellExecute函数执行指定的文件, 如何再关闭此文件<br>方法:获取执行文件的窗口句柄,但只知道文件名和路径,不知道打开的窗口句柄,能否<br>获取我执行此文件的句柄,然后应用 PostMessage进行关闭!<br>解决以后再加分! 谢谢大侠!!
 
用ShellExecute函数执行指定的文件, 如何再关闭此文件<br>方法:获取执行文件的窗口句柄,但只知道文件名和路径,不知道打开的窗口句柄,能否<br>获取我执行此文件的句柄,然后应用 PostMessage进行关闭!<br>解决以后再加分! 谢谢大侠!!
 
unit UnitMain;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs,tlhelp32, ExtCtrls, StdCtrls, shellapi;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;Timer1: TTimer;<br> &nbsp; &nbsp;Timer2: TTimer;<br> &nbsp; &nbsp;procedure Timer1Timer(Sender: TObject);<br> &nbsp; &nbsp;procedure Timer2Timer(Sender: TObject);<br> &nbsp;private<br>// &nbsp; &nbsp;procedure MyTimer(Sender:TObject);<br> &nbsp; &nbsp;function KillTask(ExeFileName: string): Integer;<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br>procedure MyTimer(Sender:TObject);<br>begin<br> &nbsp;if Form1.KillTask('QQ.exe')&gt; 0 then<br> &nbsp; &nbsp;showmessage('成功');<br>end;<br><br>function FindProcessName:THandle;<br>var<br> &nbsp;lppe: tprocessentry32;<br> &nbsp;sshandle: thandle;<br> &nbsp;found: boolean;<br>begin<br> &nbsp;result:=0;<br> &nbsp;sshandle := createtoolhelp32snapshot(TH32CS_SNAPALL, 0);<br> &nbsp;found := process32first(sshandle, lppe);<br> &nbsp;while found do<br> &nbsp;begin<br> &nbsp; &nbsp;if ansiCompareText(ExtractFileName(lppe.szExefile),'QQ.exe') = 0 then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; result:=lppe.th32ProcessID;<br> &nbsp; &nbsp; &nbsp; break;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;found := process32next(sshandle, lppe); {检索下一个进程}<br> &nbsp;end;<br> &nbsp;CloseHandle(sshandle);<br>end;<br><br>function TForm1.KillTask(ExeFileName: string): Integer;<br>const<br> &nbsp;PROCESS_TERMINATE = $0001;<br>var<br> &nbsp;ContinueLoop: BOOL;<br> &nbsp;FSnapshotHandle: THandle;<br> &nbsp;FProcessEntry32: TProcessEntry32;<br>begin<br> &nbsp;Result := 0;<br> &nbsp;FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);<br> &nbsp;FProcessEntry32.dwSize := SizeOf(FProcessEntry32);<br> &nbsp;ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);<br> &nbsp;while Integer(ContinueLoop) &lt;&gt; &nbsp;0 do<br> &nbsp;begin<br> &nbsp; &nbsp;if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) = UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) = UpperCase(ExeFileName))) then<br> &nbsp; &nbsp; &nbsp;Result := Integer(TerminateProcess(OpenProcess(PROCESS_TERMINATE,BOOL(0),FProcessEntry32.th32ProcessID),0));<br> &nbsp; &nbsp;ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);<br> &nbsp;end;<br> &nbsp;CloseHandle(FSnapshotHandle);<br>end;<br><br>procedure TForm1.Timer1Timer(Sender: TObject);<br>var<br> &nbsp; h: THandle;<br>begin<br> &nbsp;h:=FindProcessName;<br> &nbsp;if h&lt;&gt;0 then begin<br>// &nbsp;if KillTask('QQ.exe')&gt; 0 then<br>// &nbsp; &nbsp;showmessage('成功');<br> &nbsp; &nbsp;Timer2.Enabled := True;<br>// &nbsp; &nbsp;SetTimer(0, 0, 120, @MyTimer);<br>// &nbsp; &nbsp;ShellExecute(0,'open','E:/BJGE/Program Files/Tencent/QQ.exe',nil,nil,sw_hide);<br>// &nbsp; &nbsp; &nbsp;winexec('E:/BJGE/Program Files/Tencent/QQ.exe',sw_normal);<br> &nbsp;end;<br>end;<br><br><br>procedure TForm1.Timer2Timer(Sender: TObject);<br>begin<br> &nbsp;if KillTask('QQ.exe')&gt; 0 then<br> &nbsp; &nbsp;showmessage('成功');<br>end;<br><br>end.
 
判断当前WORD文件是打开还是关闭,然后对应用程序进行启动和关闭。
 
var &nbsp; HWndCalculator : HWnd;<br> &nbsp;HWndCalculator := Winprocs.FindWindow(nil, 'EXCEL.EXE');<br> &nbsp;if HWndCalculator &lt;&gt; 0 then<br> &nbsp; SendMessage(HWndCalculator, WM_CLOSE, 0, 0); &nbsp; // close the PROCESS
 
接受答案了.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
503
import
I
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
后退
顶部