如何在Delphi中集中Windows Exception对话框?

时间:2010-07-21 10:58:07

标签: delphi api dialog delphi-7

我正在尝试集中所有消息对话框,包括父窗体上的任何异常对话框,而不是让它们始终显示在屏幕的中心。

我正在使用Delphi 7我注意到使用MessageDlgPos允许X和Y的参数在屏幕上找到对话框,这对于我希望显示给用户的任何消息都很好。但是异常对话框的位置呢?它们也可以出现在父表单的中心吗?

任何帮助都非常感谢!

1 个答案:

答案 0 :(得分:1)

@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;