delphi - 如何在Delphi中集中Windows异常对话框?

标签 delphi api dialog delphi-7

我试图将所有消息对话框居中,包括父窗体上的所有异常对话框,而不是始终将它们显示在屏幕中央。

我使用的是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/

相关文章:

windows - 在对话框中自动单击“确定”的工具/代码

java - DialogFragment 中的 getContentResolver() 和 getWindow()

android - Android 上的 AlertDialog 内部 EditText 扩展太慢

multithreading - 如何使用 Indy 建立双向 TCP 连接?

forms - 从 XE8 中的 Form Tag 分配给 RadioGroup Tag 会导致访问冲突

delphi - ssl httpserver 抛出 'Access violation at address 00000000. Read of address 00000000' 错误

javascript - 如何在reactjs中使用AJAX单击时重新加载表格

json - 如何解决flutter中的http error code 400 missing parameters?

rest - Yii2 从后端 Controller 调用 api 方法

delphi - 如何检测表单大小调整何时开始和停止?