他のスレッドの助けを借りて、私は実用的な解決策を見つけました:
using (ConnectingForm CF = new ConnectingForm())
{
CF.StartPosition = FormStartPosition.Manual;
CF.Show(this);
......
}
新しいフォームのロード イベント:
private void ConnectingForm_Load(object sender, EventArgs e)
{
this.Location = this.Owner.Location;
this.Left += this.Owner.ClientSize.Width / 2 - this.Width / 2;
this.Top += this.Owner.ClientSize.Height / 2 - this.Height / 2;
}
(私は専門家ではないので、間違っている場合は修正してください) 問題と解決策をどのように解釈するかを以下に示します。フォームを起動したとき。次に、新しいフォーム (接続フォーム) を呼び出したとき、その場所は親の場所に対して相対的ではなく、場所 (0, 0) (画面の左上隅) でした。つまり、MainForms の位置が変化し、Connecting Form の位置が移動しているように見えました。したがって、この問題の解決策は基本的に、最初に新しいフォームの場所をメイン フォームの場所に設定することでした。その後、場所を MainForm の中心に設定することができました。
TL;DR 新しいフォームの位置は、親フォームの位置に対してではなく、固定位置 (0, 0) であると推測しています。
便宜上、MainForm の Startup Position を固定のものに変更しました。また、新しいフォームの位置が常に MainForm の中心になるようにするためのイベントも追加しました。
private void Location_Changed(object sender, EventArgs e)
{
this.Location = this.Owner.Location;
this.Left += this.Owner.ClientSize.Width / 2 - this.Width / 2;
this.Top += this.Owner.ClientSize.Height / 2 - this.Height / 2;
}
private void ConnectingForm_Load(object sender, EventArgs e)
{
this.Owner.LocationChanged += new EventHandler(this.Location_Changed);
this.Location = this.Owner.Location;
this.Left += this.Owner.ClientSize.Width / 2 - this.Width / 2;
this.Top += this.Owner.ClientSize.Height / 2 - this.Height / 2;
}
うまくいけば、これは同じ問題を抱えている他の人を助けるでしょう!