WinForm设置控件居中
发表于:2021-03-19 | 分类: .Net Winform
字数统计: 126 | 阅读时长: 1分钟 | 阅读量:

简单阐述

在C#的WinForm里面,原生控件是没有居中属性的,故通过重写OnResize(EventArgs e)方法,通过计算,重新定位控件位置。

以 Label 控件为例

(1)将label的AutoSize属性设置为false;Dock属性设置为fill;TextAlign属性设置为MiddleCenter。
(2)重写居中的代码如下:
1
2
3
4
5
6
7
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
int x = (int)(0.5 * (this.Width - label1.Width));
int y = label1.Location.Y;
label1.Location = new System.Drawing.Point(x,y);
}

参考地址

https://blog.csdn.net/mingyueyixi/article/details/55035935

上一篇:
CefSharp创建客户端
下一篇:
Spring Batch学习笔记(一)