如何使MessageDlg以所有者表单为中心
我希望 MessageDlg 显示在其父窗体的中心。 关于如何在 Delphi 2010 中实现这一点有什么建议吗?
我在这里找到了下面的代码: http://delphi.about.com/od/ formsdialogs/l/aa010304a.htm 但它对我不起作用。弹出窗口仍然不以所有者表单为中心。 (我不清楚该方法实际上如何知道所有者表单......)
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
该对话框与 TForm1 的实例没有关系。手动设置表单的位置并不难,但我敢打赌,更熟悉 VCL 这方面的人会知道如何以更简洁的方式进行设置。
就我个人而言,我从不使用 Position 属性并使用我自己的代码来定位所有表单,因为我从未对 Position 属性的性能感到满意。
更新:您可以使用
Self.InsertComponent(Dialog)
更改对话框的所有者。您必须将对话框存储到本地变量中,例如Dialog
,这样才能正常工作:The dialog doesn't have a relationship with the instance of TForm1. It would not be hard to set the position of the form manually, but I bet someone who is more familiar with this area of the VCL will know how to do it a cleaner way.
Personally I never use the Position property and use my own code to position all my forms because I've never been satisfied with the performance of the Position property.
UPDATE: You can change the owner of the dialog using
Self.InsertComponent(Dialog)
. You'd have to store your dialog into a local variable, say,Dialog
, for this to work:您可以
这样做并调用它
,但是,我个人不会这样做,因为
CreateMessageDialog
显示的对话框不是本机 Windows 对话框。将视觉结果与本机内容进行比较:至少在启用了 Aero 主题的 Windows 7 中,本机对话框看起来要好得多。然而,这似乎不能集中于任何特定的形式。相反,对话框以当前监视器为中心。但这也是 Windows 中的默认行为(尝试记事本、写字板或画图),那么为什么需要这种新行为呢?
You can do
and call it like
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: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?
为什么要将这种愿望限制在消息对话框中?正如 David Heffernan 评论:
使用以下单元,您可以将任何本机对话框居中,例如:
MessageBox
、TFindDialog
、TOpenDialog
、TFontDialog
、TPrinterSetupDialog
等...主单元提供了两个例程,两个例程都带有一些可选参数:您可以使用
OpenDialog1.Execute
并让 Windows 决定在哪里显示对话框,您现在使用ExecuteCentered(OpenDialog1)
并且对话框在屏幕的活动表单中居中:要显示消息对话框,请使用
MsgBox
,它是Application.MessageBox
的包装器(它又是Application.MessageBox
的包装器)代码>Windows.MessageBox)。一些示例:MsgBox('Hello world!');
MsgBox('取消保存?', MB_YESNO 或 MB_ICONQUESTION 或 MB_DEFBUTTON2);
MsgBox('请重试.', MB_OK, '错误');
MsgBox('我'在工具栏居中。', MB_OK, '有趣!', Toolbar1.Handle);
单位:
法律声明:这些单元是我在这个荷兰主题。原始版本来自Mark van Renswoude,参见NLDMessageBox。
Why limit this desire to message dialogs? Like David Heffernan commented:
With the following unit(s), you can center any native dialog, such as:
MessageBox
,TFindDialog
,TOpenDialog
,TFontDialog
,TPrinterSetupDialog
, etc... The main unit provides two routines, both with some optional parameters:Wherelse you would use
OpenDialog1.Execute
and let Windows decide where to show the dialog, you now useExecuteCentered(OpenDialog1)
and the dialog is centered in the screen's active form:To show message dialogs, use
MsgBox
, a wrapper aroundApplication.MessageBox
(which in turn is a wrapper aroundWindows.MessageBox
). Some examples:MsgBox('Hello world!');
MsgBox('Cancel saving?', MB_YESNO or MB_ICONQUESTION or MB_DEFBUTTON2);
MsgBox('Please try again.', MB_OK, 'Error');
MsgBox('I''m centered in the toolbar.', MB_OK, 'Fun!', Toolbar1.Handle);
The units:
Legal notice: These units are written by me in this Dutch topic. The original versions are from Mark van Renswoude, see NLDMessageBox.
这是我当前用来在活动表单上显示居中对话框的代码:
Here's the code I currently use to show a centered dialog over the active form: