http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 23966|回复: 13

[C#皮肤] C#皮肤-Button 控件实现

[复制链接]
发表于 2013-3-4 11:26:53 | 显示全部楼层 |阅读模式
                C#皮肤-Button 控件实现

导读部分
-------------------------------------------------------------------------------------------------------------
C#皮肤-实现原理系列文章导航
http://www.sufeinet.com/thread-2-1-1.html

本节开始我们一起来讨论一些简单的系统控件的实现,在这里我列出一些基本的控件 如下图所示,在写文章的同时我会不断的更新控件,和添加新的控件,也都会一一的发上来和大家分享。    QQbutton1.png
    Button控件 是一个很常见也是用的比较多的一个控件,其实他的实现正和他的使用一样的简单明了。
在看它的实现 之前我们先来看看CommandButton用户控件的实现吧
    界面 QQbutton2.png
ComboBox3.png        ComboBox4.png    
  呵呵 主要看代码吧,这上面没有什么东东可看的
我们先来指定一下默认的事件吧
[code=csharp][DefaultEvent("Click")][/code]
其实这个简单只要这么一行就OK了,但记着一定要写在类的上面哦
[code=csharp] private Image _mouseMoveImage = null;
        private Image _mouseDownImage = null;
        private Image _normalImage = null;
        private ToolTip toolTip;
        private System.ComponentModel.IContainer components;
        private string _toolTip;
        private Color imageTransparentColor;

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.toolTip = new System.Windows.Forms.ToolTip(this.components);
            this.SuspendLayout();
            //
            // CommandButton
            //
            this.Name = "CommandButton";
            this.Size = new System.Drawing.Size(150, 45);
            this.ResumeLayout(false);

        }

        public CommandButton()
        {
            //this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            InitializeComponent();
        }

        public Image MouseMoveImage
        {
            get
            {
               
                return _mouseMoveImage;
            }
            set
            {
                _mouseMoveImage = value;
            }
        }

        public Image MouseDownImage
        {
            get
            {
               
                return _mouseDownImage;
            }
            set
            {
                _mouseDownImage = value;
            }
        }

        public Image NormalImage
        {
            get
            {
              
                return _normalImage;
            }
            set
            {
                _normalImage = value;
                this.BackgroundImage = _normalImage;
            }
        }

        public Color ImageTransparentColor
        {
            get
            {
                return this.imageTransparentColor;
            }
            set
            {
                this.imageTransparentColor = value;

                Bitmap image = this.BackgroundImage as Bitmap;

                if (((image != null) && (value != Color.Empty)) && !ImageAnimator.CanAnimate(image))
                {
                    try
                    {
                        image.MakeTransparent(this.imageTransparentColor);
                    }
                    catch
                    { }
                }
            }
        }

        //重写一下创建控件的方法
        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            if (this.NormalImage != null)
            {
                this.BackgroundImage = NormalImage;
            }
        }

        //重写进入事件
        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);

            if (this.MouseMoveImage != null)
            {
                this.BackgroundImage = MouseMoveImage;
            }
            this.Invalidate();
        }

        //重写离开可见部分的事件
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);

            if (this.NormalImage != null)
            {
                this.BackgroundImage = NormalImage;
            }
            this.Invalidate();
        }

        //重写鼠标按下事件
        protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
        {
            base.OnMouseDown(e);

            if (this.MouseDownImage != null)
            {
                this.BackgroundImage = this.MouseDownImage;
            }
        }

        //重写鼠标离开事件
        protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
        {
            base.OnMouseUp(e);

            if (this.NormalImage != null)
            {
                this.BackgroundImage = NormalImage;
            }
        }

        //重写背景修改时的事件
        protected override void OnBackgroundImageChanged(EventArgs e)
        {
            base.OnBackgroundImageChanged(e);

            this.ImageTransparentColor = Color.FromArgb(255, 0, 255);
        }

        public string ToolTip
        {
            get { return _toolTip; }
            set
            {
                _toolTip = value;
                this.toolTip.SetToolTip(this, _toolTip);
            }
        }
    }[/code]

