C#創(chuàng)建背景色漸變窗體的方法實例
前言
在窗體設(shè)計時,可以通過設(shè)置窗體的BackColor屬性來改變窗口的背景顏色,但是該屬性改變后整個窗體的客戶區(qū)都會變成這種顏色,這樣顯得非常單調(diào)。如果窗體的客戶區(qū)可以像標(biāo)題欄一樣能夠體現(xiàn)顏色的漸變效果,那么窗體風(fēng)格將會另有一番風(fēng)味。
1.讓背景漸變色的理論基礎(chǔ)
在實現(xiàn)窗體背景色漸變功能時主要用到了Color結(jié)構(gòu)的FromArgb方法,Color結(jié)構(gòu)表示一種ARGB顏色(alpha、紅色、綠色和藍(lán)色),其FromArgb方法用來從指定的8位顏色值(紅色、綠色和藍(lán)色)創(chuàng)建Color結(jié)構(gòu),該方法為可重載方法,其最常用的語法格式如下:
publie static Color FromArgb(int red,int green,int blue)
FromArgb方法中的參數(shù)說明如表:
參 數(shù) | 說 明 |
red | 新Color的紅色分量值,有效值為0~255 |
green | 新Color的綠色分量值,有效值為0~255 |
blue | 新Color的藍(lán)色分量值,有效值為0~255 |
返回值 | 創(chuàng)建的Color結(jié)構(gòu) |
2.讓背景漸變色的方法
FromArgb方法就是用3種不同的色值來返回一個顏色,而稍微調(diào)整某一種顏色值就可以使整體的顏色發(fā)生細(xì)微的變化,在窗體中至上而下每行填充一種稍微調(diào)整后的顏色,這樣整體看來就會產(chǎn)生漸變的效果。可以利用窗體的Graphics對象對窗體進(jìn)行繪圖,該對象可以完全操控窗體的客戶區(qū)。
3.一個實施例
生成漸變的藍(lán)色背景。
(1)Form1.Designer.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
// 窗體漸變色 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)漸變的藍(lán)色背景
到此這篇關(guān)于C#創(chuàng)建背景色漸變窗體的方法實例的文章就介紹到這了,更多相關(guān)C#創(chuàng)建背景色漸變窗體內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#?從字符串中分離文件路徑、文件名及擴(kuò)展名的操作
在C#中,處理文件時經(jīng)常需要分離文件路徑、文件名和擴(kuò)展名,通過使用Microsoft.Win32命名空間和字符串處理方法如Substring、IndexOf()和LastIndexOf(),可以有效地獲取和操作這些信息,本文介紹C#?字符串中分離文件路徑,感興趣的朋友一起看看2024-09-09快速解決C# android base-64 字符數(shù)組的無效長度問題
下面小編就為大家?guī)硪黄焖俳鉀QC# android base-64 字符數(shù)組的無效長度問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-08-08DevExpress獲取節(jié)點下可視區(qū)域子節(jié)點集合的實現(xiàn)方法
這篇文章主要介紹了DevExpress獲取節(jié)點下可視區(qū)域子節(jié)點集合的實現(xiàn)方法,是C#程序設(shè)計中較為常見的技巧,需要的朋友可以參考下2014-08-08