设为首页收藏本站

Scripts 学盟

 找回密码
 加入学盟

QQ登录

只需一步,快速开始

查看: 1974|回复: 7
打印 上一主题 下一主题

我3年前写的东西,今天拿出来给大家分享下:C#按钮 [复制链接]

Rank: 7Rank: 7Rank: 7

跳转到指定楼层
1#
俊俊 实名认证  发表于 2011-7-7 00:00:47 |只看该作者 |倒序浏览
本帖最后由 cjqq0218 于 2011-7-13 17:46 编辑
  1. namespace Cj_Control
  2. {
  3.     using System;
  4.     using System.ComponentModel;
  5.     using System.Drawing;
  6.     using System.Drawing.Drawing2D;
  7.     using System.Windows.Forms;
  8.     [DefaultEvent("Click")]
  9.     public class ButtonsMax : UserControl
  10.     {
  11.         private Color BoundsInside = Color.White;
  12.         private Color BoundsOutside = Color.FromArgb(80, 0x8b, 0xc9);
  13.         private IContainer components;
  14.         private Color FontBackColor = Color.White;
  15.         private Color LinearGradientDown1 = Color.FromArgb(0xe3, 0xeb, 0xf6);
  16.         private Color LinearGradientDown2 = Color.FromArgb(0xd7, 0xe3, 0xf3);
  17.         private Color LinearGradientEnter1 = Color.FromArgb(0xe3, 0xeb, 0xf6);
  18.         private Color LinearGradientEnter2 = Color.FromArgb(0xfe, 0xfe, 0xff);
  19.         private Color LinearGradientUp1 = Color.FromArgb(0xf5, 0xf7, 250);
  20.         private Color LinearGradientUp2 = Color.FromArgb(240, 0xf4, 250);
  21.         private Mouse_DrawState MDS = Mouse_DrawState.MouseLever;
  22.         private bool MouseCBL;
  23.         private string strText = string.Empty;
  24.         public ButtonsMax()
  25.         {
  26.             this.InitializeComponent();
  27.             //开启双缓存
  28.             this.DoubleBuffered = true;
  29.             base.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
  30.             this.ChangeRegion();
  31.             this.ForeColor = Color.FromArgb(80, 0x8b, 0xc9);
  32.             this.DrawText = base.Name;
  33.         }
  34.         /// <summary>
  35.         /// 切割圆角
  36.         /// </summary>
  37.         private void ChangeRegion()
  38.         {
  39.             GraphicsPath path = new GraphicsPath(FillMode.Winding);
  40.             path.AddLines(Fillet_Bounds(0, 0, base.Width, base.Height));
  41.             base.Region = new Region(path);
  42.             path.Dispose();
  43.         }
  44.         /// <summary>
  45.         /// 为圆角瞄点
  46.         /// </summary>
  47.         /// <param name="nLeftEdge">起点X坐标</param>
  48.         /// <param name="nTopEdge">起点Y坐标</param>
  49.         /// <param name="nWidth">宽</param>
  50.         /// <param name="nHeight">高</param>
  51.         /// <returns></returns>
  52.         internal static Point[] Fillet_Bounds(int nLeftEdge, int nTopEdge, int nWidth, int nHeight)
  53.         {
  54.             int x = nWidth;
  55.             int y = nHeight;
  56.             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) };
  57.             for (int i = 0; i < pointArray.Length; i++)
  58.             {
  59.                 pointArray.Offset(nLeftEdge, nTopEdge);
  60.             }
  61.             return pointArray;
  62.         }

  63.         protected override void Dispose(bool disposing)
  64.         {
  65.             if (disposing && (this.components != null))
  66.             {
  67.                 this.components.Dispose();
  68.             }
  69.             base.Dispose(disposing);
  70.         }
  71.         private void InitializeComponent()
  72.         {
  73.             base.SuspendLayout();
  74.             base.AutoScaleDimensions = new SizeF(6f, 12f);
  75.             //base.AutoScaleMode = AutoScaleMode.Font;
  76.             //base.Name = "ButtonsMax";
  77.             base.Size = new Size(0x57, 0x1d);
  78.             base.ResumeLayout(false);
  79.         }
  80.          //   DialogResult
  81.          
  82.         protected override void OnClick(EventArgs e)
  83.         {
  84.             if (this.MouseCBL)
  85.             {
  86.                 base.OnClick(e);
  87.             }
  88.         }
  89.         protected override void OnMouseDown(MouseEventArgs e)
  90.         {
  91.             //因为是继承自UserControl ,所以要判断是否是鼠标左键点击
  92.             if (e.Button == MouseButtons.Left)
  93.             {
  94.                 this.MouseCBL = true;
  95.                 this.MDS = Mouse_DrawState.MouseDown;
  96.                 base.Invalidate();
  97.             }
  98.         }
  99.         protected override void OnMouseEnter(EventArgs e)
  100.         {
  101.             this.MDS = Mouse_DrawState.MouseEnter;
  102.             base.Invalidate();
  103.         }
  104.         protected override void OnMouseLeave(EventArgs e)
  105.         {
  106.             this.MDS = Mouse_DrawState.MouseLever;
  107.             this.MouseCBL = false;
  108.             base.Invalidate();
  109.         }
  110.         protected override void OnMouseUp(MouseEventArgs e)
  111.         {
  112.             if (!base.IsDisposed && (e.Button == MouseButtons.Left))
  113.             {
  114.                 this.MDS = Mouse_DrawState.MouseUp;
  115.                 this.MouseCBL = false;
  116.                 base.Invalidate();
  117.             }
  118.         }
  119.         protected override void OnPaint(PaintEventArgs e)
  120.         {
  121.             Graphics graphics = e.Graphics;
  122.             RectangleF rect = new RectangleF(0f, 0f, (float)base.Width, ((float)base.Height) / 2f);
  123.             RectangleF ef2 = new RectangleF(0f, ((float)base.Height) / 2f, (float)base.Width, (float)(base.Height / 2));
  124.             LinearGradientBrush brush = new LinearGradientBrush(rect, this.SG_LinearGradientUp1, this.SG_LinearGradientUp2, LinearGradientMode.Vertical);
  125.             LinearGradientBrush brush2 = new LinearGradientBrush(rect, this.SG_LinearGradientDown1, this.SG_LinearGradientDown2, LinearGradientMode.Vertical);
  126.             
  127.             //判断状态,选择适合的画笔、颜色
  128.             if (this.MDS == Mouse_DrawState.MouseDown)
  129.             {
  130.                 brush = new LinearGradientBrush(rect, this.SG_LinearGradientDown2, this.SG_LinearGradientDown1, LinearGradientMode.Vertical);
  131.                 brush2 = new LinearGradientBrush(rect, this.SG_LinearGradientUp2, this.SG_LinearGradientUp1, LinearGradientMode.Vertical);
  132.             }
  133.             if (this.MDS == Mouse_DrawState.MouseEnter)
  134.             {
  135.                 brush = new LinearGradientBrush(rect, this.SG_LinearGradientUp1, this.SG_LinearGradientUp2, LinearGradientMode.Vertical);
  136.                 brush2 = new LinearGradientBrush(ef2, this.SG_LinearGradientEnter1, this.SG_LinearGradientEnter2, LinearGradientMode.Vertical);
  137.             }
  138.             graphics.FillRectangle(brush, rect);
  139.             graphics.FillRectangle(brush2, ef2);
  140.             Pen pen = new Pen(this.SG_BoundsOutside, 1f);
  141.             graphics.DrawLines(pen, Fillet_Bounds(0, 0, base.Width - 1, base.Height - 1));
  142.             pen.Color = this.SG_BoundsInside;
  143.             graphics.DrawLines(pen, Fillet_Bounds(1, 1, base.Width - 3, base.Height - 3));
  144.             pen.Dispose();
  145.             //绘制字体
  146.             StringFormat format = new StringFormat();
  147.             format.LineAlignment = StringAlignment.Center;
  148.             format.Alignment = StringAlignment.Center;
  149.             SolidBrush brush3 = new SolidBrush(this.SG_FontBackColor);
  150.             RectangleF layoutRectangle = new RectangleF(0f, 0f, (float)base.Width, (float)base.Height);
  151.             //绘制字体阴影
  152.             graphics.DrawString(this.DrawText, this.Font, brush3, layoutRectangle, format);
  153.             brush3.Color = this.ForeColor;
  154.             layoutRectangle.Offset(0f, 1f);
  155.             //绘制字体
  156.             graphics.DrawString(this.DrawText, this.Font, brush3, layoutRectangle, format);
  157.             brush3.Dispose();
  158.             pen.Dispose();
  159.             format.Dispose();
  160.         }
  161.         protected override void OnSizeChanged(EventArgs e)
  162.         {
  163.             base.OnSizeChanged(e);
  164.             this.ChangeRegion();
  165.         }
