Tuesday, July 15, 2008

How to create Action Lable in .Net compact framework.

Hi Guys,

Now i'm back in my blog, so i have plan post some cool stuff in my blog every day.

According that, today i will post a code for , how to create Action Lable in .NETCF.

It's pretty simple, Because of we dont need play with P/Invoke. but we want to play with GDI.

Because of i want to create a graphical objects,

Here complete code for you.


using System;
using System.Windows.Forms;
using System.Drawing;

namespace RaveSoftBlog
{
///
/// Arrow label button control.
///

public class ActionLabel : Control
{
class Const
{
public static Color DisableColor = Color.FromArgb(150,150,80);
public static Color PushedColor = Color.FromArgb(160,100,0);
public static Color ForeColor = Color.FromArgb(90,90,45);
public static Color BulletColor = Color.FromArgb(180,180,110);
public const string FontName = "Arial";
public const int FontSize = 10;
public const int BulletSize = 8;
}

// internal fields
bool m_pushed;
Rectangle m_rcHitArea;
Point[] m_bulletPts;

// gdi objects
Bitmap m_bmp;
Font m_font;
Pen m_penPushed, m_penFore, m_penDisabled;
Brush m_brushPushed, m_brushFore, m_brushDisabled;

// ctor
public ActionLabelControl()
{
// colors
this.ForeColor = Const.ForeColor;

// gdi objects
CreateGdiObjects();
}

protected override void OnPaint(PaintEventArgs e)
{
// draw to memory bitmap
CreateMemoryBitmap(e.Graphics);
Graphics g = Graphics.FromImage(m_bmp);
DrawLabel(g);

// blit memory bitmap to screen
e.Graphics.DrawImage(m_bmp, 0, 0);
}

protected override void OnPaintBackground(PaintEventArgs e)
{
// don't pass to base since we paint everything, avoid flashing
}

protected override void OnEnabledChanged(EventArgs e)
{
// redraw when enabled state changes
Invalidate();
}

// draw label and arrow
private void DrawLabel(Graphics g)
{
// background
g.Clear(Parent.BackColor);

// determine what pen and brush to use
Pen pen = m_pushed ? m_penPushed :
(this.Enabled ? m_penFore : m_penDisabled);

Brush brush = m_pushed ? m_brushPushed :
(this.Enabled ? m_brushFore : m_brushDisabled);

// draw solid arrow if enabled
if (this.Enabled)
g.FillPolygon(brush, m_bulletPts);

g.DrawPolygon(pen, m_bulletPts);

// label text
Size textSize = g.MeasureString(Text, m_font).ToSize();
g.DrawString(Text, m_font, brush,
Const.BulletSize+8, (this.Height - textSize.Height)/2);
}

protected override void OnMouseDown (MouseEventArgs e)
{
base.OnMouseDown(e);
this.Capture = true;

// see if clicked on label text or arrow
if (m_rcHitArea.Contains(e.X, e.Y))
{
m_pushed = true;
Invalidate();
}
}

protected override void OnMouseUp (MouseEventArgs e)
{
base.OnMouseUp(e);
this.Capture = false;
m_pushed = false;
Invalidate();
}

protected override void OnClick(EventArgs e)
{
if (m_pushed)
base.OnClick(e);
}

private void CreateGdiObjects()
{
// gdi objects
m_font = new Font(Const.FontName, Const.FontSize, FontStyle.Bold);
m_penPushed = new Pen(Const.PushedColor);
m_penFore = new Pen(this.ForeColor);
m_penDisabled = new Pen(Const.DisableColor);
m_brushPushed = new SolidBrush(Const.PushedColor);
m_brushFore = new SolidBrush(this.ForeColor);
m_brushDisabled = new SolidBrush(Const.DisableColor);
}

private void CreateMemoryBitmap(Graphics g)
{
// see if need to create gdi objects
if (m_bmp == null || m_bmp.Width != this.Width || m_bmp.Height != this.Height)
{
// memory bitmap
m_bmp = new Bitmap(this.Width, this.Height);

// bullet points
int halfHeight = this.Height/2;
m_bulletPts = new Point[3];
m_bulletPts[0] = new Point(0, halfHeight-(Const.BulletSize/2));
m_bulletPts[1] = new Point(Const.BulletSize, halfHeight);
m_bulletPts[2] = new Point(0, halfHeight+(Const.BulletSize/2));

// hit area
Size textSize = g.MeasureString(Text, m_font).ToSize();
m_rcHitArea = new Rectangle(0,
(this.Height - textSize.Height)/2,
textSize.Width + Const.BulletSize + 8,
textSize.Height);
}
}
}
}

Thank you

No comments: