zoukankan      html  css  js  c++  java
  • Delphi Modal窗体(ModalResult、ShowModal)的介绍、使用方法和注意事项

    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值定义了以下常量:

    • mrNone  //无。在用户退出之前用作默认值。
    • mrOk   //idOK   用户使用OK按钮退出。
    • mrCancel  //idCancel  用户使用“取消”按钮退出。
    • mrAbort   //idAbort用户使用中止按钮退出。
    • mrRetry   //idRetry用户使用重试按钮退出。
    • mrIgnore  //idIgnore用户使用IGNORE按钮退出。
    • mrYes  //IdYes 用户使用“是”按钮退出。
    • mrNo   //idNo 用户使用“否”按钮退出。
    • mrAll   //mrNo+1用户使用ALL按钮退出。
    • mrNoToAll  //mrAll+1用户使用“全部拒绝”按钮退出。
    • mrYesToAll   //mrNoToAll+1  用户使用“全部是”按钮退出。

    2、ModalResult 注意事项:

    • ModalResult属性返回值执行之后,该按钮所在的窗体会自动关闭,请勿再次使用Close关闭窗体
    • 基本上窗体和按钮的都有ModalResult的属性值
    • 窗体的ModalResult属性会自动传递给ShowModal,作为方法的返回值。

    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;
    • 使用ShowModal将窗体显示为模态窗体。模式窗体是指在关闭窗体之前应用程序无法继续运行的窗体。因此,ShowModal在表单关闭之前不会返回。当窗体关闭时,它返回ModalResult属性的值。
    • 要关闭模态窗体,请将其ModalResult属性设置为非零值。
    • 注意:如果表单包含ModalResult属性设置为mrNone以外的值的按钮,则当用户单击其中一个按钮并将ModalResult值作为ShowModal的返回值返回时,表单将自动关闭。
    • 您可以使用全局IsAbortResult、IsAllResult、IsNegativeResult或IsPositiveResult函数对照常见返回值检查返回值。  

    4、使用示例:

    //Form1中:
    if Form2.ShowModal = mrok then ShowMessage('TaoRoy OK!');
    
    //Form2中 
    procedure TForm2.button1Click(Sender: TObject);
    begin
       self.ModalResult := mrok;
    end;
    

      

    创建时间:2021.08.31  更新时间:

    博客园 滔Roy https://www.cnblogs.com/guorongtao 希望内容对你所有帮助,谢谢!
  • 相关阅读:
    leetcode108 Convert Sorted Array to Binary Search Tree
    leetcode98 Validate Binary Search Tree
    leetcode103 Binary Tree Zigzag Level Order Traversal
    leetcode116 Populating Next Right Pointers in Each Node
    Python全栈之路Day15
    Python全栈之路Day11
    集群监控
    Python全栈之路Day10
    自动部署反向代理、web、nfs
    5.Scss的插值
  • 原文地址:https://www.cnblogs.com/guorongtao/p/15209614.html
Copyright © 2011-2022 走看看