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

第一次我将表单放在位置 0,0 然后在关闭事件中我希望它回到中心屏幕,但表单仍处于 0,0 位置。

我该如何解决?

4

2 回答 2

1

首先防止通过 set 关闭此表单e.Cancel = true。然后将窗口移动到屏幕中心:

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

这篇 MSDN 文章可能有用:

解释:

CancelEventArgs.Cancel: 获取或设置一个值,该值指示是否应该取消事件。

Form.Location Property: 获取或设置代表窗体左上角屏幕坐标的Point。

于 2012-10-29T09:20:16.957 回答
0

如果您设置 this.StartPosition = FormStartPosition.CenterScreen; 那么您需要重新打开表单。否则不会生效。

于 2012-10-29T09:11:41.547 回答