复制代码
//------------------------------------------------------------未完---------------------------------
2

查看全部评分

分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
分享分享0 收藏收藏0

Rank: 7Rank: 7Rank: 7

2#
俊俊 实名认证  发表于 2011-7-7 00:01:24 |只看该作者
本帖最后由 cjqq0218 于 2011-7-13 17:47 编辑

//------------------------------------接上-----------------------------------------
  1. [EditorBrowsable(EditorBrowsableState.Always), Category("Font"), Browsable(true), Description("设置或获取文本")]
  2.         public string DrawText
  3.         {
  4.             get
  5.             {
  6.                 return this.strText;
  7.             }
  8.             set
  9.             {
  10.                 this.strText = value;
  11.                 base.Invalidate();
  12.             }
  13.         }
  14.         [Description("设置或获取文本前景色"), Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
  15.         public override Color ForeColor
  16.         {
  17.             get
  18.             {
  19.                 return base.ForeColor;
  20.             }
  21.             set
  22.             {
  23.                 base.ForeColor = value;
  24.                 base.Invalidate();
  25.             }
  26.         }
  27.         [Category("Color"), EditorBrowsable(EditorBrowsableState.Always), Browsable(true), Description("设置或获内边框色")]
  28.         public Color SG_BoundsInside
  29.         {
  30.             get
  31.             {
  32.                 return this.BoundsInside;
  33.             }
  34.             set
  35.             {
  36.                 this.BoundsInside = value;
  37.                 base.Invalidate();
  38.             }
  39.         }
  40.         [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), Description("设置或获取外边框色"), Category("Color")]
  41.         public Color SG_BoundsOutside
  42.         {
  43.             get
  44.             {
  45.                 return this.BoundsOutside;
  46.             }
  47.             set
  48.             {
  49.                 this.BoundsOutside = value;
  50.                 base.Invalidate();
  51.             }
  52.         }
  53.         [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), Description("设置或获取字体背景色"), Category("Color")]
  54.         public Color SG_FontBackColor
  55.         {
  56.             get
  57.             {
  58.                 return this.FontBackColor;
  59.             }
  60.             set
  61.             {
  62.                 this.FontBackColor = value;
  63.                 base.Invalidate();
  64.             }
  65.         }
  66.         [Browsable(true), EditorBrowsable(EditorBrowsableState.Always), Description("设置或获取渐变Down_1色"), Category("Color")]
  67.         public Color SG_LinearGradientDown1
  68.         {
  69.             get
  70.             {
  71.                 return this.LinearGradientDown1;
  72.             }
  73.             set
  74.             {
  75.                 this.LinearGradientDown1 = value;
  76.                 base.Invalidate();
  77.             }
  78.         }
  79.         [Category("Color"), EditorBrowsable(EditorBrowsableState.Always), Browsable(true), Description("设置或获取渐变Down_2色")]
  80.         public Color SG_LinearGradientDown2
  81.         {
  82.             get
  83.             {
  84.                 return this.LinearGradientDown2;
  85.             }
  86.             set
  87.             {
  88.                 this.LinearGradientDown2 = value;
  89.                 base.Invalidate();
  90.             }
  91.         }
  92.         [Browsable(true), EditorBrowsable(EditorBrowsableState.Always), Category("Color"), Description("设置或获取渐变色_鼠标进入1")]
  93.         public Color SG_LinearGradientEnter1
  94.         {
  95.             get
  96.             {
  97.                 return this.LinearGradientEnter1;
  98.             }
  99.             set
  100.             {
  101.                 this.LinearGradientEnter1 = value;
  102.                 base.Invalidate();
  103.             }
  104.         }
  105.         [EditorBrowsable(EditorBrowsableState.Always), Category("Color"), Browsable(true), Description("设置或获取渐变色_鼠标进入2")]
  106.         public Color SG_LinearGradientEnter2
  107.         {
  108.             get
  109.             {
  110.                 return this.LinearGradientEnter2;
  111.             }
  112.             set
  113.             {
  114.                 this.LinearGradientEnter2 = value;
  115.                 base.Invalidate();
  116.             }
  117.         }
  118.         [Browsable(true), EditorBrowsable(EditorBrowsableState.Always), Category("Color"), Description("设置或获取渐变Up_1色")]
  119.         public Color SG_LinearGradientUp1
  120.         {
  121.             get
  122.             {
  123.                 return this.LinearGradientUp1;
  124.             }
  125.             set
  126.             {
  127.                 this.LinearGradientUp1 = value;
  128.                 base.Invalidate();
  129.             }
  130.         }
  131.         [EditorBrowsable(EditorBrowsableState.Always), Category("Color"), Browsable(true), Description("设置或获取渐变Up_2色")]
  132.         public Color SG_LinearGradientUp2
  133.         {
  134.             get
  135.             {
  136.                 return this.LinearGradientUp2;
  137.             }
  138.             set
  139.             {
  140.                 this.LinearGradientUp2 = value;
  141.                 base.Invalidate();
  142.             }
  143.         }
  144.     }
  145.     /// <summary>
  146.     /// 绘制的状态
  147.     /// </summary>
  148.     public enum DrawState
  149.     {
  150.         Normal,
  151.         Hot,
  152.         Pressed,
  153.         Disable,
  154.         Focus
  155.     }
  156.     /// <summary>
  157.     /// 鼠标状态
  158.     /// </summary>
  159.     internal enum Mouse_DrawState
  160.     {
  161.         MouseEnter,
  162.         MouseLever,
  163.         MouseDown,
  164.         MouseUp
  165.     }
  166. }
