如何使 MessageDlg 以所有者表单为中心 [英] How to make MessageDlg centered on owner form

查看:22
本文介绍了如何使 MessageDlg 以所有者表单为中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望 MessageDlg 出现在其父窗体的中心.关于如何在 Delphi 2010 中完成此操作的任何建议?

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

我在这里找到了以下代码:http://delphi.about.com/od/formsdialogs/l/aa010304a.htm 但它对我不起作用.弹出窗口仍然没有以所有者表单为中心.(我不清楚该方法如何实际知道所有者表单...)

I found the code below here: http://delphi.about.com/od/formsdialogs/l/aa010304a.htm but it's not working for me. The pop-up still is not centered on the owner form. (It's not clear to me how the method would actually know the owner form...)

 function TForm1.MessageDlg(const Msg: string; DlgType: TMsgDlgType;
   Buttons: TMsgDlgButtons; HelpCtx: Integer): Integer;
 begin
   with CreateMessageDialog(Msg, DlgType, Buttons) do
     try
       Position := poOwnerFormCenter;
       Result := ShowModal
     finally
       Free
     end
 end;

推荐答案

你可以做

function MessageDlg(const AOwner: TForm; const Msg: string; DlgType: TMsgDlgType;
  Buttons: TMsgDlgButtons; HelpCtx: Integer = 0): Integer;
begin
  with CreateMessageDialog(Msg, DlgType, Buttons) do
    try
      Left := AOwner.Left + (AOwner.Width - Width) div 2;
      Top := AOwner.Top + (AOwner.Height - Height) div 2;
      Result := ShowModal;
    finally
      Free;
    end
end;

并称之为

procedure TForm1.FormClick(Sender: TObject);
begin
  MessageDlg(Self, 'This is a test', mtInformation, [mbOK]);
end;

但是,我个人不会这样做,因为 CreateMessageDialog 显示的对话框不是本机 Windows 对话框.将视觉效果与原生内容进行比较:

However, I would personally not do this, because the dialog shown by CreateMessageDialog is not a native Windows dialog. Compare the visual result with the native stuff:

procedure TForm1.FormClick(Sender: TObject);
begin
  case MessageBox(Handle, PChar('This is a test. Do you wish to do something?'), PChar('A Silly Example'), MB_ICONQUESTION or MB_YESNO) of
    ID_YES:
      MessageBox(Handle, PChar('Great!'), PChar('A Silly Example'), MB_ICONINFORMATION or MB_OK);
    ID_NO:
      MessageBox(Handle, PChar('OK, well, I cannot force you...'), PChar('A Silly Example'), MB_ICONINFORMATION or MB_OK);
  end;
end;

至少在启用 Aero 主题的 Windows 7 中,本机对话框看起来好多了.然而,这似乎不能以任何特定形式为中心.相反,对话框以当前监视器为中心.但这也是 Windows 中的默认行为(尝试记事本、写字板或画图),那么您为什么需要这种新行为?

At least in Windows 7 with the Aero theme enabled, the native dialog looks much better. However, it seems, this cannot be centered over any particular form. Instead, the dialog is centered on the current monitor. But this is also the default behaviour in Windows (try Notepad, WordPad, or Paint), so why do you need this new behaviour?

这篇关于如何使 MessageDlg 以所有者表单为中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