From the documentation:
Do not call this directly from your code. Instead, set the
StartPosition property to CenterScreen.
The CenterToScreen method uses the following priority list to
determine the screen used to center the form:
- The Owner property of the form.
- The HWND owner of the form.
- The screen that currently has the mouse cursor.
So, effectively it's used during the initial showing of the form. It's not intended to be used later.
You could write your own like so:
protected void ReallyCenterToScreen()
{
Screen screen = Screen.FromControl(this);
Rectangle workingArea = screen.WorkingArea;
this.Location = new Point() {
X = Math.Max(workingArea.X, workingArea.X + (workingArea.Width - this.Width) / 2),
Y = Math.Max(workingArea.Y, workingArea.Y + (workingArea.Height - this.Height) / 2)
};
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…