如何直接在第一个窗口上打开第二个窗口,而不是在默认位置稍微向右下方或向后打开?我只是想点击几个屏幕。我还有另一种方法吗?
为什么CenterParent不这样做?那么CenterParent会做什么?
答案 0 :(得分:2)
尝试将新表单的位置设置为等于第一个现有表单。确保第二个窗体的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 :(得分:1)
参见属性:
StartPosition,尝试将其设置为CenterParent
所有者,尝试将其设置为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();
}
}