• Delphi中将ShowMessage,MessageDlg, MessageBox,InputBox,InputQuery及任意模态窗口相对主窗口(父窗口、母窗口)居中


    Delphi中调用系统的一些Modal模态对话框(如ShowMessage,MessageDlg, Application.MessageBox,InputBox,InputQuery等),一般是屏幕居中,也有其他窗体的TForm.Position未定义主窗口居中poMainFormCenter,若主窗口不在屏幕中间或多个显示屏时,这些对话框位置就不在主窗口中间,既不美观也不便于操作。

    本文提供一简易方法,不修改模态对话框任何代码,实现将任意模态窗口相对于主窗口(或任意父窗口)居中。本方法也可移植到C#,Java等

    procedure NextModalFormMoveToMyCenter(Owner: TForm);
    begin
    //From HeZiHang@cnBlogs
    TThread.ForceQueue(nil, procedure var R: TRect; X, Y: Integer; begin GetWindowRect(Application.ActiveFormHandle, R); X := Owner.Left + (Owner.Width - R.Width) div 2; Y := Owner.Top + (Owner.Height - R.Height) div 2; // 将InputBox或MessageBox在Owner Form居中 SetWindowPos(Application.ActiveFormHandle, HWND_TOP, X,Y, 0, 0, SWP_NOSIZE or SWP_SHOWWINDOW or SWP_NOOWNERZORDER); end); end;

    //Demo1:MessageBox自动居中
    procedure TForm1.btnResetClick(Sender: TObject); begin NextModalFormMoveToMyCenter(Self); if Application.MessageBox('是否复位重启?', '重启', MB_YESNOCANCEL or MB_ICONQUESTION or MB_DEFBUTTON2) <> mrYes then 。。。。。。。。。//此时MessageBox对Form1居中 end;
    //Demo2:InputBox自动居中 
    procedure TForm1.btnCommandClick(Sender: TObject);
    var
      S: String;
    begin
      NextModalFormMoveToMyCenter(Self);
      S := InputBox('请输入命令', '命令:', '');。//此时InputBox对Form1居中
        。。。。
    end;
    
    //Demo3:其他窗体,自动居中
    procedure TForm1.btnCommandClick(Sender: TObject); var F:TMyForm; S: String; begin NextModalFormMoveToMyCenter(Self); F:=TMyForm.Create(Self); F.ShowModal;//此时 F对Form1居中
    ..............
    F.DisposeOf; end;
  • 相关阅读:
    ARCGIS JAVASCRIPT API (3.2)部署
    WINFORM 只能运行一个实例问题
    iOS 版本号
    同步和异步的区别
    简单的手机号判断
    "_inflateEnd", referenced from "_inflateInit_"等。这时需要在工程中加入libz.dlib 文件
    iOS 实现打电话
    assign retain copy iOS
    iOS 长按事件 UILongPressGestureRecognizer
    UITableView 滑动删除
  • 原文地址:https://www.cnblogs.com/hezihang/p/16079484.html
Copyright © 2020-2023  润新知