方法1、
Pen pen1 = new Pen(Color.FromArgb(233, 149, 87));
e.Graphics.DrawRectangle(pen1, new Rectangle(0, 0, this.Width - 1, this.Height - 1));
方法2、
Rectangle myRectangle = new Rectangle(0, 0, this.Width, this.Height);
ControlPaint.DrawBorder(e.Graphics, myRectangle,
Color.FromArgb(0, 0, 0), 2, ButtonBorderStyle.Solid,
Color.FromArgb(0, 0, 0), 2, ButtonBorderStyle.Solid,
Color.FromArgb(0, 0, 0), 2, ButtonBorderStyle.Solid,
Color.FromArgb(0, 0, 0), 2, ButtonBorderStyle.Solid
);
對比:
一、Winform Panel邊框方法一:每邊能設置不同的顏色、寬度和樣式
1、拖一個 Panel控件到主窗體中,保持默認名稱 panel1,BorderStyle 選擇 Fixed3D。
2、雙擊 Panel1,打開后台代碼文件,在 panel1_Paint(object sender, PaintEventArgs e) 方法中添加如下代碼:
private void panel1_Paint(object sender, PaintEventArgs e)
{
ControlPaint.DrawBorder(e.Graphics, panel1.ClientRectangle,
Color.White, 1, ButtonBorderStyle.Solid, //左邊
Color.White, 1, ButtonBorderStyle.Solid, //上邊
Color.DimGray, 1, ButtonBorderStyle.Solid, //右邊
Color.DimGray, 1, ButtonBorderStyle.Solid);//底邊
}
每邊共有三個參數,分別為:邊框顏色、寬度和樣式;如果把 1 改為 0,則覆蓋原來的邊框,否則顏色搭配得當將出現凹凸邊框效果,示例運行效果如圖1所示:
圖1
如果 BorderStyle 選擇 None,則又是另外一種效果(邊框內突起)。
二、Winform Panel邊框方法二:每邊樣式一樣
步驟跟方法一一樣,只是在 panel1_Paint(object sender, PaintEventArgs e) 方法中添加如下代碼:
private void panel1_Paint(object sender, PaintEventArgs e)
{
ControlPaint.DrawBorder(e.Graphics, ClientRectangle, Color.Ivory, ButtonBorderStyle.Solid);
}
每邊都設置為同一風格,即相同的顏色、寬度和樣式。
三、推薦重繪邊框的方法
private void panelAll_Paint(object sender, PaintEventArgs e)
{
Rectangle myRectangle = new Rectangle(0, 0, this.panelAll.Width, this.panelAll.Height);
ControlPaint.DrawBorder(e.Graphics, myRectangle,
System.Drawing.Color.FromArgb(((int)(((byte)(245)))), ((int)(((byte)(145)))), ((int)(((byte)(28))))), 2, ButtonBorderStyle.Solid,
System.Drawing.Color.FromArgb(((int)(((byte)(245)))), ((int)(((byte)(145)))), ((int)(((byte)(28))))), 2, ButtonBorderStyle.Solid,
System.Drawing.Color.FromArgb(((int)(((byte)(245)))), ((int)(((byte)(145)))), ((int)(((byte)(28))))), 2, ButtonBorderStyle.Solid,
System.Drawing.Color.FromArgb(((int)(((byte)(245)))), ((int)(((byte)(145)))), ((int)(((byte)(28))))), 2, ButtonBorderStyle.Solid
);
}此方法可以避免一些重繪時發生的錯誤。