C#之WinForm設置控件居中


簡單闡述

在C#的WinForm里面,原生控件是沒有居中屬性的,故通過重寫OnResize(EventArgs e)方法,通過計算,重新定位控件位置。

以Label控件為例

(1)將label的AutoSize屬性設置為false;Dock屬性設置為fill;TextAlign屬性設置為MiddleCenter。
(2)重寫居中的代碼如下:
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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM