• C#仿QQ皮肤-Button 控件实现


      原文地址:http://www.sufeinet.com/thread-2076-1-1.html

    导读部分
    -------------------------------------------------------------------------------------------------------------
    C#仿QQ皮肤-实现原理系列文章导航 最新版源码下载

    http://www.sufeinet.com/thread-2-1-1.html                                             

           本节开始我们一起来讨论一些简单的系统控件的实现,在这里我列出一些基本的控件 如下图所示,在写文章的同时我会不断的更新控件,和添加新的控件,也都会一一的发上来和大家分享。

       

        Button控件 是一个很常见也是用的比较多的一个控件,其实他的实现正和他的使用一样的简单明了。

    在看它的实现 之前我们先来看看CommandButton用户控件的实现吧

        界面

               

      呵呵 主要看代码吧,这上面没有什么东东可看的

    我们先来指定一下默认的事件吧

    [DefaultEvent("Click")]

    其实这个简单只要这么一行就OK了,但记着一定要写在类的上面哦

    代码

            
    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(15045);
                
    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(2550255);
            }

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

    在这里我就不多说了,因为代码都很简单,所以很容易看明白,大家看看代码吧。

    我们主要来看看Button的控件吧

    不用说要先继承一下刚说的用户控件哦

     public class Button : CommandButton

    接下来来看一下 InitializeComponent()方法

    代码
      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(00);
                
    this.lblText.Name = "lblText";
                
    this.lblText.Size = new System.Drawing.Size(7830);
                
    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(7830);
                
    this.ResumeLayout(false);

            }

    我们让字体同比率的话可以加上这个


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

    当然也要处理一下OnSizeChanged(EventArgs e)事件

    代码
     //固定比率暂时不需要,如果有需要的话可以取消注释
            
    //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);
            
    //}

    其它代码全在这里

    代码
    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(00);
                
    this.lblText.Name = "lblText";
                
    this.lblText.Size = new System.Drawing.Size(7830);
                
    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(7830);
                
    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"), truefalse);
                
    this.MouseDownImage = Bitmap.FromStream(Shared.AssemblyWinUI.GetManifestResourceStream("CRD.WinUI.Resources.Common.button.btndown.bmp"), truefalse);
                
    this.MouseMoveImage = Bitmap.FromStream(Shared.AssemblyWinUI.GetManifestResourceStream("CRD.WinUI.Resources.Common.button.btnfore.bmp"), truefalse);
                
            }

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

            
    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);
            
    //}
        }

    }
  • 相关阅读:
    maven工程下添加oracle驱动包
    面试题----入参两个Integer,无返回值,然后使这个两个值在调用函数后交换
    使用spring data solr 实现搜索关键字高亮显示
    MyBatisPlus忽略映射字段注解
    Linux下Solr单机版、集群版安装与配置
    AngularJs实现全选功能
    SpringBoot2.0整合mybatis、shiro、redis实现基于数据库权限管理系统
    springboot2.0集成shiro出现ShiroDialect报错找不到AbstractTextChildModifierAttrPr
    linux下ssh远程登录服务器入门操作
    英文邮件寻求帮助的礼貌用语
  • 原文地址:https://www.cnblogs.com/sufei/p/1767270.html
Copyright © 2020-2023  润新知