找不到类型或命名空间名称“GraphicsPath”(是否缺少 using 指令或程序集引用?)
找不到类型或命名空间名称“GraphicsPath”(是否缺少 using 指令或程序集引用?)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace hstwintoolbox
{
public partial class hstbutton : Button
{
public hstbutton()
{
InitializeComponent();
BackColor = Color.YellowGreen;
m_Region = CreateRoundRectangle(this.ClientRectangle, m_radius);
if (m_Region != null)
this.Region = new Region(m_Region);
this.SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.DoubleBuffer |
ControlStyles.UserPaint, true);
}
private GraphicsPath m_Region = null;
private bool IsMouseHover = false;
private bool IsMouseDown = false;
private int m_radius = 3;
private Color m_outBorderColor = Color.DarkGreen;
private Color m_upperShineColor = Color.White;
private Color m_bottomShineColor = Color.FromArgb(140, Color.White);
public int ButtonCornorRadius
{
get
{
return m_radius;
}
set
{
if (value < 0)
return;
else
m_radius = value;
}
}
public Color OutBorderColor
{
get
{
return m_outBorderColor;
}
set
{
m_outBorderColor = value;
}
}
public Color UpperShineColor
{
get
{
return m_upperShineColor;
}
set
{
m_upperShineColor = value;
}
}
public Color BottomShineColor
{
get
{
return m_bottomShineColor;
}
set
{
m_bottomShineColor = value;
}
}
protected virtual GraphicsPath CreateRoundRectangle(Rectangle rectangle, int radius)
{
GraphicsPath path = new GraphicsPath();
int l = rectangle.Left;
int t = rectangle.Top;
int w = rectangle.Width;
int h = rectangle.Height;
int d = radius << 1;
path.AddArc(l, t, d, d, 180, 90); // topleft
path.AddLine(l + radius, t, l + w - radius, t); // top
path.AddArc(l + w - d, t, d, d, 270, 90); // topright
path.AddLine(l + w, t + radius, l + w, t + h - radius); // right
path.AddArc(l + w - d, t + h - d, d, d, 0, 90); // bottomright
path.AddLine(l + w - radius, t + h, l + radius, t + h); // bottom
path.AddArc(l, t + h - d, d, d, 90, 90); // bottomleft
path.AddLine(l, t + h - radius, l, t + radius); // left
path.CloseFigure();
return path;
}
protected override void OnMouseDown(MouseEventArgs mevent)
{
IsMouseDown = true;
this.Invalidate();
base.OnMouseDown(mevent);
}
protected override void OnMouseUp(MouseEventArgs mevent)
{
IsMouseDown = false;
this.Invalidate();
base.OnMouseUp(mevent);
}
protected override void OnMouseEnter(EventArgs e)
{
IsMouseHover = true;
this.Invalidate();
base.OnMouseEnter(e);
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
IsMouseHover = false;
this.Invalidate();
}
protected override void OnGotFocus(EventArgs e)
{
IsMouseHover = true;
this.Invalidate();
base.OnGotFocus(e);
}
protected override void OnLostFocus(EventArgs e)
{
base.OnLostFocus(e);
IsMouseHover = false;
this.Invalidate();
}
protected override void OnSizeChanged(EventArgs e)
{
m_Region = CreateRoundRectangle(this.ClientRectangle, m_radius);
if (m_Region != null)
this.Region = this.Region = new Region(m_Region);
base.OnSizeChanged(e);
}
protected virtual void DrawBorder(Graphics g, Pen pen)
{
if (m_Region != null)
{
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBilinear;
g.DrawPath(pen, CreateRoundRectangle(new Rectangle(this.ClientRectangle.Left, this.ClientRectangle.Top, this.ClientRectangle.Width - 1, this.ClientRectangle.Height - 1), m_radius));
g.SmoothingMode = SmoothingMode.Default;
g.InterpolationMode = InterpolationMode.Default;
}
pen.Dispose();
pen = null;
}
protected virtual void DrawUpperShine(Graphics g)
{
Color c1 = Color.FromArgb(200, this.m_upperShineColor);
Color c2 = Color.FromArgb(100, Color.White);
Rectangle rc = new Rectangle(this.ClientRectangle.Left, this.ClientRectangle.Top, Width - 1, Height / 2);
if(IsMouseDown)
rc = new Rectangle(this.ClientRectangle.Left, this.ClientRectangle.Top, Width - 1, (Height-10) / 2);
using (LinearGradientBrush lbrush = new LinearGradientBrush(rc, c1, c2,
LinearGradientMode.Vertical))
{
g.FillRectangle(lbrush, rc);
}
}
protected virtual void DrawBottomShine(Graphics g)
{
Rectangle rc1 = new Rectangle(this.ClientRectangle.Left,
this.ClientRectangle.Top + this.ClientRectangle.Height / 2,
this.ClientRectangle.Width / 2, this.ClientRectangle.Height / 2);
Rectangle rc2 = new Rectangle(this.ClientRectangle.Left + this.ClientRectangle.Width / 2,
this.ClientRectangle.Top + this.ClientRectangle.Height / 2,
this.ClientRectangle.Width / 2, this.ClientRectangle.Height / 2);
Color c1 = BackColor;
Color c2 = m_bottomShineColor;
using (LinearGradientBrush lbrush = new LinearGradientBrush(rc1, c1, c2,
LinearGradientMode.ForwardDiagonal))
{
g.FillRectangle(lbrush, rc1);
}
using (LinearGradientBrush lbrush = new LinearGradientBrush(rc2, c1, c2,
LinearGradientMode.BackwardDiagonal))
{
g.FillRectangle(lbrush, rc2);
}
}
protected virtual void DrawDisable(Graphics g,Brush brush,Pen pen)
{
g.FillPath(brush, m_Region);
DrawBorder(g, pen);
brush.Dispose();
pen.Dispose();
brush = null;
pen = null;
}
protected virtual void DrawString(Graphics g)
{
if (this.Text.Trim() == "")
return;
int x = 0, y = 0;
int w = 0, h = 0;
Size sz = g.MeasureString(Text, Font).ToSize();
w = sz.Width > Width ? Width : sz.Width;
h = sz.Height > Height ? Height : sz.Height;
switch (TextAlign)
{
case ContentAlignment.MiddleCenter:
{
x = sz.Width > Width ? 0 : (Width - sz.Width) / 2;
y = sz.Height > Height ? 0 : (Height - sz.Height) / 2;
}
break;
case ContentAlignment.MiddleLeft:
{
x = 2;
y = sz.Height > Height ? 0 : (Height - sz.Height) / 2;
} break;
case ContentAlignment.MiddleRight:
{
x = sz.Width > sz.Width ? 0 : (Width - sz.Width) - 2;
y = sz.Height > Height ? 0 : (Height - sz.Height) / 2;
} break;
default:
{
//the other tyles in middle center.
x = sz.Width > Width ? 0 : (Width - sz.Width) / 2;
y = sz.Height > Height ? 0 : (Height - sz.Height) / 2;
} break;
}
Pen pen = new Pen(ForeColor);
if (!Enabled)
pen = new Pen(Color.Gray);
g.DrawString(Text, Font, pen.Brush, new RectangleF(
(float)x, (float)y, (float)w+3, (float)h));
pen.Dispose();
pen = null;
}
protected override void OnPaint(PaintEventArgs pevent)
{
Pen pen = new Pen(m_outBorderColor);
Brush brush = new SolidBrush(BackColor);
if (!this.Enabled)
{
DrawDisable(pevent.Graphics, brush, pen);
DrawString(pevent.Graphics);
return;
}
pevent.Graphics.FillPath(brush, m_Region);
DrawUpperShine(pevent.Graphics);
if(IsMouseHover)
DrawBottomShine(pevent.Graphics);
DrawString(pevent.Graphics);
DrawBorder(pevent.Graphics, pen);
//base.OnPaint(pevent);
}
}
}