请问发送消息来关闭程序的方法(100分)

  • 主题发起人 主题发起人 rackrain
  • 开始时间 开始时间
R

rackrain

Unregistered / Unconfirmed
GUEST, unregistred user!
我要用另一个程序发送消息来关闭一个程序,可是被关闭的程序在退出时有提示,请问应该如何做?<br>被关闭的程序在事件中:<br>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br>if &nbsp;application.messagebox('你是否确定要退出','提示',MB_YESNO+Mb_IconQuestion)=IDYES<br>&nbsp;then Action := caFree<br>&nbsp;else Action:= caNone;<br>end;<br><br>我的程序这样写:<br>procedure Tqq.Button1Click(Sender: TObject);<br>var handle:integer;<br>begin<br>&nbsp; handle:=findwindow('Tform1',nil);<br>&nbsp; if handle&lt;&gt;0 then<br>&nbsp; &nbsp; sendmessage(handle,WM_Close,0,0);<br>被关闭的程序弹出一个提示框选择是否关闭,请问语句应该如何写?
 
按着给对话框发送键盘事件<br>handle:=handle:=findwindow(nil,'提示');<br>if Handle&lt;&gt;0 then<br>&nbsp; PostMessage(Handle, WM_KEYDOWN, VK_RETURN,0)<br>看他关不关<br>&nbsp;
 
这个可以吧<br>postmessage(handle,wm_quit,0,0);
 
handle:=findwindow('Tform1','form1'); ///---这句的原因吧<br>&nbsp;if handle&lt;&gt;0 then<br>&nbsp; &nbsp;sendmessage(handle,WM_Close,0,0);
 
这样是否可以:<br>Form1中关闭发自定义消息WM_CloseMy01<br>其它程序中发自定义消息WM_CloseMy02<br>把Form1的关闭提示放在一个自定义的消息响应中:<br>if WM_CloseMy01 then <br>&nbsp; &nbsp; &nbsp;messagebox;<br>&nbsp; &nbsp; &nbsp;close<br>&nbsp; &nbsp;else WM_CloseMy02<br>&nbsp; &nbsp; &nbsp;close;<br>今天才注册,想好好学Delphi,请问哪里有v7的图书下载?<br>
 
拦截自定义的消息比较好
 
直接杀死进程
 
sendmessage(handle,WM_Close,0,0); &nbsp;
 
我试了一下,用下面这个函数,可以关闭程序而不出现提示<br>uses <br>&nbsp;...TLHelp32;<br>procedure TForm1.Button1Click(Sender: TObject);<br>const<br>&nbsp;PROCESS_TERMINATE = $0001;<br>var<br>&nbsp;ExeFileName: string;<br>&nbsp;ContinueLoop: BOOL;<br>&nbsp;FSnapshotHandle: THandle;<br>&nbsp;FProcessEntry32: TProcessEntry32;<br>begin<br>&nbsp;ExeFileName := 'aa{你要关闭的程序名}.EXE';<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; 0 do<br>&nbsp;begin<br>&nbsp; &nbsp;if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =<br>&nbsp; &nbsp; &nbsp;UpperCase(ExeFileName))<br>&nbsp; &nbsp; &nbsp;or (UpperCase(FProcessEntry32.szExeFile) =<br>&nbsp; &nbsp; &nbsp;UpperCase(ExeFileName))) then<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp;TerminateProcess(OpenProcess(PROCESS_TERMINATE, BOOL(0),<br>&nbsp; &nbsp; &nbsp; &nbsp;FProcessEntry32.th32ProcessID), 0);<br>&nbsp; &nbsp;end;<br>&nbsp; &nbsp;ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);<br>&nbsp;end;<br>end;<br>
 
多人接受答案了。
 

Similar threads

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