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)渐变的蓝色背景

相关推荐
故事和你911 分钟前
洛谷-算法2-2-常见优化技巧3
开发语言·数据结构·c++·算法·深度优先·动态规划·图论
foundbug9994 分钟前
MATLAB时频分析工具箱:基于FRFT的信号检测与参数估计
开发语言·matlab
DevilSeagull6 分钟前
Rust 方法语法:从定义到实践
开发语言·后端·rust
charlie1145141916 分钟前
通用GUI编程技术——图形渲染实战(三十七)——D3D11初始化与SwapChain:从零搭建GPU渲染框架
开发语言·c++·3d·图形渲染
陈天伟教授6 分钟前
GPT Image 2-城市海报
开发语言·人工智能·gpt·神经网络
原来是猿7 分钟前
线程安全的单例模式
linux·服务器·开发语言·单例模式·策略模式
charlie1145141917 分钟前
通用GUI编程技术——图形渲染实战(三十六)——Constant Buffer与数据传递:CPU-GPU通信通道
开发语言·c++·windows·c·图形渲染·win32
南境十里·墨染春水9 分钟前
C++笔记 STL lterator迭代器
开发语言·c++·笔记
学习使我健康11 分钟前
Android 广播介绍详情
android·开发语言·kotlin
lsx20240615 分钟前
JavaScript Array(数组)
开发语言