Gradient Panel Demo
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Design;
public class Form1 : Form
{
private GradientPanel gradientPanel1;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
public Form1() {
InitializeComponent();
}
private void InitializeComponent()
{
this.gradientPanel1 = new GradientPanel();
this.label3 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.gradientPanel1.SuspendLayout();
this.SuspendLayout();
//
// gradientPanel1
//
this.gradientPanel1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.gradientPanel1.AutoScroll = true;
this.gradientPanel1.Controls.Add(this.label3);
this.gradientPanel1.Controls.Add(this.button1);
this.gradientPanel1.Controls.Add(this.label2);
this.gradientPanel1.Controls.Add(this.label1);
this.gradientPanel1.Location = new System.Drawing.Point(4, 6);
this.gradientPanel1.Name = "gradientPanel1";
this.gradientPanel1.Size = new System.Drawing.Size(427, 190);
this.gradientPanel1.TabIndex = 1;
//
// label3
//
this.label3.BackColor = System.Drawing.Color.Transparent;
this.label3.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label3.ForeColor = System.Drawing.Color.White;
this.label3.Location = new System.Drawing.Point(18, 57);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(328, 39);
this.label3.TabIndex = 2;
this.label3.Text = "text." +
"";
//
// button1
//
this.button1.Location = new System.Drawing.Point(231, 109);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "Test Button";
//
// label2
//
this.label2.AutoSize = true;
this.label2.BackColor = System.Drawing.Color.Transparent;
this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label2.ForeColor = System.Drawing.Color.White;
this.label2.Location = new System.Drawing.Point(40, 226);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(108, 37);
this.label2.TabIndex = 1;
this.label2.Text = "label2";
//
// label1
//
this.label1.AutoSize = true;
this.label1.BackColor = System.Drawing.Color.Transparent;
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label1.ForeColor = System.Drawing.Color.White;
this.label1.Location = new System.Drawing.Point(17, 12);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(277, 37);
this.label1.TabIndex = 0;
this.label1.Text = "Transparent Text";
//
// GradientPanelTest
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(435, 260);
this.Controls.Add(this.gradientPanel1);
this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Name = "GradientPanelTest";
this.Text = "GradientPanel";
this.gradientPanel1.ResumeLayout(false);
this.gradientPanel1.PerformLayout();
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
public class GradientPanel : Panel
{
private Color ColorA = Color.LightBlue;
private Color ColorB = Color.Red;
private LinearGradientMode GradientFillStyle = LinearGradientMode.ForwardDiagonal;
private Brush gradientBrush;
public GradientPanel()
{
handlerGradientChanged = new EventHandler(GradientChanged);
ResizeRedraw = true;
}
private EventHandler handlerGradientChanged;
protected override void OnPaintBackground( System.Windows.Forms.PaintEventArgs e)
{
gradientBrush = new LinearGradientBrush(ClientRectangle, ColorA, ColorB, GradientFillStyle);
e.Graphics.FillRectangle(gradientBrush, ClientRectangle);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (gradientBrush != null) gradientBrush.Dispose();
}
base.Dispose(disposing);
}
protected override void OnScroll(ScrollEventArgs se)
{
Invalidate();
}
private void GradientChanged(object sender, EventArgs e)
{
if (gradientBrush != null) gradientBrush.Dispose();
gradientBrush = null;
Invalidate();
}
}
|
HTML code for linking to this page:
|
|