在这里我就不多说了,因为代码都很简单,所以很容易看明白,大家看看代码吧。
我们主要来看看Button的控件吧
不用说要先继承一下刚说的用户控件哦
[code=csharp]public class Button : CommandButton[/code]
接下来来看一下 InitializeComponent()方法
[code=csharp] private void InitializeComponent()
        {
            this.lblText = new Label();
            this.SuspendLayout();
            //
            // lblText
            //
            this.lblText.BackColor = System.Drawing.Color.Transparent;
            this.lblText.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lblText.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.lblText.Location = new System.Drawing.Point(0, 0);
            this.lblText.Name = "lblText";
            this.lblText.Size = new System.Drawing.Size(78, 30);
            this.lblText.TabIndex = 0;
            this.lblText.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.lblText.TextChanged += new System.EventHandler(this.lblText_TextChanged);
            this.lblText.MouseLeave += new System.EventHandler(this.lblText_MouseLeave);
            this.lblText.Click += new System.EventHandler(this.lblText_Click);
            this.lblText.MouseUp += new System.Windows.Forms.MouseEventHandler(this.lblText_MouseUp);
            this.lblText.MouseEnter += new System.EventHandler(this.lblText_MouseEnter);
            this.lblText.ForeColor = Shared.FontColor;
            //
            // Button
            //
            this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.Controls.Add(this.lblText);
            this.Name = "Button";
            this.Size = new System.Drawing.Size(78, 30);
            this.ResumeLayout(false);

        }[/code]
我们让字体同比率的话可以加上这个
[code=csharp]//字体大小
        //protected override void OnResize(EventArgs e)
        //{
        //    base.OnResize(e);
        //    this.Height = this.Width * 30 / 78;
        //}[/code]
当然也要处理一下OnSizeChanged(EventArgs e)事件
[code=csharp]//固定比率暂时不需要,如果有需要的话可以取消注释
        //protected override void OnSizeChanged(EventArgs e)
        //{
        //    base.OnSizeChanged(e);

        //    int Rgn = 0, height = 0, width = 0,rgn=0;

        //    height = Height > 40 ? Height - 1 : Height;
        //    width = Width > 110 ? Width - 1 : Width;
        //    rgn = Height > 40 ? 8 : 7;

        //    Rgn = Win32.CreateRoundRectRgn(1, 1, width, height, rgn, rgn);

        //    Win32.SetWindowRgn(this.Handle, Rgn, true);
        //}[/code]
其它代码全在这里
[code=csharp]代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using CRD.Common;

namespace CRD.WinUI.Misc
{
    public class Button : CommandButton
    {
        public Label lblText;
        private DialogResult _dialogResult = DialogResult.None;


      
        public virtual DialogResult DialogResult
        {
            get { return _dialogResult; }
            set { _dialogResult = value; }
        }

        public Button()
            : base()
        {
            //this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            InitializeComponent();
        }

        public void PerformClick()
        {
            this.OnClick(new EventArgs());
        }

        private void InitializeComponent()
        {
            this.lblText = new Label();
            this.SuspendLayout();
            //
            // lblText
            //
            this.lblText.BackColor = System.Drawing.Color.Transparent;
            this.lblText.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lblText.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.lblText.Location = new System.Drawing.Point(0, 0);
            this.lblText.Name = "lblText";
            this.lblText.Size = new System.Drawing.Size(78, 30);
            this.lblText.TabIndex = 0;
            this.lblText.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.lblText.TextChanged += new System.EventHandler(this.lblText_TextChanged);
            this.lblText.MouseLeave += new System.EventHandler(this.lblText_MouseLeave);
            this.lblText.Click += new System.EventHandler(this.lblText_Click);
            this.lblText.MouseUp += new System.Windows.Forms.MouseEventHandler(this.lblText_MouseUp);
            this.lblText.MouseEnter += new System.EventHandler(this.lblText_MouseEnter);
            this.lblText.ForeColor = Shared.FontColor;
            //
            // Button
            //
            this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.Controls.Add(this.lblText);
            this.Name = "Button";
            this.Size = new System.Drawing.Size(78, 30);
            this.ResumeLayout(false);

        }

