C# 为什么当我关闭控件时Form1没有回到中心屏幕位置?
我第一次把表格放在0,0的位置 然后在关闭事件中,我希望它返回到中央屏幕,但窗体仍然处于0,0位置C# 为什么当我关闭控件时Form1没有回到中心屏幕位置?,c#,C#,我第一次把表格放在0,0的位置 然后在关闭事件中,我希望它返回到中央屏幕,但窗体仍然处于0,0位置 如何修复它?如果您设置this.StartPosition=FormStartPosition.CenterScreen;然后你需要重新打开表单。从其他方面看,它不会生效。首先通过设置e.Cancel=true来阻止关闭此表单。然后将窗口移到屏幕中央: private void histogramGraphsToolStripMenuItem_Click(object sender, EventA
如何修复它?如果您设置this.StartPosition=FormStartPosition.CenterScreen;然后你需要重新打开表单。从其他方面看,它不会生效。首先通过设置
e.Cancel=true来阻止关闭此表单。然后将窗口移到屏幕中央:
private void histogramGraphsToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Location = new Point(0, 0);
HistogramGraphs1 = new Lightnings_Extractor.Histogram_Graphs();
HistogramGraphs1.Show();
HistogramGraphs1.FormClosing += new FormClosingEventHandler(HistogramGraphs1_FormClosing);
histogramGraphsToolStripMenuItem.Enabled = false;
}
private void HistogramGraphs1_FormClosing(object sender , FormClosingEventArgs e)
{
this.StartPosition = FormStartPosition.CenterScreen;
histogramGraphsToolStripMenuItem.Enabled = true;
}
这篇MSDN文章可能很有用:
说明:
:获取或设置一个值,该值指示是否应取消事件
:获取或设置以屏幕坐标表示窗体左上角的点
Ria您的代码正在运行,只是我必须删除行:e.Cancel=true;如果我使用它,它将阻止我关闭ZedGraph控件。我想关闭它,但也将Form1移动到屏幕的中心。HistorograMgraphs1是包含ZedGraph控件的新表单。
private void HistogramGraphs1_FormClosing(object sender , FormClosingEventArgs e)
{
histogramGraphsToolStripMenuItem.Enabled = true;
e.Cancel = true;
int x = Screen.PrimaryScreen.WorkingArea.Width / 2 - this.Width / 2;
int y = Screen.PrimaryScreen.WorkingArea.Height / 2 - this.Height / 2;
this.Location = new Point(x, y);
}