我如何定位TOpenDialog

dum*_*uch 3 delphi windows-xp delphi-2007

我有一个Delphi应用程序,它使用TOpenDialog让用户选择一个文件.默认情况下,打开的对话框以当前监视器为中心显示,现在可以离应用程序窗口"英里".我希望对话框以TOpenDialog的所有者控件为中心显示,如果失败了,我会选择应用程序的主窗口.

以下代码类型的工作,它是从TJvOpenDialog派生的,它给了我一些如何做到的提示:

type
  TMyOpenDialog = class(TJvOpenDialog)
  private
    procedure SetPosition;
  protected
    procedure DoFolderChange; override;
    procedure WndProc(var Msg: TMessage); override;
  end;

procedure TMyOpenDialog.SetPosition;
begin
var
  Monitor: TMonitor;
  ParentControl: TWinControl;
  Res: LongBool;
begin
  if (Assigned(Owner)) and (Owner is TWinControl) then
    ParentControl := (Owner as TWinControl)
  else if Application.MainForm <> nil then
    ParentControl := Application.MainForm
  else begin
    // this code was already in TJvOpenDialog
    Monitor := Screen.Monitors[0];
    Res := SetWindowPos(ParentWnd, 0,
      Monitor.Left + ((Monitor.Width - Width) div 2),
      Monitor.Top + ((Monitor.Height - Height) div 3),
      Width, Height,
      SWP_NOACTIVATE or SWP_NOZORDER);
    exit; // =>
  end;
  // this is new
  Res := SetWindowPos(GetParent(Handle), 0,
    ParentControl.Left + ((ParentControl.Width - Width) div 2),
    ParentControl.Top + ((ParentControl.Height - Height) div 3),
    Width, Height,
    SWP_NOACTIVATE or SWP_NOZORDER);
end;

procedure TMyOpenDialog.DoFolderChange
begin
  inherited DoFolderChange;  // call inherited first, it sets the dialog style etc.
  SetPosition;
end;

procedure TMyOpenDialog.WndProc(var Msg: TMessage);
begin
  case Msg.Msg of
    WM_ENTERIDLE: begin
      // This has never been called in my tests, but since TJVOpenDialog
      // does it I figured there may be some fringe case which requires
      // SetPosition being called from here.
      inherited; // call inherited first, it sets the dialog style etc.
      SetPosition;
      exit;
    end;
  end;
  inherited;
end;
Run Code Online (Sandbox Code Playgroud)

"有点工作"意味着第一次打开对话框时,它会以所有者表单为中心显示.但是,如果我然后关闭对话框,移动窗口并再次打开对话框,SetWindowPos似乎没有任何效果,即使它确实返回true.对话框在第一次打开的位置打开.

这是在Windows XP上运行的Delphi 2007,目标框也运行Windows XP.

Dav*_*nan 6

您描述的行为我只能通过将OwnerHwnd的伪值传递给对话框的Execute方法来重现.

然后,此窗口句柄将传递给基础Windows公共控件,如果在显示对话框时未将其设置为活动窗体的句柄,则实际上您的对话框会出现其他问题.

例如,当我调用Execute并传递时Application.Handle,对话框总是出现在同一个窗口中,位于一个相当奇怪的位置,无论我的主窗体在哪里.

当我调用Execute并将句柄传递给我的主窗体时,对话框出现在主窗体的顶部,稍微向右和向下移动.无论表单是哪个监视器都是如此.

我正在使用Delphi 2010,我不知道你的Delphi版本是否有重载的Execute版本.即使你没有那个,你仍然可以创建一个派生类,它将为OwnerHwnd传递一个更合理的值.

虽然我没有确凿的100%证据表明这是你的问题,但我认为这种观察会让你得到满意的解决方案.


Ser*_*yuz 2

TJvOpenDialog是 的后代TOpenDialog,因此您应该在 VCL 将对话框居中后运行放置调用。VCL 这样做是为了响应CDN_INITDONE通知。响应消息WM_SHOWWINDOW太早了,在我的测试中,窗口过程从未收到消息WM_ENTERIDLE

uses
  commdlg;

[...]

procedure TJvOpenDialog.DoFolderChange;
begin
  inherited DoFolderChange;  
//  SetPosition; // shouldn't be needing this, only place the dialog once
end;

procedure TJvOpenDialog.WndProc(var Msg: TMessage);
begin
  case Msg.Msg of
    WM_NOTIFY: begin
      if POFNotify(Msg.LParam)^.hdr.code = CDN_INITDONE then begin
        inherited;    // VCL centers the dialog here
        SetPosition;  // we don't like it ;)
        Exit;
      end;
  end;
  inherited;
end;
Run Code Online (Sandbox Code Playgroud)

或者,

procedure TJvOpenDialog.WndProc(var Msg: TMessage);
begin
  case Msg.Msg of
    WM_NOTIFY: if POFNotify(Msg.LParam)^.hdr.code = CDN_INITDONE then
                 Exit;
  end;
  inherited;
end;
Run Code Online (Sandbox Code Playgroud)

将对话框放置在操作系统放置的位置,这实际上是有意义的。