复制代码
//这个按钮到目前为止已经差不多了,唯一没有实现的功能就是 当 Enabled = false;的时候没有绘制,因为我不是美工,有兴趣的朋友可以拿去用,嘿嘿

使用道具 举报

管理员

超级大菜鸟

Rank: 9Rank: 9Rank: 9

3#
混混@普宁.中国 实名认证  发表于 2011-7-7 00:06:39 |只看该作者
哇靠,自己渲染按钮。。。

使用道具 举报

Rank: 8Rank: 8

4#
那个谁 发表于 2011-7-7 08:46:29 |只看该作者
表示看不懂。

使用道具 举报

Rank: 7Rank: 7Rank: 7

5#
俊俊 实名认证  发表于 2011-7-8 13:30:00 来自手机 |只看该作者
混混@普宁.中国 发表于 2011-7-7 00:06
哇靠,自己渲染按钮。。。

三年前我就干这样的活,感兴趣的话,我还有其他控件

使用道具 举报

管理员

超级大菜鸟

Rank: 9Rank: 9Rank: 9

6#
混混@普宁.中国 实名认证  发表于 2011-7-8 14:00:50 |只看该作者
建议打包,发送到 alvin@iscripts.org

使用道具 举报

Rank: 6Rank: 6

7#
Yisin 发表于 2011-7-10 12:33:31 来自手机 |只看该作者
强悍 全部发出来
路不好走,你却依旧满眼的爱,找不到理由...

使用道具 举报

Rank: 7Rank: 7Rank: 7

8#
奔波儿灞 发表于 2012-3-31 17:25:51 |只看该作者
这  这...这怎么用啊 这个..

使用道具 举报

您需要登录后才可以回帖 登录 | 加入学盟

手机版|Scripts 学盟   |

GMT+8, 2024-5-6 18:55 , Processed in 1.076610 second(s), 12 queries .

Powered by Discuz! X2

© 2001-2011 Comsenz Inc.

回顶部