开发者

How to centralize Windows Exception dialogs in Delphi?

I'm trying to center all message dialogs, including any exception dialogs on the parent form rather than having them always appear in the center of the screen.

I'm using Delphi 7 i note that using MessageDlgPos allows the params of X an Y to locate the dialog on screen, and thats fine for any messages i wish to display to the user. But what about the location of the exception dialogs? Can they appear in the cent开发者_如何学运维er of the parent form as well?

Any help greatly appreciated!


@Rucia, my suggestion is wich you use the OnException Event from the TApplicationEvents component, and then create your own dialog using the CreateMessageDialog function.

see this sample.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