dze*_*ras 193
Nay*_*iya 143
Using the Property window
Select form → go to property window → select "start position" → select whatever the place you want.
Programmatically
Form form1 = new Form();
form1.StartPosition = FormStartPosition.CenterScreen;
form1.ShowDialog();
Note: Do not directly call Form.CenterToScreen() from your code. Read here.
har*_*eyt 34
一行:
this.Location = new Point((Screen.PrimaryScreen.WorkingArea.Width - this.Width) / 2,
(Screen.PrimaryScreen.WorkingArea.Height - this.Height) / 2);
Run Code Online (Sandbox Code Playgroud)
小智 27
在Windows窗体中:
this.StartPosition = FormStartPosition.CenterScreen;
Run Code Online (Sandbox Code Playgroud)
在WPF中:
this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
Run Code Online (Sandbox Code Playgroud)
这就是你要做的一切......
Sar*_*r.A 16
如果您想在运行时使用以下代码使窗口居中,请将其复制到您的应用程序中:
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)};
}
Run Code Online (Sandbox Code Playgroud)
最后调用上面的方法让它工作:
ReallyCenterToScreen();
Run Code Online (Sandbox Code Playgroud)
在运行时居中表单1.
设置Form的以下属性:
- > StartPosition:CenterScreen
- > WindowState:Normal
这将在运行时将表单居中,但如果表单大小超出预期,请执行第二步.
2.在InitializeComponent()之后添加自定义大小;
public Form1()
{
InitializeComponent();
this.Size = new Size(800, 600);
}
Run Code Online (Sandbox Code Playgroud)
用这个:
this.CenterToScreen(); // This will take care of the current form
Run Code Online (Sandbox Code Playgroud)
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace centrewindow
{
public partial class Form1 : Form
{
public struct RECT
{
public int Left; // x position of upper-left corner
public int Top; // y position of upper-left corner
public int Right; // x position of lower-right corner
public int Bottom; // y position of lower-right corner
}
[DllImport("user32.dll")]
public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
[DllImport("user32.dll")]
public static extern bool GetWindowRect(HandleRef hwnd, out RECT lpRect);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
CentreWindow(Handle, GetMonitorDimensions());
}
private void CentreWindow(IntPtr handle, Size monitorDimensions)
{
RECT rect;
GetWindowRect(new HandleRef(this, handle), out rect);
var x1Pos = monitorDimensions.Width/2 - (rect.Right - rect.Left)/2;
var x2Pos = rect.Right - rect.Left;
var y1Pos = monitorDimensions.Height/2 - (rect.Bottom - rect.Top)/2;
var y2Pos = rect.Bottom - rect.Top;
SetWindowPos(handle, 0, x1Pos, y1Pos, x2Pos, y2Pos, 0);
}
private Size GetMonitorDimensions()
{
return SystemInformation.PrimaryMonitorSize;
}
}
}
Run Code Online (Sandbox Code Playgroud)
集中任何窗口,你可以得到的句柄
可能与问题不完全相关。但也许可以帮助别人。
中心屏幕以上对我来说都不起作用。原因是我正在向表单动态添加控件。从技术上讲,当它居中时,它是正确的,基于添加控件之前的表单。
这是我的解决方案。(应该适用于这两种情况)
int x = Screen.PrimaryScreen.Bounds.Width - this.PreferredSize.Width;
int y = Screen.PrimaryScreen.Bounds.Height - this.PreferredSize.Height;
this.Location = new Point(x / 2, y / 2);
Run Code Online (Sandbox Code Playgroud)
所以你会注意到我使用“PreferredSize”而不是仅仅使用高度/宽度。添加控件后,首选大小将保留表单的值。高度/宽度不会的地方。
希望这对某人有帮助。
干杯
归档时间: |
|
查看次数: |
195253 次 |
最近记录: |