- 注册时间
- 2011-5-26
- 最后登录
- 2013-9-17
- 在线时间
- 164 小时
- 阅读权限
- 100
- 积分
- 7890
- 帖子
- 992
- 精华
- 1
- UID
- 73
|
本帖最后由 cjqq0218 于 2011-7-13 17:46 编辑
- namespace Cj_Control
- {
- using System;
- using System.ComponentModel;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Windows.Forms;
- [DefaultEvent("Click")]
- public class ButtonsMax : UserControl
- {
- private Color BoundsInside = Color.White;
- private Color BoundsOutside = Color.FromArgb(80, 0x8b, 0xc9);
- private IContainer components;
- private Color FontBackColor = Color.White;
- private Color LinearGradientDown1 = Color.FromArgb(0xe3, 0xeb, 0xf6);
- private Color LinearGradientDown2 = Color.FromArgb(0xd7, 0xe3, 0xf3);
- private Color LinearGradientEnter1 = Color.FromArgb(0xe3, 0xeb, 0xf6);
- private Color LinearGradientEnter2 = Color.FromArgb(0xfe, 0xfe, 0xff);
- private Color LinearGradientUp1 = Color.FromArgb(0xf5, 0xf7, 250);
- private Color LinearGradientUp2 = Color.FromArgb(240, 0xf4, 250);
- private Mouse_DrawState MDS = Mouse_DrawState.MouseLever;
- private bool MouseCBL;
- private string strText = string.Empty;
- public ButtonsMax()
- {
- this.InitializeComponent();
- //开启双缓存
- this.DoubleBuffered = true;
- base.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
- this.ChangeRegion();
- this.ForeColor = Color.FromArgb(80, 0x8b, 0xc9);
- this.DrawText = base.Name;
- }
- /// <summary>
- /// 切割圆角
- /// </summary>
- private void ChangeRegion()
- {
- GraphicsPath path = new GraphicsPath(FillMode.Winding);
- path.AddLines(Fillet_Bounds(0, 0, base.Width, base.Height));
- base.Region = new Region(path);
- path.Dispose();
- }
- /// <summary>
- /// 为圆角瞄点
- /// </summary>
- /// <param name="nLeftEdge">起点X坐标</param>
- /// <param name="nTopEdge">起点Y坐标</param>
- /// <param name="nWidth">宽</param>
- /// <param name="nHeight">高</param>
- /// <returns></returns>
- internal static Point[] Fillet_Bounds(int nLeftEdge, int nTopEdge, int nWidth, int nHeight)
- {
- int x = nWidth;
- int y = nHeight;
- Point[] pointArray = new Point[] { new Point(1, 0), new Point(x - 1, 0), new Point(x - 1, 1), new Point(x, 1), new Point(x, y - 1), new Point(x - 1, y - 1), new Point(x - 1, y), new Point(1, y), new Point(1, y - 1), new Point(0, y - 1), new Point(0, 1), new Point(1, 1) };
- for (int i = 0; i < pointArray.Length; i++)
- {
- pointArray.Offset(nLeftEdge, nTopEdge);
- }
- return pointArray;
- }
- protected override void Dispose(bool disposing)
- {
- if (disposing && (this.components != null))
- {
- this.components.Dispose();
- }
- base.Dispose(disposing);
- }
- private void InitializeComponent()
- {
- base.SuspendLayout();
- base.AutoScaleDimensions = new SizeF(6f, 12f);
- //base.AutoScaleMode = AutoScaleMode.Font;
- //base.Name = "ButtonsMax";
- base.Size = new Size(0x57, 0x1d);
- base.ResumeLayout(false);
- }
- // DialogResult
-
- protected override void OnClick(EventArgs e)
- {
- if (this.MouseCBL)
- {
- base.OnClick(e);
- }
- }
- protected override void OnMouseDown(MouseEventArgs e)
- {
- //因为是继承自UserControl ,所以要判断是否是鼠标左键点击
- if (e.Button == MouseButtons.Left)
- {
- this.MouseCBL = true;
- this.MDS = Mouse_DrawState.MouseDown;
- base.Invalidate();
- }
- }
- protected override void OnMouseEnter(EventArgs e)
- {
- this.MDS = Mouse_DrawState.MouseEnter;
- base.Invalidate();
- }
- protected override void OnMouseLeave(EventArgs e)
- {
- this.MDS = Mouse_DrawState.MouseLever;
- this.MouseCBL = false;
- base.Invalidate();
- }
- protected override void OnMouseUp(MouseEventArgs e)
- {
- if (!base.IsDisposed && (e.Button == MouseButtons.Left))
- {
- this.MDS = Mouse_DrawState.MouseUp;
- this.MouseCBL = false;
- base.Invalidate();
- }
- }
- protected override void OnPaint(PaintEventArgs e)
- {
- Graphics graphics = e.Graphics;
- RectangleF rect = new RectangleF(0f, 0f, (float)base.Width, ((float)base.Height) / 2f);
- RectangleF ef2 = new RectangleF(0f, ((float)base.Height) / 2f, (float)base.Width, (float)(base.Height / 2));
- LinearGradientBrush brush = new LinearGradientBrush(rect, this.SG_LinearGradientUp1, this.SG_LinearGradientUp2, LinearGradientMode.Vertical);
- LinearGradientBrush brush2 = new LinearGradientBrush(rect, this.SG_LinearGradientDown1, this.SG_LinearGradientDown2, LinearGradientMode.Vertical);
-
- //判断状态,选择适合的画笔、颜色
- if (this.MDS == Mouse_DrawState.MouseDown)
- {
- brush = new LinearGradientBrush(rect, this.SG_LinearGradientDown2, this.SG_LinearGradientDown1, LinearGradientMode.Vertical);
- brush2 = new LinearGradientBrush(rect, this.SG_LinearGradientUp2, this.SG_LinearGradientUp1, LinearGradientMode.Vertical);
- }
- if (this.MDS == Mouse_DrawState.MouseEnter)
- {
- brush = new LinearGradientBrush(rect, this.SG_LinearGradientUp1, this.SG_LinearGradientUp2, LinearGradientMode.Vertical);
- brush2 = new LinearGradientBrush(ef2, this.SG_LinearGradientEnter1, this.SG_LinearGradientEnter2, LinearGradientMode.Vertical);
- }
- graphics.FillRectangle(brush, rect);
- graphics.FillRectangle(brush2, ef2);
- Pen pen = new Pen(this.SG_BoundsOutside, 1f);
- graphics.DrawLines(pen, Fillet_Bounds(0, 0, base.Width - 1, base.Height - 1));
- pen.Color = this.SG_BoundsInside;
- graphics.DrawLines(pen, Fillet_Bounds(1, 1, base.Width - 3, base.Height - 3));
- pen.Dispose();
- //绘制字体
- StringFormat format = new StringFormat();
- format.LineAlignment = StringAlignment.Center;
- format.Alignment = StringAlignment.Center;
- SolidBrush brush3 = new SolidBrush(this.SG_FontBackColor);
- RectangleF layoutRectangle = new RectangleF(0f, 0f, (float)base.Width, (float)base.Height);
- //绘制字体阴影
- graphics.DrawString(this.DrawText, this.Font, brush3, layoutRectangle, format);
- brush3.Color = this.ForeColor;
- layoutRectangle.Offset(0f, 1f);
- //绘制字体
- graphics.DrawString(this.DrawText, this.Font, brush3, layoutRectangle, format);
- brush3.Dispose();
- pen.Dispose();
- format.Dispose();
- }
- protected override void OnSizeChanged(EventArgs e)
- {
- base.OnSizeChanged(e);
- this.ChangeRegion();
- }
复制代码 //------------------------------------------------------------未完--------------------------------- |
-
2
查看全部评分
-
|