Windows.Forms Visual Studio, как открыть второе окно прямо над первым окном? - PullRequest
2 голосов
/ 09 августа 2009

Как я могу открыть второе окно непосредственно над первым, а не слегка по диагонали вниз-вправо или назад в положении по умолчанию? Я просто пытаюсь сделать несколько экранов, по которым можно щелкнуть. Есть ли другой способ, которым я должен делать это?

почему CenterParent этого не делает? Что тогда делает CenterParent?

Ответы [ 2 ]

2 голосов
/ 09 августа 2009

Попробуйте установить местоположение новой формы равным первой существующей форме. Убедитесь, что свойство StartPosition второй формы установлено на «Ручной». Это предполагает, что все ваши формы одинакового размера .

Пример конструктора «плавающей» формы:

// reference to the form underneath, as it might
// change location between creating the FloatingWindow, and showing
// FloatingWindow!
Form BeneathWindow;

public FloatingWindow(Form BeneathWindow)
{
   InitializeComponent();

   // save this for when we show the form
   this.BeneathWindow = BeneathWindow;
   StartPosition = FormStartPosition.Manual;

}

// OnLoad event handler
private void FloatingWindowLoad(object sender, EventArgs e)
{
   Location = BeneathWindow.Location;
}

Если ваши формы не одинакового размера , то вы, вероятно, захотите отцентрировать их. Вы можете использовать CenterParent, как предлагали другие, или вы можете вручную центрировать их самостоятельно, как мне иногда нравится делать:

Location = new Point((BeneathWindow.Width - Width)/2 , (BeneathWindow.Height - Height)/2 );

Либо должно работать!

1 голос
/ 09 августа 2009

см. Свойства:

  1. StartPosition, попробуйте установить значение CenterParent

  2. Владелец, попробуйте установить его в ParentForm. Или откройте окно, используя методы:

    //
    // Summary:
    //     Shows the form with the specified owner to the user.
    //
    // Parameters:
    //   owner:
    //     Any object that implements System.Windows.Forms.IWin32Window and represents
    //     the top-level window that will own this form.
    //
    // Exceptions:
    //   System.ArgumentException:
    //     The form specified in the owner parameter is the same as the form being shown.
    public void Show(IWin32Window owner);
    

или

    //
    // Summary:
    //     Shows the form as a modal dialog box with the specified owner.
    //
    // Parameters:
    //   owner:
    //     Any object that implements System.Windows.Forms.IWin32Window that represents
    //     the top-level window that will own the modal dialog box.
    //
    // Returns:
    //     One of the System.Windows.Forms.DialogResult values.
    //
    // Exceptions:
    //   System.ArgumentException:
    //     The form specified in the owner parameter is the same as the form being shown.
    //
    //   System.InvalidOperationException:
    //     The form being shown is already visible.-or- The form being shown is disabled.-or-
    //     The form being shown is not a top-level window.-or- The form being shown
    //     as a dialog box is already a modal form.-or-The current process is not running
    //     in user interactive mode (for more information, see System.Windows.Forms.SystemInformation.UserInteractive).
    public DialogResult ShowDialog(IWin32Window owner);

Или Вы можете сделать это программно:

public partial class ChildForm : Form
{
    public ChildForm(Form owner)
    {
        InitializeComponent();
        this.StartPosition = FormStartPosition.Manual;
        int x = owner.Location.X + owner.Width / 2 - this.Width / 2;
        int y = owner.Location.Y + owner.Height / 2 - this.Height / 2;
        this.DesktopLocation = new Point(x, y);
    }
}

Родительская форма:

public partial class ParentForm : Form
{
    public ParentForm()
    {
        InitializeComponent();
    }

    private void ButtonOpenClick(object sender, EventArgs e)
    {
        ChildForm form = new ChildForm(this);
        form.Show();
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...