C#中使窗口居中
时间: 2023-06-29 21:19:18 浏览: 99
你可以使用以下代码来使窗口在屏幕中央显示:
```csharp
this.StartPosition = FormStartPosition.CenterScreen;
```
将此代码放在窗口的构造函数或Load事件中即可。
相关问题
c# tooptip窗体居中显示
在C#中,要让ToolTip窗体居中显示,可以使用以下代码:
```csharp
// 获取屏幕的宽度和高度
int screenWidth = Screen.PrimaryScreen.WorkingArea.Width;
int screenHeight = Screen.PrimaryScreen.WorkingArea.Height;
// 计算ToolTip窗体的位置
int x = (screenWidth - toolTipForm.Width) / 2;
int y = (screenHeight - toolTipForm.Height) / 2;
// 设置ToolTip窗体的位置
toolTipForm.StartPosition = FormStartPosition.Manual;
toolTipForm.Location = new Point(x, y);
```
其中,`toolTipForm`是你的ToolTip窗体对象。这段代码会将ToolTip窗体居中显示在屏幕上。
C#窗口居中
要让C#程序中的窗口居中,可以通过代码设置窗口的位置。
以下是一段示例代码,可以在窗口加载时居中窗口:
```
private void Form1_Load(object sender, EventArgs e)
{
// 计算居中位置
int x = (Screen.PrimaryScreen.WorkingArea.Width - this.Width) / 2;
int y = (Screen.PrimaryScreen.WorkingArea.Height - this.Height) / 2;
// 设置窗口位置
this.StartPosition = FormStartPosition.Manual;
this.Location = new Point(x, y);
}
```
在这段代码中,使用了 `Screen.PrimaryScreen.WorkingArea` 属性获取屏幕的工作区域,然后通过计算,得出窗口应该显示的位置。最后,将窗口的位置设置为计算得出的位置即可。