using System;
using System.Drawing;
using System.Text;
using System.Reflection;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace UniverseReport
{
internal enum State
{
Normal = 1,
MouseOver = 2,
MouseDown = 3,
Disable = 4,
Default = 5
}
[Serializable]
internal class ThemedButton : Button
{
private State state = State.Normal;
public ThemedButton()
{
try
{
this.SetStyle(ControlStyles.DoubleBuffer,true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint,true);
this.SetStyle(ControlStyles.UserPaint,true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor,true);
this.SetStyle(ControlStyles.StandardDoubleClick,false);
this.SetStyle(ControlStyles.Selectable,true);
this.ResizeRedraw = true;
}
catch { }
}
protected override void OnMouseEnter(EventArgs e)
{
state = State.MouseOver;
this.Invalidate();
base.OnMouseEnter(e);
}
protected override void OnMouseLeave(EventArgs e)
{
state = State.Normal;
this.Invalidate();
base.OnMouseLeave(e);
}
protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
if ((e.Button & MouseButtons.Left) != MouseButtons.Left) return;
state = State.MouseDown;
this.Invalidate();
base.OnMouseDown(e);
}
protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
{
if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
state = State.Normal;
this.Invalidate();
base.OnMouseUp(e);
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
if (SkinImage.button.img == null)
{
base.OnPaint(e);
return;
}
int i = (int)state;
if (this.Focused) i = 5;
Rectangle rc = this.ClientRectangle;
Graphics g = e.Graphics;
base.InvokePaintBackground(this,new PaintEventArgs(e.Graphics,base.ClientRectangle));
SkinDraw.DrawRect2(g,SkinImage.button,rc,i);
Image img = null;
Size txts,imgs;
txts = Size.Empty;
imgs = Size.Empty;
if (this.Image != null)
{
img = this.Image;
}
else if (this.ImageList != null && this.ImageIndex != -1)
{
img = this.ImageList.Images[this.ImageIndex];
}
if (img != null)
{
imgs.Width = img.Width;
imgs.Height = img.Height;
}
StringFormat format1;
using (format1 = new StringFormat())
{
SizeF ef1 = g.MeasureString(this.Text,this.Font,new SizeF((float)rc.Width,(float)rc.Height),format1);
txts = Size.Ceiling(ef1);
}
rc.Inflate(-4,-4);
if (imgs.Width * imgs.Height != 0)
{
Rectangle imgr = rc;
imgr = SkinDraw.HAlignWithin(imgs,imgr,this.ImageAlign);
imgr = SkinDraw.VAlignWithin(imgs,imgr,this.ImageAlign);
if (!this.Enabled)
{
ControlPaint.DrawImageDisabled(g,img,imgr.Left,imgr.Top,this.BackColor);
}
else
{
g.DrawImage(img,imgr.Left,imgr.Top,img.Width,img.Height);
}
}
Rectangle txtr = rc;
txtr = SkinDraw.HAlignWithin(txts,txtr,this.TextAlign);
txtr = SkinDraw.VAlignWithin(txts,txtr,this.TextAlign);
Brush brush2;
format1 = new StringFormat();
if (this.RightToLeft == RightToLeft.Yes)
{
format1.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
}
brush2 = new SolidBrush(this.ForeColor);
g.DrawString(this.Text,this.Font,brush2,(RectangleF)txtr,format1);
brush2.Dispose();
}
}
}