Setting the start position for OpenFileDialog/SaveFileDialog

前端 未结 8 1636
野趣味
野趣味 2021-01-01 11:48

For any custom dialog (form) in a WinForm application I can set its size and position before I display it with:

form.StartPosition = FormStartPosition.Manual         


        
8条回答
  •  無奈伤痛
    2021-01-01 12:03

    OpenFileDialog and SaveFileDialog position themselves in the upper-left corner of the client area of the most recently displayed window. So just create a new invisible window positioned where you want the the dialog to appear before creating and showing that dialog.

    Window dialogPositioningWindow = new Window();
    dialogPositioningWindow.Left = MainWindow.Left + ;
    dialogPositioningWindow.Top  = MainWindow.Top  + ;
    dialogPositioningWindow.Width = 0; 
    dialogPositioningWindow.Height = 0; 
    dialogPositioningWindow.WindowStyle = WindowStyle.None;
    dialogPositioningWindow.ResizeMode = ResizeMode.NoResize;
    dialogPositioningWindow.Show();// OpenFileDialog is positioned in the upper-left corner
                                   // of the last shown window (dialogPositioningWindow)
    Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog();
    ...
    if ((bool)dialog.ShowDialog()){
       ...
    }
    dialogPositioningWindow.Close();
    

提交回复
热议问题