溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C# 開發(fā)圓角窗體

發(fā)布時(shí)間:2020-09-08 16:27:09 來源:網(wǎng)絡(luò) 閱讀:5249 作者:helicon80 欄目:編程語(yǔ)言

   因?yàn)轫?xiàng)目需要做個(gè)Winform的隨機(jī)啟動(dòng)的數(shù)據(jù)上傳工具,用Visual Studio的窗體感覺太丑了,就想進(jìn)行優(yōu)化,反正就一個(gè)窗體,上面也沒啥按鈕,就不要標(biāo)題欄了,就搞一個(gè)圓角的窗體好了,搞個(gè)漂亮的背景圖片。上面搞一個(gè)最小化和關(guān)閉按鈕。把窗體設(shè)置為圓角窗口的操作如下:

    1、把窗體frmMain的FormBorderStyle屬性設(shè)置為None,去掉窗體的邊框,讓窗體成為無邊框的窗體。

    2、設(shè)置窗體的Region屬性,該屬性設(shè)置窗體的有效區(qū)域,我們把窗體的有效區(qū)域設(shè)置為圓角矩形,窗體就變成圓角的。

    3、添加兩個(gè)控件,控制窗體的最小化和關(guān)閉。

    設(shè)置為圓角窗體,主要涉及GDI+中兩個(gè)重要的類 Graphics和GraphicsPath類,分別位于System.Drawing和System.Drawing.Drawing2D。

  接著我們需要這樣一個(gè)函數(shù) private void SetWindowRegion() 此函數(shù)設(shè)置窗體有效區(qū)域?yàn)閳A角矩形,以及一個(gè)輔助函數(shù) private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)此函數(shù)用來創(chuàng)建圓角矩形路徑,將在SetWindowRegion()中調(diào)用它。

ublic void SetWindowRegion()  
{  
    System.Drawing.Drawing2D.GraphicsPath FormPath;  
    FormPath = new System.Drawing.Drawing2D.GraphicsPath();  
    Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);  
    FormPath = GetRoundedRectPath(rect, 10);  
    this.Region = new Region(FormPath);    
}  
private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)  
{  
    int diameter = radius;  
    Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));  
    GraphicsPath path = new GraphicsPath();    
    // 左上角  
    path.AddArc(arcRect, 180, 90);    
    // 右上角  
    arcRect.X = rect.Right - diameter;  
    path.AddArc(arcRect, 270, 90);    
    // 右下角  
    arcRect.Y = rect.Bottom - diameter;  
    path.AddArc(arcRect, 0, 90);    
    // 左下角  
    arcRect.X = rect.Left;  
    path.AddArc(arcRect, 90, 90);  
    path.CloseFigure();//閉合曲線  
    return path;  
}

   在窗體尺寸改變的時(shí)候我們需要調(diào)用SetWindowRegion()將窗體變成圓角的。

private void frmMain_Resize(object sender, EventArgs e)
{
    SetWindowRegion();
}

設(shè)置按鈕的形狀:

   添加兩個(gè)普通的按鈕button,設(shè)置按鈕的BackColor屬性為Transparent,讓背景透明,不然按鈕的背景色與窗體的圖片背景不相符。設(shè)置按鈕的FlatStyle屬性為Flat,同時(shí)設(shè)置FlatAppearance屬性中的BorderSize=0,MouseDownBackColor和MouseOverBackColor的值均為Transparent,防止點(diǎn)擊按鈕時(shí),顏色變化影響美觀。調(diào)整按鈕的大小和位置即可。最小化和關(guān)閉按鈕(是右下角托盤,所以沒有退出程序)的代碼如下:

private void btn_min_Click(object sender, EventArgs e)
{
	this.WindowState = FormWindowState.Minimized;
}
private void btn_close_Click(object sender, EventArgs e)
{
	this.WindowState = FormWindowState.Minimized;
	this.Hide();
}

  還可以添加代碼,控制鼠標(biāo)移動(dòng)到操作按鈕上時(shí),改變按鈕上文字的顏色,來增加體驗(yàn)。

程序界面如下圖

C# 開發(fā)圓角窗體

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI