从0自学C#01–自绘窗体边框
主要介绍自绘窗体边框和背景颜色逐级变浅(深)。
1.先设置窗体为无边框模式
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
也可以右击窗体,选择属性,找到FormBorderStyler设置为None。此时窗体右上角的最小化、最大化和关闭按钮会消失,鼠标拖动窗体的功能也会消失,这些会在第4步添加。
2.绘制边框
选择当前项目右击,添加组件,让组件类Component1继承Panel类,然后重写OnPaint(PaintEventArgs e),绘制边框颜色。生成解决方案,该组件会自动添加到工具箱里,组件栏下。类代码如下:
public partial class Component1 : Panel
{
public Component1()
{
InitializeComponent();
}
public Component1(IContainer container)
{
container.Add(this);
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
ControlPaint.DrawBorder(e.Graphics,
this.ClientRectangle,
Color.FromArgb(00,59,96),
2,
ButtonBorderStyle.Sol