Delphi Modal窗体(ModalResult、ShowModal)的介绍、使用方法和注意事项
<p><span style="font-size: 16px"><strong>Delphi Modal窗体(ModalResult)的介绍、使用方法和注意事项</strong></span></p><p><span style="font-size: 16px"><strong>1、ModalResult 介绍</strong></span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">//usescontrols
const
mrNone = 0;
mrOk = idOk;
mrCancel = idCancel;
mrAbort = idAbort;
mrRetry = idRetry;
mrIgnore = idIgnore;
mrYes = idYes;
mrNo = idNo;
mrAll = mrNo + 1;
mrNoToAll= mrAll + 1;
mrYesToAll = mrNoToAll + 1;
type
TModalResult = Low(Integer)..High(Integer);
//useswindows
const
IDOK = 1; ID_OK = IDOK;
IDCANCEL = 2; ID_CANCEL = IDCANCEL;
IDABORT = 3; ID_ABORT = IDABORT;
IDRETRY = 4; ID_RETRY = IDRETRY;
IDIGNORE = 5; ID_IGNORE = IDIGNORE;
IDYES = 6; ID_YES = IDYES;
IDNO = 7; ID_NO = IDNO;
IDCLOSE = 8; ID_CLOSE = IDCLOSE;
</span></pre>
</div>
<p><span style="font-size: 16px">ModalResult 表示模式对话框的返回值。应用程序可以使用任何整数值作为模式结果值。尽管TModalResult可以采用任何整数值,但为常用的TModalResult值定义了以下常量:</span></p>
<ul>
<li><span style="font-size: 16px">mrNone //无。在用户退出之前用作默认值。</span></li>
<li><span style="font-size: 16px">mrOk //idOK 用户使用OK按钮退出。</span></li>
<li><span style="font-size: 16px">mrCancel //idCancel 用户使用“取消”按钮退出。</span></li>
<li><span style="font-size: 16px">mrAbort //idAbort用户使用中止按钮退出。</span></li>
<li><span style="font-size: 16px">mrRetry //idRetry用户使用重试按钮退出。</span></li>
<li><span style="font-size: 16px">mrIgnore //idIgnore用户使用IGNORE按钮退出。</span></li>
<li><span style="font-size: 16px">mrYes //IdYes 用户使用“是”按钮退出。</span></li>
<li><span style="font-size: 16px">mrNo //idNo 用户使用“否”按钮退出。</span></li>
<li><span style="font-size: 16px">mrAll //mrNo+1用户使用ALL按钮退出。</span></li>
<li><span style="font-size: 16px">mrNoToAll //mrAll+1用户使用“全部拒绝”按钮退出。</span></li>
<li><span style="font-size: 16px">mrYesToAll //mrNoToAll+1 用户使用“全部是”按钮退出。</span></li>
</ul>
<p><span style="font-size: 16px"><strong>2、ModalResult 注意事项:</strong></span></p>
<ul>
<li><span style="font-size: 16px"><span style="color: rgba(255, 0, 0, 1)">ModalResult属性返回值执行之后,该按钮所在的窗体会自动关闭,请勿再次使用Close关闭窗体</span>。</span></li>
<li><span style="font-size: 16px">基本上窗体和按钮的都有ModalResult的属性值</span></li>
<li><span style="font-size: 16px">窗体的ModalResult属性会自动传递给ShowModal,作为方法的返回值。</span></li>
</ul>
<p><strong><span style="font-size: 16px">3、ShowModal 介绍</span></strong></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;">function ShowModal: Integer; virtual;
function TCustomForm.ShowModal: Integer;
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
Application.HandleMessage;
if Application.FTerminate then ModalResult := mrCancel else
if ModalResult <> 0 then CloseModal;
until ModalResult <> 0;
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;</pre>
</div>
<ul>
<li>使用ShowModal将窗体显示为模态窗体。模式窗体是指在关闭窗体之前应用程序无法继续运行的窗体。因此,ShowModal在表单关闭之前不会返回。当窗体关闭时,它返回ModalResult属性的值。</li>
<li>要关闭模态窗体,请将其ModalResult属性设置为非零值。</li>
<li>注意:如果表单包含ModalResult属性设置为mrNone以外的值的按钮,则当用户单击其中一个按钮并将ModalResult值作为ShowModal的返回值返回时,表单将自动关闭。</li>
<li>您可以使用全局IsAbortResult、IsAllResult、IsNegativeResult或IsPositiveResult函数对照常见返回值检查返回值。 </li>
</ul>
<p><span style="font-size: 16px"><strong>4、使用示例:</strong></span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">//Form1中:
if Form2.ShowModal = mrok then ShowMessage('TaoRoy OK!');
//Form2中
procedure TForm2.button1Click(Sender: TObject);
begin
self.ModalResult := mrok;
end;
</span></pre>
</div>
<p><span style="font-size: 16px"> </span></p>
<p> </p>
<p> </p>
<p><span style="color: rgba(136, 136, 136, 1)">创建时间:2021.08.31 更新时间:</span></p>
</div>
<div id="MySignature" role="contentinfo">
博客园 滔Roy https://www.cnblogs.com/guorongtao 希望内容对你有所帮助,谢谢!<br><br>
来源:https://www.cnblogs.com/guorongtao/p/15209614.html
頁:
[1]