我也遇到过,解决办法如下: var h:HWND; h:=FindWindow('TForm1',nil);//注意这里,应该以第一个参数,也就ClassName为标志查找 if h<>0 then begin// SendMessage(); end;
可以这样实现: 在你要关闭的窗口单元中重载窗口消息WndProc: protected procedure WndProc(var Message: TMessage); override; ... ...//实现代码: procedure TForm1.WndProc(var Message: TMessage); begin inherited; if Message.Msg = SC_CLOSE then Application.Terminate; end; 然后在另一个窗口中:procedure TForm2.btn1Click(Sender: TObject); var h: HWND; begin h := FindWindow('TForm1', nil); if h <> 0 then SendMessage(h, SC_CLOSE, 0, 0); end;
var
h:HWND;
h:=FindWindow('TForm1',nil);//注意这里,应该以第一个参数,也就ClassName为标志查找
if h<>0 then
begin// SendMessage();
end;
在你要关闭的窗口单元中重载窗口消息WndProc:
protected
procedure WndProc(var Message: TMessage); override;
...
...//实现代码:
procedure TForm1.WndProc(var Message: TMessage);
begin
inherited;
if Message.Msg = SC_CLOSE then
Application.Terminate;
end;
然后在另一个窗口中:procedure TForm2.btn1Click(Sender: TObject);
var
h: HWND;
begin
h := FindWindow('TForm1', nil);
if h <> 0 then
SendMessage(h, SC_CLOSE, 0, 0);
end;