【问题标题】:Windows.Forms Visual Studio, How to open a second window directly over the first window?Windows.Forms Visual Studio,如何在第一个窗口上直接打开第二个窗口?
【发布时间】:2009-08-09 17:29:45
【问题描述】:

如何在第一个窗口的上方直接打开第二个窗口,而不是在默认位置略微向右下方或向后打开?我只是想让几个屏幕被点击。我还有其他方法吗?

为什么CenterParent 不这样做?那么CenterParent 会做什么呢?

【问题讨论】:

标签: c# winforms visual-studio-2008


【解决方案1】:

尝试将新表单的位置设置为与第一个现有表单相同。确保第二个窗体的 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 );

两者都应该工作!

【讨论】:

  • 正确的代码,错误的地方。这(仅)属于 Load 事件。
  • @Henk:没错!在上面的窗口构建之后,下面的窗口可能会改变位置。已编辑帖子以反映这一点。
【解决方案2】:

查看属性:

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

【讨论】:

  • myDialog.ShowDialog(this);为我工作(这是父表单)。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 2019-06-08
  • 1970-01-01
  • 2012-12-21
  • 2018-12-06
  • 2014-06-18
  • 1970-01-01
相关资源
最近更新 更多