        //字体大小
        //protected override void OnResize(EventArgs e)
        //{
        //    base.OnResize(e);
        //    this.Height = this.Width * 30 / 78;
        //}

        protected override void OnCreateControl()
        {
            base.OnCreateControl();

            this.NormalImage = Bitmap.FromStream(Shared.AssemblyWinUI.GetManifestResourceStream("CRD.WinUI.Resources.Common.button.btnnomal.bmp"), true, false);
            this.MouseDownImage = Bitmap.FromStream(Shared.AssemblyWinUI.GetManifestResourceStream("CRD.WinUI.Resources.Common.button.btndown.bmp"), true, false);
            this.MouseMoveImage = Bitmap.FromStream(Shared.AssemblyWinUI.GetManifestResourceStream("CRD.WinUI.Resources.Common.button.btnfore.bmp"), true, false);
            
        }

        public void ResetBackGroundImage()
        {
            this.NormalImage = Bitmap.FromStream(Shared.AssemblyWinUI.GetManifestResourceStream("CRD.WinUI.Resources.Common.button.btnnomal.bmp"), true, false);
            this.MouseDownImage = Bitmap.FromStream(Shared.AssemblyWinUI.GetManifestResourceStream("CRD.WinUI.Resources.Common.button.btndown.bmp"), true, false);
            this.MouseMoveImage = Bitmap.FromStream(Shared.AssemblyWinUI.GetManifestResourceStream("CRD.WinUI.Resources.Common.button.btnfore.bmp"), true, false);
        }

        private string _text = string.Empty;

        [Browsable(true)]
        public new string Caption
        {
            get { return _text; }
            set
            {
                _text = value;

                if (lblText.Text != value)
                {
                    lblText.Text = value;
                }
            }
        }

        private void lblText_TextChanged(object sender, EventArgs e)
        {
            this.Text = lblText.Text;
        }

        private void lblText_MouseEnter(object sender, EventArgs e)
        {
            this.OnMouseEnter(e);
        }

        private void lblText_MouseLeave(object sender, EventArgs e)
        {
            this.OnMouseLeave(e);
        }

        private void lblText_MouseUp(object sender, MouseEventArgs e)
        {
            this.OnMouseUp(e);
        }

        private void lblText_Click(object sender, EventArgs e)
        {
            this.PerformClick();
        }

        //固定比率暂时不需要,如果有需要的话可以取消注释
        //protected override void OnSizeChanged(EventArgs e)
        //{
        //    base.OnSizeChanged(e);

        //    int Rgn = 0, height = 0, width = 0,rgn=0;

        //    height = Height > 40 ? Height - 1 : Height;
        //    width = Width > 110 ? Width - 1 : Width;
        //    rgn = Height > 40 ? 8 : 7;

        //    Rgn = Win32.CreateRoundRectRgn(1, 1, width, height, rgn, rgn);

        //    Win32.SetWindowRgn(this.Handle, Rgn, true);
        //}
    }
}[/code]

本帖被以下淘专辑推荐:

  • · 皮肤|主题: 15, 订阅: 0


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2013-4-10 17:02:32 | 显示全部楼层
朋友不错,谢谢您的努力,顶了
发表于 2013-4-23 00:35:06 | 显示全部楼层
真是个好地方!~~~~
发表于 2013-5-2 19:00:41 | 显示全部楼层
强烈支持楼主ing……
发表于 2013-10-12 10:59:35 | 显示全部楼层
一个按钮就这么多代码  晕了呢,
发表于 2013-11-3 20:48:06 | 显示全部楼层
厉害   
发表于 2013-11-7 15:26:28 | 显示全部楼层
非常感谢你帮了我的大忙,真的太感谢你啦!
发表于 2014-7-13 15:30:42 | 显示全部楼层
真是难得给力的帖子啊,强烈支持楼主。
发表于 2014-8-29 15:20:00 | 显示全部楼层
强烈支持楼主ing……
发表于 2014-9-5 10:52:21 | 显示全部楼层
强烈支持楼主ing……
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-4-27 11:06

© 2014-2021

快速回复 返回顶部 返回列表