旗下导航:搜·么
当前位置:网站首页 > .Net教程 > 正文

从0自学C#01–自绘窗体边框【C#.Net教程】,C#,自绘窗体边框

作者:搜教程发布时间:2019-11-27分类:.Net教程浏览:82评论:0


导读:重要引见自绘窗体边框和背景色彩逐级变浅(深)。1.先设置窗体为无边框形式this.FormBorderStyle=System.Windows.Forms.Form...
重要引见自绘窗体边框和背景色彩逐级变浅(深)。

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++;
            }        
        }

以上就是从0自学C#01–自绘窗体边框的内容,更多相关内容请关注ki4网(www.ki4.cn)!

标签:C#自绘窗体边框


欢迎 发表评论: