Delphi Modal窗体(ModalResult)的介绍、使用方法和注意事项
1、ModalResult 介绍
//uses controls
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);
//uses windows
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;
ModalResult 表示模式对话框的返回值。应用程序可以使用任何整数值作为模式结果值。尽管TModalResult可以采用任何整数值,但为常用的TModalResult值定义了以下常量:
2、ModalResult 注意事项:
3、ShowModal 介绍
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;
4、使用示例:
//Form1中:
if Form2.ShowModal = mrok then ShowMessage(‘TaoRoy OK!‘);
//Form2中
procedure TForm2.button1Click(Sender: TObject);
begin
self.ModalResult := mrok;
end;
创建时间:2021.08.31 更新时间:
Delphi Modal窗体(ModalResult、ShowModal)的介绍、使用方法和注意事项
原文:https://www.cnblogs.com/guorongtao/p/15209614.html