C#创建背景色渐变窗体的方法:创建特殊窗体

目录

1.让背景渐变色的理论基础

2.让背景渐变色的方法

3.一个实施例

(1)Form1.Designer.cs

(2)Form1.cs

(3)渐变的蓝色背景


在窗体设计时,可以通过设置窗体的BackColor属性来改变窗口的背景颜色,但是该属性改变后整个窗体的客户区都会变成这种颜色,这样显得非常单调。如果窗体的客户区可以像标题栏一样能够体现颜色的渐变效果,那么窗体风格将会另有一番风味。

1.让背景渐变色的理论基础

在实现窗体背景色渐变功能时主要用到了Color结构的FromArgb方法,Color结构表示一种ARGB颜色(alpha、红色、绿色和蓝色),其FromArgb方法用来从指定的8位颜色值(红色、绿色和蓝色)创建Color结构,该方法为可重载方法,其最常用的语法格式如下:

cs 复制代码
publie static Color FromArgb(int red,int green,int blue)

FromArgb方法中的参数说明如表:

|---------------------|------------------------|
| | |
| red | 新Color的红色分量值,有效值为0~255 |
| green | 新Color的绿色分量值,有效值为0~255 |
| blue | 新Color的蓝色分量值,有效值为0~255 |
| 返回值 | 创建的Color结构 |

2.让背景渐变色的方法

FromArgb方法就是用3种不同的色值来返回一个颜色,而稍微调整某一种颜色值就可以使整体的颜色发生细微的变化,在窗体中至上而下每行填充一种稍微调整后的颜色,这样整体看来就会产生渐变的效果。可以利用窗体的Graphics对象对窗体进行绘图,该对象可以完全操控窗体的客户区。

3.一个实施例

生成渐变的蓝色背景。

(1)Form1.Designer.cs

cs 复制代码
namespace _184
{
    partial class Form1
    {
        /// <summary>
        ///  Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        ///  Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        ///  Required method for Designer support - do not modify
        ///  the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            SuspendLayout();
            // 
            // Form1
            // 
            AutoScaleDimensions = new SizeF(7F, 17F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(368, 252);
            Name = "Form1";
            StartPosition = FormStartPosition.CenterScreen;
            Text = "窗体背景渐变色";
            ResumeLayout(false);
        }

        #endregion
    }
}

(2)Form1.cs

cs 复制代码
// 窗体渐变色
namespace _184
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 重写窗体背景色
        /// </summary>
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            int intLocation, intHeight;
            intLocation = ClientRectangle.Location.Y;//为变量intLocation赋值
            intHeight = ClientRectangle.Height / 200;//为变量intHeight赋值
            for (int i = 255; i >= 0; i--)
            {
                Color color = Color.FromArgb(1, i, 100);
                SolidBrush SBrush = new(color);      //实例化一个单色画笔类对象SBrush
                Pen pen = new(SBrush, 1);            //实例化一个用于绘制直线和曲线的对象pen
                e.Graphics.DrawRectangle(pen, ClientRectangle.X, intLocation, Width, intLocation + intHeight);//绘制图形
                intLocation += intHeight;            //重新为变量intLocation赋值
            }
        }
    }
}

(3)渐变的蓝色背景

相关推荐
快乐非自愿17 分钟前
Java垃圾收集器全解:从Serial到G1的进化之旅
java·开发语言·python
树在风中摇曳21 分钟前
Java 静态成员与继承封装实战:从报错到彻底吃透核心特性
java·开发语言
芳草萋萋鹦鹉洲哦3 小时前
【Windows】tauri+rust运行打包工具链安装
开发语言·windows·rust
权泽谦3 小时前
R Shiny 交互式网页实战:从零到上线可视化应用
开发语言·信息可视化·r语言
hweiyu003 小时前
Go Fiber 简介
开发语言·后端·golang
ᐇ9596 小时前
Java LinkedList集合全面解析:双向链表的艺术与实战
java·开发语言·链表
码银6 小时前
【数据结构】顺序表
java·开发语言·数据结构
William_cl7 小时前
C# ASP.NET MVC 数据验证实战:View 层双保险(Html.ValidationMessageFor + jQuery Validate)
后端·c#·asp.net·mvc
Python私教7 小时前
Python 开发环境安装与配置全指南(2025版)
开发语言·python
百锦再7 小时前
第12章 测试编写
android·java·开发语言·python·rust·go·erlang