从0自学C#01--自绘窗体边框

从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.Solid,
                          Color.FromArgb(00,59,96),
                          2,
                          ButtonBorderStyle.Solid,
                          Color.FromArgb(00,59,96),
                          2,
                          ButtonBorderStyle.Solid,
                          Color.FromArgb(00,59,96),
                          2,
                          ButtonBorderStyle.Solid);
            base.OnPaint(e);
        }
    }

Color.FromArgb(00,59,96)设置边框颜色,2设置边框尺寸。
注:如果不知道设置什么rgb值,使得颜色好看。可以去网上下载一个屏幕取色器,读出你认为好看颜色的rbg值。

3.添加边框

将组件Component1添加到窗体,设置其Dock属性为Fill。然后设置Padding属性为2,2,2,2(与边框尺寸对应)。也可以动态实现,代码如下:

this.component11.Dock = System.Windows.Forms.DockStyle.Fill;
this.component11.Padding = new System.Windows.Forms.Padding(2);

4.添加窗体关闭、最小化,鼠标拖动功能

在窗体组件this.Component11上,添加Panel,设置其Dock属性为Top,自定义背景颜色为0,102,171,前景色为Transparent。

this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
this.panel1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(102)))), ((int)(((byte)(171)))));
this.panel1.ForeColor = System.Drawing.Color.Transparent;

然后在Pannel上添加两个label,设置其Text分别为__和X,为最小化和最大化图标。字体设置为微软雅黑、粗体、小五号。最后给Pannel添加MouseDown和MouseMove事件。给label添加Click事件,添加MouseEnter和MouseLeave事件,使得鼠标移动到图标上时,鼠标变为手型。

Point mouseOffSet;

private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            mouseOffSet = new Point(-e.X, -e.Y);
        }

private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Point mousePosition = Control.MousePosition;
                mousePosition.Offset(mouseOffSet.X, mouseOffSet.Y);
                Location = mousePosition;
            }
        }

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

private void labelClose_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
            Application.Exit();
        }

private void labelMinimize_MouseEnter(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Hand;
        }

private void labelMinimize_MouseLeave(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Default;
        }

private void labelClose_MouseEnter(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Hand;
        }

private void labelClose_MouseLeave(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Default;
        }

5.Panel背景色设置为逐级变淡

给Pannel添加Paint事件。

private void panel1_Paint(object sender, PaintEventArgs e)
        {
            int y, dy;
            y = this.panel1.ClientRectangle.Location.Y;
            dy = this.panel1.ClientRectangle.Height / 90;
            for (int i = 0; i <= 89;i++ )
            {
                Color c = new Color();
                //调用Color对象的FromArgb方法
                c = Color.FromArgb(50, i + 50, i + 120);//0,102,171
                SolidBrush sb = new SolidBrush(c);
                Pen p = new Pen(sb, 1);
                //绘制矩形
                e.Graphics.DrawRectangle(p, this.panel1.ClientRectangle.X, y, this.Width, y + dy);
                y = y + dy;
                //i++;
            }        
        }

你可能感兴趣的:(自绘窗体边框,背景颜色逐级变浅)