我试图将所有消息对话框居中,包括父窗体上的所有异常对话框,而不是始终将它们显示在屏幕中央。
我使用的是Delphi 7,我注意到使用MessageDlgPos允许X和Y的参数在屏幕上定位对话框,这对于希望显示给用户的任何消息都很好。但是异常对话框的位置呢?它们也可以出现在父表单的中心吗?
任何帮助,不胜感激!
最佳答案
@Rucia,我的建议是使用OnException
组件中的TApplicationEvents
事件,然后使用CreateMessageDialog
函数创建自己的对话框。
看到这个例子。
procedure TForm1.ApplicationEvents1Exception(Sender: TObject; E: Exception);
var
MyDialogMsg : TForm;
ALeft : Integer;
ATop : Integer;
begin
//Create the dialog with the exeception message
MyDialogMsg := CreateMessageDialog(E.Message, mtError, [mbOk]);
try
//Calculate the pos of the dialog using the Screen.ActiveForm and the dialog size.
ALeft := Screen.ActiveForm.Left + (Screen.ActiveForm.Width div 2) - (MyDialogMsg.Width div 2);
ATop := Screen.ActiveForm.Top + (Screen.ActiveForm.Height div 2) - (MyDialogMsg.Height div 2);
if ALeft < 0 then ALeft := Screen.ActiveForm.Left;
if ATop < 0 then ATop := Screen.ActiveForm.Top;
if (ALeft + MyDialogMsg.Width > Screen.Width) or (ATop + MyDialogMsg.Height > Screen.Height)
then
begin
ALeft := (Screen.Width - MyDialogMsg.Width) div 2;
ATop := (Screen.Height - MyDialogMsg.Height) div 2;
MyDialogMsg.SetBounds (ALeft, ATop, MyDialogMsg.Width, MyDialogMsg.Height);
end
else
MyDialogMsg.SetBounds(ALeft, ATop, MyDialogMsg.Width, MyDialogMsg.Height);
//show the dialog
MyDialogMsg.ShowModal;
finally
MyDialogMsg.Free;
end;
end;
关于delphi - 如何在Delphi中集中Windows异常对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3298454/