برای ایجاد طیف نوری برای فرم ابتدا یک برنامه ویندوزی ایجاد کرده و سپس با زدن دکمه F7 به محیط کدنویسی رفته
و کدهای زیر را در جایگزین کدهای آن کنید :
کد گرادیانت برای سی شارپ :
using System.Drawing; using System.Windows.Forms; using System.Drawing.Drawing2D; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public void PaintGradient(Control control1, LinearGradientMode _direction, Color _gradientColorStart, Color _gradientColorEnd) { LinearGradientBrush gradBrush; gradBrush = new LinearGradientBrush(new Rectangle(0, 0, control1.Width, control1.Height), _gradientColorStart, _gradientColorEnd, _direction); Bitmap bmp = new Bitmap(control1.Width, control1.Height); Graphics g = Graphics.FromImage(bmp); g.FillRectangle(gradBrush, new Rectangle(0, 0, control1.Width, control1.Height)); control1.BackgroundImage = bmp; control1.BackgroundImageLayout = ImageLayout.Stretch; } private void Form1_Paint(object sender, PaintEventArgs e) { PaintGradient(this, LinearGradientMode.Vertical, Color.RoyalBlue, Color.DarkBlue); } } }
کد گرادیانت برای VB.NET :
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Drawing.Drawing2D
Public Class Form1
Public Sub PaintGradient(control1 As Control, _direction As LinearGradientMode, _gradientColorStart As Color, _gradientColorEnd As Color)
Dim gradBrush As LinearGradientBrush
gradBrush = New LinearGradientBrush(New Rectangle(0, 0, control1.Width, control1.Height), _gradientColorStart, _gradientColorEnd, _direction)
Dim bmp As New Bitmap(control1.Width, control1.Height)
Dim g As Graphics = Graphics.FromImage(bmp)
g.FillRectangle(gradBrush, New Rectangle(0, 0, control1.Width, control1.Height))
control1.BackgroundImage = bmp
control1.BackgroundImageLayout = ImageLayout.Stretch
End Sub
Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
PaintGradient(Me, LinearGradientMode.Vertical, Color.RoyalBlue, Color.DarkBlue)
End Sub
End Class
حالا نتیجه را مشاهده نمایید :