How to make MessageDlg centered on owner form

后端 未结 4 783
终归单人心
终归单人心 2020-12-03 06:21

I\'d like that MessageDlg appear centered on its parent form. Any suggestions on how to accomplish this in Delphi 2010?

I found the code below here: http://delphi.ab

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-03 06:47

    Here's the code I currently use to show a centered dialog over the active form:

    function MessageDlgCenter(const Msg: string; DlgType: TMsgDlgType;
      Buttons: TMsgDlgButtons): Integer;
    var R: TRect;
    begin
      if not Assigned(Screen.ActiveForm) then
      begin
        Result := MessageDlg(Msg, DlgType, Buttons, 0);
      end else
      begin
        with CreateMessageDialog(Msg, DlgType, Buttons) do
        try
          GetWindowRect(Screen.ActiveForm.Handle, R);
          Left := R.Left + ((R.Right - R.Left) div 2) - (Width div 2);
          Top := R.Top + ((R.Bottom - R.Top) div 2) - (Height div 2);
          Result := ShowModal;
        finally
          Free;
        end;
      end;
    end;
    

提交回复
热议问题