program distributable1;uses
Forms,
ufrmUsers in 'ufrmUsers.pas' {frmUsers},{$R *.res}begin
Application.Initialize;
TfrmUsers.Create(Application).ShowModal;
Application.Run;
end.对上面的代码我很疑惑, 一般情况下. 是在执行Application.Run后,才会进入程序..., 而我单步跟踪的结果, 是在TfrmUsers.Create(Application).ShowModal;,进入窗体界面操作完成后,然后执行的Application.Run进入主消息循环.
那么主消息循环和窗体消息有什么关系吗? 上面的代码中Application.Run有什么意义吗?
Forms,
ufrmUsers in 'ufrmUsers.pas' {frmUsers},{$R *.res}begin
Application.Initialize;
TfrmUsers.Create(Application).ShowModal;
Application.Run;
end.对上面的代码我很疑惑, 一般情况下. 是在执行Application.Run后,才会进入程序..., 而我单步跟踪的结果, 是在TfrmUsers.Create(Application).ShowModal;,进入窗体界面操作完成后,然后执行的Application.Run进入主消息循环.
那么主消息循环和窗体消息有什么关系吗? 上面的代码中Application.Run有什么意义吗?
解决方案 »
- 高手请进,dll中如何使用exe传过来的handle问题
- 在delhpi7中专家模版中追加自定义FORM的问题
- 刚刚在网上看到的一段程序,运行以下出现了这个错误,请指教
- TWebBrowser问题
- 连接Database时出现Operation must be performed on the current session
- 怎么样把阿拉伯数字转换成汉字?
- 求助如何把图片存入到access里边?有否输入汉字自动转化成拼音的控件?多谢....
- 请大虾们进来看一看
- 我都快崩溃了,关于微型(票据)打印机的问题?
- 动态创建窗体后,Canvas.Pixels失效
- mdichild跟随mdiform大小变化
- 下面的代码哪里错了,系统总是在SQL.ADD处报错
基实这是一个创建窗体的程序,假如没有创建你就不可以运行了
Application.Run;在这种情况下所起的作用... 因为在进入主消息循环之前就已经创建显示窗体了...
TfrmUsers.Create(Application)创建的窗体是如何处理消息循环的呢? Application.Run在这里还有什么意义吗?
接收,派发消息。
repeat
HandleMessage
until Terminated;
说点其他的,或许有点用。
Delphi会自动把Run()放到项目文件的主代码块中。不要自己去调用这个方法,但需要知道这个
方法到底干了些什么。
首先,TApplication.Run()建立一个退出过程,以保证当应用程序退出运行时所有的组件都会得到释放。
然后,它就建立一个循环来处理消息,直到应用程序终止。
我疑惑的就是在Application.Run;运行之前, 就已经进入窗体并可以处理事件了.单步跟踪发现
动态创建的窗体处理完毕后, 也就是模式返回后才运行的Application.Run;
竟然窗体在Application.Run;运行之前就可以创建能够处理事件. 那么运行Application.Run;还有什么意义吗? 可以删除吗?
var
WindowList: Pointer;
SaveFocusCount: Integer;
SaveCursor: TCursor;
SaveCount: Integer;
ActiveWindow: HWnd;
begin
CancelDrag;
if Visible or not Enabled or (fsModal in FFormState) or
(FormStyle = fsMDIChild) then
raise EInvalidOperation.Create(SCannotShowModal);
if GetCapture <> 0 then SendMessage(GetCapture, WM_CANCELMODE, 0, 0);
ReleaseCapture;
Application.ModalStarted;
try
Include(FFormState, fsModal);
ActiveWindow := GetActiveWindow;
SaveFocusCount := FocusCount;
Screen.FSaveFocusedList.Insert(0, Screen.FFocusedForm);
Screen.FFocusedForm := Self;
SaveCursor := Screen.Cursor;
Screen.Cursor := crDefault;
SaveCount := Screen.FCursorCount;
WindowList := DisableTaskWindows(0);
try
Show;
try
SendMessage(Handle, CM_ACTIVATE, 0, 0);
ModalResult := 0;
repeat // ********* BEGIN 开始自己的消息循环。。
Application.HandleMessage;
if Application.FTerminate then ModalResult := mrCancel else
if ModalResult <> 0 then CloseModal;
until ModalResult <> 0; // END....
Result := ModalResult;
SendMessage(Handle, CM_DEACTIVATE, 0, 0);
if GetActiveWindow <> Handle then ActiveWindow := 0;
finally
Hide;
end;
finally
if Screen.FCursorCount = SaveCount then
Screen.Cursor := SaveCursor
else Screen.Cursor := crDefault;
EnableTaskWindows(WindowList);
if Screen.FSaveFocusedList.Count > 0 then
begin
Screen.FFocusedForm := Screen.FSaveFocusedList.First;
Screen.FSaveFocusedList.Remove(Screen.FFocusedForm);
end else Screen.FFocusedForm := nil;
if ActiveWindow <> 0 then SetActiveWindow(ActiveWindow);
FocusCount := SaveFocusCount;
Exclude(FFormState, fsModal);
end;
finally
Application.ModalFinished;
end;
end;由于你创建了主窗口 而每个窗口都有自己的消息循环<当ShowModel>的时候对消息循环进行处理。。
所以后面的run 可以不用。。
源码之前 一目了然 :->