Ok, after Darren Neimke told me I should start blogging again - I thought for a while what I should blog about; what knowledge do I have that I should share with you guys? I decided that graphical programming is something I have learned to master over the years, so I decided I should start posting about GDI+ and DirectX. I am hoping that I can show off some tricks off the trade and perhaps help people who are new to GDI(+) and/or DirectX with some small snippets of code every now and then... Here's the first little snippet.
Ever wanted to enhance the standard, boring WinForms controls? The Control below shows you how simple it is to make a more neat looking Label control - Enjoy!
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace CustomControls
{
public class CustomLabel : Label
{
private Color _backColor1;
private Color _backColor2;
[ToolboxItem( true ), Category( "Appearance" ), DefaultValue( typeof( Color ), "AliceBlue" )]
public Color BackColor1
{
get { return _backColor1; }
set { _backColor1 = value; }
}
[ToolboxItem( true ), Category( "Appearance" ), DefaultValue( typeof( Color ), "DodgerBlue" )]
public Color BackColor2
{
get { return _backColor2; }
set { _backColor2 = value; }
}
public CustomLabel()
{
BackColor1 = Color.AliceBlue;
BackColor2 = Color.DodgerBlue;
}
protected override void OnPaintBackground( PaintEventArgs pe )
{
Graphics g = pe.Graphics;
Rectangle rect = pe.ClipRectangle;
LinearGradientBrush gradientBrush = new LinearGradientBrush( rect, BackColor1, BackColor2, 70 );
g.Clear( Parent.BackColor );
g.FillRectangle( gradientBrush, rect );
}
protected override void OnResize( EventArgs e )
{
Refresh();
base.OnResize( e );
}
}
}