簡體   English   中英

TSaveDialog position

[英]TSaveDialog position

TSaveDialog 的默認 position 是屏幕的中心。 如何設置 TSaveDialog window 的 position? 我想要這樣的東西:

SaveDialog1.top := topValue;
SaveDialog1.left := leftValue;
if (SaveDialog1.execute(self.handle)) then begin
  ...
end;

我在此頁面上找到了此示例,但我已將其修改為與退出 TSaveDialog 一起使用,而不是創建新的 class。

type
 TSaveDialog = class(Dialogs.TSaveDialog)
   protected
      fTop: integer;
      fLeft: integer;
      procedure WndProc(var Message: TMessage); override;
   public
      property top: integer read fTop write fTop;
      property left: integer read fLeft write fLeft;
   end;

type
  TForm1 = class(TForm)
    dlgSave1: TSaveDialog;
    btn1: TButton;
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;


var
  Form1: TForm1;

implementation

{$R *.dfm}
uses
  CommDlg;

procedure TForm1.btn1Click(Sender: TObject);
begin
  dlgSave1.left := 20;
  dlgSave1.top := 100;
   if dlgSave1.Execute then
   // do your work here
end;

{ TMySaveDialog }

procedure TSaveDialog.WndProc(var Message: TMessage);
begin
  inherited WndProc(Message);
  if (Message.Msg = WM_NOTIFY) then
      case (POFNotify(Message.LParam)^.hdr.code) of
           CDN_INITDONE: SetWindowPos(POFNotify(Message.LParam)^.hdr.hwndFrom, 0, fLeft, fTop, 0, 0, SWP_NOSIZE);
      end;
end;

The GetSaveFileName API function, which is what TSaveDialog is a wrapper form, doesn't provide any way to control the position of the dialog box, so you need to intercept an early message to the dialog and adjust the position there, as other solutions you '已經看到了。

您希望對話框以您的窗體為中心,因此為對話框提供TopLeft屬性的解決方案不會很好地工作,因為它們沒有考慮窗口的大小,並且它們還需要您計算新坐標在您每次調用Execute之前。

這是一個不同的想法。 它仍然需要覆蓋WndProc

type
  TCenterSaveDialog = class(TSaveDialog)
  private
    FCenterForm: TCustomForm;
  protected
    procedure WndProc(var Message: TMessage); override;
  public
    // When this property is assigned, the dialog will center
    // itself over the given form each time the dialog appears.
    property CenterForm: TCustomForm read FCenterForm write FCenterForm;
  end;

procedure TCenterSaveDialog.WndProc(var Message: TMessage);
var
  lpOfNotify: POFNotify;
  FormRect, DialogRect: TRect;
  NewLeft, NewTop: Integer;
begin
  inherited;
  if (Message.Msg = wm_Notify) and Assigned(CenterForm) then begin
    lpOfNotify := POFNotify(Message.LParam);
    if lpOfNotify.hdr.code = cdn_InitDone then begin
      GetWindowRect(CenterForm.Handle, FormRect);
      GetWindowRect(lpOfNotify.hdr.hwndFrom, DialogRect);
      NewLeft := FormRect.Left
        + (FormRect.Right - FormRect.Left) div 2
        - (DialogRect.Right - DialogRect.Left) div 2;
      NewTop := FormRect.Top
        + (FormRect.Bottom - FormRect.Top) div 2
        - (DialogRect.Bottom - DialogRect.Top) div 2;
      SetWindowPos(lpOfNotify.hdr.hwndFrom, 0,
        NewLeft, NewTop, 0, 0,
        swp_NoActivate or swp_NoOwnerZOrder or swp_NoSize or swp_NoZOrder);
    end;
  end;
end;

另請參閱: cdn_InitDone

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM