c#Windows窗體中,每個控件都有自己的屬性,可以在屬性中修改簡單的樣式,如前景色、背景圖片等,但是如果想要窗體更美觀,就需要通過控件的paint函數進行繪制。
每個按鈕都在一個矩形內部,矩形內部左上角坐標為(http://www.amjmh.com/v/BIBRGZ_558768/),以最常見的按鈕為例,繪制按鈕所在矩形:
public static void button1_Paint(object sender, PaintEventArgs e)
{
simplebutton.ForeColor = Color.DodgerBlue;//按鈕字體顏色
e.Graphics.DrawLine(Pens.Red, 0, 0, 0, button1.Height);//左邊線
e.Graphics.DrawLine(Pens.Red, 0, 0, button1.Width, 0);//上邊線
e.Graphics.DrawLine(Pens.Red, button1.Width, 0, button1.Width, button1.Height);//右邊線
e.Graphics.DrawLine(Pens.Red, 0, button1.Height, button1.Width, button1.Height);//下邊線
}
效果如圖所示:
由於矩形下邊線和右邊線超出了控件顯示范圍,可以通過修改坐標值來改變顯示效果,修改代碼:
public static void button1_Paint(object sender, PaintEventArgs e)
{
simplebutton.ForeColor = Color.DodgerBlue;//按鈕字體顏色
e.Graphics.DrawLine(Pens.Red, 0, 0, 0, button1.Height);//左邊線
e.Graphics.DrawLine(Pens.Red, 0, 0, button1.Width, 0);//上邊線
e.Graphics.DrawLine(Pens.Red, button1.Width - 1, 0, button1.Width - 1, button1.Height - 1);//右邊線
e.Graphics.DrawLine(Pens.Red, 0, button1.Height - 1, button1.Width - 1, button1.Height - 1);//下邊線
}
修改后的效果: