Windows.Forms Visual Studio, How to open a second window directly over the first window?

本秂侑毒 提交于 2019-12-11 01:59:52

问题


How can I open a second window directly over the first, and not slightly diagonally down-right or back at the default position? I'm just trying to make a few screens to be clicked through. Is there another way I should be doing this?

why doesnt CenterParent do it? What does CenterParent do then?


回答1:


Try setting the location of the new form to be equal to the first, existing form. Make sure that the second form's StartPosition property is set to 'Manual'. This assumes your forms are all the same sizes.

Example constructor of the 'floating' form:

// 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;
}

If your forms are not the same sizes, then you'll likely want to center them. You can use CenterParent as others have suggested, or you can manually center them yourself, as I sometimes like to do:

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

Either should work!




回答2:


See properties:

  1. StartPosition, try set it to CenterParent

  2. Owner, try set it to ParentForm. Or Open Yours window using methods:

    //
    // 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);
    

Or

    //
    // 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);

Or You can do it programmatically:

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);
    }
}

Parent form:

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

    private void ButtonOpenClick(object sender, EventArgs e)
    {
        ChildForm form = new ChildForm(this);
        form.Show();
    }
}


来源:https://stackoverflow.com/questions/1251802/windows-forms-visual-studio-how-to-open-a-second-window-directly-over-the-first

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!