关闭按钮不起作用解决方法
关闭按钮不起作用
开始运行后,点击button1后,在不点button2前,窗体不能正常关闭,但是最小最大化都可以,这是为什么呢?
------解决方案--------------------
友情UP
------解决方案--------------------
- Delphi(Pascal) code
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; Mark:Boolean=True; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin while Mark do begin Application.ProcessMessages; end; end; procedure TForm1.Button2Click(Sender: TObject); begin Mark:=False; end; end.
开始运行后,点击button1后,在不点button2前,窗体不能正常关闭,但是最小最大化都可以,这是为什么呢?
------解决方案--------------------
友情UP
------解决方案--------------------
- Delphi(Pascal) code
while Mark and not Application.Terminated do Application.ProcessMessages;
------解决方案--------------------
點擊關閉按鈕,會產生WM_Close,而這個消息是非隊列消息;Application.ProcessMessages并不會處理到。
正是由于這段
procedure TForm1.Button1Click(Sender: TObject);
begin
while Mark do
begin
Application.ProcessMessages;
end;
end;
使得程序一直專注于處理隊列消息,沒有機會處理非隊列消息。
應該這么寫
- Delphi(Pascal) code
點擊關閉按鈕,會產生WM_Close,而這個消息是非隊列消息;Application.ProcessMessages并不會處理到。 正是由于這段 procedure TForm1.Button1Click(Sender: TObject); begin while Mark do begin Application.HandleMessage; end; end;
------解决方案--------------------
while Mark do
begin
Application.ProcessMessages;
end;
这是个死循环呀
------解决方案--------------------
或者Timer