Winform 自定义控件

  从事.net工作,拉拉杂杂算在一起,将近5年了,在各种技术群中,总是被问及“想要自定义控件,应该怎么做?”,“想要重绘控件,有什么入门教程没有”一类的问题,虽然很无奈,但是目前我确实没有看到过,最近在做一个ActiveX的同时,也在做一套控件美化,趁现在还没有忘了,把重绘控件的一些新的和方法与大家分享一下。

  首先给大家展示一下系统的类图:

  

  任何一个Windows窗体(这里所说的窗体包括控件,在Windows中,会把Button也识别为一个窗体,和Form略有区别的就是ClassStyle和ClassStyleEx略有区别)想要如何显示,是需要通过绘制来显示出来的,先来做一个基础类,这样各种控件在这个基础上生成。

  今天先做ControlBase类,该类功能如下:文本内容的绘制,字符串的对齐,自定义绘制方法。

  

/*************************************************************************************
   * CLR版本:        2.0
   * 类 名 称:       Button
   * 机器名称:       Bluefire-PC
   * 命名空间:       Com.Bluefire.ControlCollection.Common.Util
   * 文 件 名:       ControlBase
   * 创建时间:       2012年4月5日
   * 作    者:       Bluefire
   * 说   明: 控件基础
   * 修改时间:
   * 修 改 人:
  *************************************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using Com.Bluefire.ControlCollection.Common.Properties;
using System.Drawing.Drawing2D;
using Com.Bluefire.ControlCollection.Common.Win32Helper;

namespace Com.Bluefire.ControlCollection.Common.Util
{
    public partial class ControlBase : Control
    {
        #region Event Load
        private event EventHandler m_Load;
        [Browsable(true),
        EditorBrowsable(EditorBrowsableState.Always),
        Category("BluefireEvent"),
        Description("控件显示之前")]
        /// <summary>
        /// 控件文本对齐方式改变
        /// </summary>
        public event EventHandler Load
        {
            add
            {
                m_Load += value;
            }
            remove
            {
                m_Load -= value;
            }
        }

        protected virtual void OnLoad(EventArgs e)
        {
            EventHandler temp = m_Load;
            if (temp != null)
            {
                temp(this, e);
            }
        }

        #endregion

        #region Event TextAlignChanged
        protected virtual Boolean m_PaintText { get; set; }

        private event EventHandler m_TextAlignChanged;
        [Browsable(true),
        EditorBrowsable(EditorBrowsableState.Always),
        Category("BluefireEvent"),
        Description("控件文本对齐方式改变")]
        /// <summary>
        /// 控件文本对齐方式改变
        /// </summary>
        public event EventHandler TextAlignChanged
        {
            add
            {
                m_TextAlignChanged += value;
            }
            remove
            {
                m_TextAlignChanged -= value;
            }
        }

        protected virtual void OnTextAlignChanged(EventArgs e)
        {
            EventHandler temp = m_TextAlignChanged;
            if (temp != null)
            {
                temp(this, e);
            }
        }

        #endregion

        #region TextAlign
        private ContentAlignment m_TextAlign = ContentAlignment.MiddleCenter;

        [
       DefaultValue(typeof(ContentAlignment), "MiddleCenter"),
       Browsable(true),
       EditorBrowsable(EditorBrowsableState.Always),
       Category("BluefireStyle"),
       Description("控件文本对齐方式")
       ]
        /// <summary>
        /// 控件文本对齐方式
        /// </summary>
        public virtual ContentAlignment TextAlign
        {
            get
            {
                return m_TextAlign;
            }
            set
            {
                if (value != m_TextAlign)
                {
                    m_TextAlign = value;
                    Refresh();
                    OnTextAlignChanged(EventArgs.Empty);
                }
            }
        }
        #endregion

        #region Text
        private string m_Text;


        [
       DefaultValue(null),
       Browsable(true),
       EditorBrowsable(EditorBrowsableState.Always),
       Category("BluefireStyle"),
       Description("文本内容")
       ]
        /// <summary>
        /// 控件文本
        /// </summary>
        public override string Text
        {
            get
            {
                return m_Text;
            }
            set
            {
                if (value != m_Text)
                {
                    m_Text = value;
                    Refresh();
                    base.OnTextChanged(EventArgs.Empty);
                }
            }
        }
        #endregion

        #region ColorGradientDirection
        private LinearGradientMode m_ColorGradientDirection = LinearGradientMode.Vertical;
        [
        DefaultValue(typeof(LinearGradientMode), "Vertical"),
        Browsable(true),
        EditorBrowsable(EditorBrowsableState.Always),
        Category("BluefireStyle"),
        Description("颜色渐变方向")
        ]
        /// <summary>
        /// 颜色渐变方向
        /// </summary>
        public virtual LinearGradientMode ColorGradientDirection
        {
            get
            {
                return m_ColorGradientDirection;
            }
            set
            {
                m_ColorGradientDirection = value;
                this.Refresh();
            }
        }
        #endregion

        #region LightColor
        private Color m_LightColor = StyleManager.BaseLightColor;

        [
           DefaultValue(typeof(Color), "110, 255, 255, 255"),
           Browsable(true),
           EditorBrowsable(EditorBrowsableState.Always),
           Category("BluefireStyle"),
           Description("浅色区颜色"),
           RefreshProperties(System.ComponentModel.RefreshProperties.Repaint)
        ]
        /// <summary>
        ///浅色区颜色
        /// </summary>
        public virtual Color LightColor
        {
            get
            {
                return m_LightColor;
            }
            set
            {
                if (SystemManagedColor)
                {
                    throw new ArgumentException("operation is prohibited");
                }
                else
                {
                    m_LightColor = value;
                    Refresh();
                }
            }
        }
        #endregion

        #region DarkColor
        private Color m_DarkColor = StyleManager.BaseColor;

        [
           DefaultValue(typeof(Color), "200, 0, 191, 255"),
           Browsable(true),
           EditorBrowsable(EditorBrowsableState.Always),
           Category("BluefireStyle"),
           Description("浅色区颜色")
        ]
        /// <summary>
        ///浅色区颜色
        /// </summary>
        public virtual Color DarkColor
        {
            get
            {
                return m_DarkColor;
            }
            set
            {
                if (SystemManagedColor)
                {
                    throw new ArgumentException("operation is prohibited");
                }
                else
                {
                    m_DarkColor = value;
                    Refresh();
                }
            }
        }
        #endregion

        #region DarkColor
        private IntPtr m_ParentHandle = IntPtr.Zero;

        [
        Browsable(false),
        EditorBrowsable(EditorBrowsableState.Never),
        Category("BluefireStyle"),
        Description("父容器句柄")
        ]
        /// <summary>
        ///父容器句柄
        /// </summary>
        public virtual IntPtr ParentHandle
        {
            get
            {
                return m_ParentHandle;
            }
        }
        #endregion

        #region ControlDirection
        private ControlDirection m_ControlDirection = ControlDirection.Horizontal;

        [DefaultValue(typeof(ControlDirection), "Horizontal"),
        Browsable(true),
        EditorBrowsable(EditorBrowsableState.Always),
        Category("BluefireStyle"),
        Description("最大值"),
        RefreshProperties(System.ComponentModel.RefreshProperties.Repaint)]
        /// <summary>
        ///最大值
        /// </summary>
        public virtual ControlDirection ControlDirection
        {
            get
            {
                return m_ControlDirection;
            }
            set
            {
                if (value != m_ControlDirection)
                {
                    m_ControlDirection = value;
                    Refresh();
                }
            }
        }
        #endregion

        #region DisLightColor

        private Color m_DisLightColor = StyleManager.DisableBaseLightColor;

        [DefaultValue(typeof(Color), "110, 255, 255, 255"),
        Browsable(true),
        EditorBrowsable(EditorBrowsableState.Always),
        Category("BluefireStyle"),
        Description("禁用时浅色区颜色"),
        RefreshProperties(System.ComponentModel.RefreshProperties.Repaint)]
        /// <summary>
        ///禁用时浅色区颜色
        /// </summary>
        public virtual Color DisLightColor
        {
            get
            {
                return m_DisLightColor;
            }
            set
            {
                if (SystemManagedColor)
                {
                    throw new ArgumentException("operation is prohibited");
                }
                else
                {
                    m_DisLightColor = value;
                    Refresh();
                }
            }
        }
        #endregion

        #region DisDarkColor

        private Color m_DisDarkColor = StyleManager.DisableBaseColor;

        [DefaultValue(typeof(Color), "200, 180, 180, 180"),
        Browsable(true),
        EditorBrowsable(EditorBrowsableState.Always),
        Category("BluefireStyle"),
        Description("禁用时深色区颜色"),
        RefreshProperties(System.ComponentModel.RefreshProperties.Repaint)]
        /// <summary>
        ///禁用时深色区颜色
        /// </summary>
        public virtual Color DisDarkColor
        {
            get
            {
                return m_DisDarkColor;
            }
            set
            {
                if (SystemManagedColor)
                {
                    throw new ArgumentException("operation is prohibited");
                }
                else
                {
                    m_DisDarkColor = value;
                    Refresh();
                }
            }
        }
        #endregion

        #region SystemManagedColor

        private Boolean m_SystemManagerColor = true;

        [DefaultValue(true),
        Browsable(true),
        EditorBrowsable(EditorBrowsableState.Always),
        Category("BluefireStyle"),
        Description("是否让系统管理颜色")]
        /// <summary>
        /// 是否让系统管理颜色
        /// </summary>
        public virtual Boolean SystemManagedColor
        {
            get
            {
                return m_SystemManagerColor;
            }
            set
            {
                if (value != m_SystemManagerColor)
                {
                    m_SystemManagerColor = value;
                    if (value)
                    {
                        ReSetColor();
                    }
                    this.Refresh();
                }
            }
        }
        #endregion

        #region Enable
        [Browsable(false),
        EditorBrowsable(EditorBrowsableState.Never)]
        /// <summary>
        ///是否禁用
        /// </summary>
        public new bool Enabled
        {
            get
            {
                return Enable;
            }
            set
            {
                Enable = value;
            }
        }


        private Boolean m_Enabled = true;

        [
           DefaultValue(true),
           Browsable(true),
           EditorBrowsable(EditorBrowsableState.Always),
           Category("BluefireStyle"),
           Description("是否禁用")
        ]
        /// <summary>
        ///是否禁用
        /// </summary>
        public virtual Boolean Enable
        {
            get
            {
                return m_Enabled;
            }
            set
            {
                if (value != m_Enabled)
                {
                    m_Enabled = value;
                    Refresh();
                    OnEnabledChanged(EventArgs.Empty);
                    Enabled = value;
                }
            }
        }
        #endregion

        #region Region
        /// <summary>
        /// 边框类型
        /// </summary>
        [DefaultValue(typeof(RegionSizeType), "NONE"),
        Browsable(false),
        EditorBrowsable(EditorBrowsableState.Never),
        Category("BluefireStyle"),
        Description("边框形状")]
        public virtual RegionSizeType SizeType
        {
            get
            {
                return this.manager.SizeType;
            }
            set
            {
                this.manager.SizeType = value;
            }
        }

        /// <summary>
        /// ↖大小
        /// </summary>
        [DefaultValue(0),
        Browsable(false),
        EditorBrowsable(EditorBrowsableState.Never),
        Category("BluefireStyle"),
        Description("↖大小")]
        public virtual int LeftTopRadius
        {
            get
            {
                return this.manager.LeftTopRadius;
            }
            set
            {
                this.manager.LeftTopRadius = value;
            }
        }
        /// <summary>
        /// ↙大小
        /// </summary>
        [DefaultValue(0),
        Browsable(false),
        EditorBrowsable(EditorBrowsableState.Never),
        Category("BluefireStyle"),
        Description("↙大小")]
        public virtual int LeftButtomRadius
        {
            get
            {
                return this.manager.LeftButtomRadius;
            }
            set
            {
                this.manager.LeftButtomRadius = value;
            }
        }
        /// <summary>
        /// ↗大小
        /// </summary>
        [DefaultValue(0),
        Browsable(false),
        EditorBrowsable(EditorBrowsableState.Never),
        Category("BluefireStyle"),
        Description("↗大小")]
        public virtual int RightTopRadius
        {
            get
            {
                return this.manager.RightTopRadius;
            }
            set
            {
                this.manager.RightTopRadius = value;
            }
        }
        /// <summary>
        /// ↘大小
        /// </summary>
        [DefaultValue(0),
        Browsable(false),
        EditorBrowsable(EditorBrowsableState.Never),
        Category("BluefireStyle"),
        Description("↘大小")]
        public virtual int RightButtomRadius
        {
            get
            {
                return this.manager.RightButtomRadius;
            }
            set
            {
                this.manager.RightButtomRadius = value;
            }
        }
        #endregion

        public ControlBase()
        {
            InitializeComponent();

            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw |
                ControlStyles.UserPaint, true);
            this.DoubleBuffered = true;
            this.ResizeRedraw = true;
            this.SetStyle(ControlStyles.StandardClick | ControlStyles.StandardDoubleClick | ControlStyles.Selectable, false);
            m_PaintText = true;
        }

        #region 自定义方法
        protected virtual Boolean ReSetColor()
        {
            if (DesignMode)
            {
                if (MessageBox.Show(Resources.sys_ResetColor, Resources.sys_Caption, MessageBoxButtons.YesNo) == DialogResult.No)
                {
                    return false;
                }
            }
            m_LightColor = StyleManager.BaseLightColor;
            m_DarkColor = StyleManager.BaseColor;
            m_DisLightColor = StyleManager.DisableBaseLightColor;
            m_DisDarkColor = StyleManager.DisableBaseColor;
            return true;
        }

        protected virtual void PaintRectangle(Graphics graphic, Color basecolor, Color baselightcolor)
        {
            graphic.SmoothingMode = SmoothingMode.HighQuality;
            graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
            switch (m_ColorGradientDirection)
            {
                case LinearGradientMode.BackwardDiagonal:
                    {
                        LinearGradientBrush brushtop = new LinearGradientBrush(this.ClientRectangle,
                            baselightcolor, basecolor, LinearGradientMode.BackwardDiagonal);
                        graphic.FillRectangle(brushtop, this.ClientRectangle);
                    }
                    break;
                case LinearGradientMode.ForwardDiagonal:
                    {
                        LinearGradientBrush brushtop = new LinearGradientBrush(this.ClientRectangle,
                            baselightcolor, basecolor, LinearGradientMode.ForwardDiagonal);
                        graphic.FillRectangle(brushtop, this.ClientRectangle);
                    }
                    break;
                case LinearGradientMode.Horizontal:
                    {
                        int leftwidth = this.Width / 2;
                        int rightwidth = this.Width - leftwidth;

                        Rectangle rectleft = new Rectangle(0, 0, leftwidth,
                            this.Height);
                        Rectangle rectright = new Rectangle(leftwidth, 0,
                            rightwidth, Height);
                        LinearGradientBrush brushleft = new LinearGradientBrush(rectleft,
                           baselightcolor, basecolor, LinearGradientMode.Horizontal);
                        LinearGradientBrush brushrigth = new LinearGradientBrush(rectright,
                            basecolor, baselightcolor, LinearGradientMode.Horizontal);
                        graphic.FillRectangle(brushrigth, rectright);
                        graphic.FillRectangle(brushleft, rectleft);
                    }
                    break;
                case LinearGradientMode.Vertical:
                    {
                        int topheight = this.Height / 2;
                        int buttomheight = this.Height - topheight;

                        Rectangle recttop = new Rectangle(0, 0, this.Width,
                            topheight);
                        Rectangle rectbuttom = new Rectangle(0, topheight,
                            this.Width, buttomheight);
                        LinearGradientBrush brushtop = new LinearGradientBrush(recttop, baselightcolor, basecolor, LinearGradientMode.Vertical);
                        LinearGradientBrush brushbuttom = new LinearGradientBrush(rectbuttom, basecolor, baselightcolor, LinearGradientMode.Vertical);
                        graphic.FillRectangle(brushbuttom, rectbuttom);
                        graphic.FillRectangle(brushtop, recttop);
                    }
                    break;
            }
        }

        protected virtual void PaintRectangle(Graphics graphic, Color basecolor, Color baselightcolor, Rectangle rect)
        {
            if (rect.Width == 0 || rect.Height == 0)
            {
                return;
            }
            graphic.SmoothingMode = SmoothingMode.HighQuality;
            graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
            switch (m_ColorGradientDirection)
            {
                case LinearGradientMode.BackwardDiagonal:
                    {
                        LinearGradientBrush brushtop = new LinearGradientBrush(rect,
                            baselightcolor, basecolor, LinearGradientMode.BackwardDiagonal);
                        graphic.FillRectangle(brushtop, rect);
                    }
                    break;
                case LinearGradientMode.ForwardDiagonal:
                    {
                        LinearGradientBrush brushtop = new LinearGradientBrush(rect,
                            baselightcolor, basecolor, LinearGradientMode.ForwardDiagonal);
                        graphic.FillRectangle(brushtop, rect);
                    }
                    break;
                case LinearGradientMode.Horizontal:
                    {
                        float leftwidth = rect.Width / 2;
                        float rightwidth = rect.Width - leftwidth;

                        RectangleF rectleft = new RectangleF(rect.X, rect.Y, leftwidth,
                            rect.Height);
                        RectangleF rectright = new RectangleF(leftwidth, rect.Y,
                            rightwidth, rect.Height);
                        LinearGradientBrush brushleft = new LinearGradientBrush(rectleft,
                           baselightcolor, basecolor, LinearGradientMode.Horizontal);
                        LinearGradientBrush brushrigth = new LinearGradientBrush(rectright,
                            basecolor, baselightcolor, LinearGradientMode.Horizontal);
                        graphic.FillRectangle(brushrigth, rectright);
                        graphic.FillRectangle(brushleft, rectleft);
                    }
                    break;
                case LinearGradientMode.Vertical:
                    {
                        float topheight = rect.Height / 2;
                        float buttomheight = rect.Height - topheight;

                        RectangleF recttop = new RectangleF(rect.X, rect.Y, rect.Width,
                            topheight);
                        RectangleF rectbuttom = new RectangleF(rect.X, rect.Y + topheight,
                            rect.Width, buttomheight);
                        LinearGradientBrush brushtop = new LinearGradientBrush(recttop, baselightcolor, basecolor, LinearGradientMode.Vertical);
                        LinearGradientBrush brushbuttom = new LinearGradientBrush(rectbuttom, basecolor, baselightcolor, LinearGradientMode.Vertical);
                        graphic.FillRectangle(brushbuttom, rectbuttom);
                        graphic.FillRectangle(brushtop, recttop);
                    }
                    break;
            }
        }

        protected virtual void PaintRectangle(Graphics graphic, Color basecolor, Color baselightcolor, Rectangle rect, LinearGradientMode mode)
        {
            if (rect.Width == 0 || rect.Height == 0)
            {
                return;
            }
            graphic.SmoothingMode = SmoothingMode.HighQuality;
            graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
            switch (mode)
            {
                case LinearGradientMode.BackwardDiagonal:
                    {
                        LinearGradientBrush brushtop = new LinearGradientBrush(rect,
                            baselightcolor, basecolor, LinearGradientMode.BackwardDiagonal);
                        graphic.FillRectangle(brushtop, rect);
                    }
                    break;
                case LinearGradientMode.ForwardDiagonal:
                    {
                        LinearGradientBrush brushtop = new LinearGradientBrush(rect,
                            baselightcolor, basecolor, LinearGradientMode.ForwardDiagonal);
                        graphic.FillRectangle(brushtop, rect);
                    }
                    break;
                case LinearGradientMode.Horizontal:
                    {
                        float leftwidth = rect.Width / 2;
                        float rightwidth = rect.Width - leftwidth;

                        RectangleF rectleft = new RectangleF(rect.X, rect.Y, leftwidth,
                            rect.Height);
                        RectangleF rectright = new RectangleF(leftwidth, rect.Y,
                            rightwidth, rect.Height);
                        LinearGradientBrush brushleft = new LinearGradientBrush(rectleft,
                           baselightcolor, basecolor, LinearGradientMode.Horizontal);
                        LinearGradientBrush brushrigth = new LinearGradientBrush(rectright,
                            basecolor, baselightcolor, LinearGradientMode.Horizontal);
                        graphic.FillRectangle(brushrigth, rectright);
                        graphic.FillRectangle(brushleft, rectleft);
                    }
                    break;
                case LinearGradientMode.Vertical:
                    {
                        float topheight = rect.Height / 2;
                        float buttomheight = rect.Height - topheight;

                        RectangleF recttop = new RectangleF(rect.X, rect.Y, rect.Width,
                            topheight);
                        RectangleF rectbuttom = new RectangleF(rect.X, rect.Y + topheight,
                            rect.Width, buttomheight);
                        LinearGradientBrush brushtop = new LinearGradientBrush(recttop, baselightcolor, basecolor, LinearGradientMode.Vertical);
                        LinearGradientBrush brushbuttom = new LinearGradientBrush(rectbuttom, basecolor, baselightcolor, LinearGradientMode.Vertical);
                        graphic.FillRectangle(brushbuttom, rectbuttom);
                        graphic.FillRectangle(brushtop, recttop);
                    }
                    break;
            }
        }
        protected virtual void PaintRectangle(Graphics graphic, Color basecolor, Color baselightcolor, RectangleF rect, LinearGradientMode mode)
        {
            if (rect.Width == 0 || rect.Height == 0)
            {
                return;
            }
            graphic.SmoothingMode = SmoothingMode.HighQuality;
            graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
            switch (mode)
            {
                case LinearGradientMode.BackwardDiagonal:
                    {
                        LinearGradientBrush brushtop = new LinearGradientBrush(rect,
                            baselightcolor, basecolor, LinearGradientMode.BackwardDiagonal);
                        graphic.FillRectangle(brushtop, rect);
                    }
                    break;
                case LinearGradientMode.ForwardDiagonal:
                    {
                        LinearGradientBrush brushtop = new LinearGradientBrush(rect,
                            baselightcolor, basecolor, LinearGradientMode.ForwardDiagonal);
                        graphic.FillRectangle(brushtop, rect);
                    }
                    break;
                case LinearGradientMode.Horizontal:
                    {
                        float leftwidth = rect.Width / 2;
                        float rightwidth = rect.Width - leftwidth;

                        RectangleF rectleft = new RectangleF(rect.X, rect.Y, leftwidth,
                            rect.Height);
                        RectangleF rectright = new RectangleF(leftwidth, rect.Y,
                            rightwidth, rect.Height);
                        LinearGradientBrush brushleft = new LinearGradientBrush(rectleft,
                           baselightcolor, basecolor, LinearGradientMode.Horizontal);
                        LinearGradientBrush brushrigth = new LinearGradientBrush(rectright,
                            basecolor, baselightcolor, LinearGradientMode.Horizontal);
                        graphic.FillRectangle(brushrigth, rectright);
                        graphic.FillRectangle(brushleft, rectleft);
                    }
                    break;
                case LinearGradientMode.Vertical:
                    {
                        float topheight = rect.Height / 2;
                        float buttomheight = rect.Height - topheight;

                        RectangleF recttop = new RectangleF(rect.X, rect.Y, rect.Width,
                            topheight);
                        RectangleF rectbuttom = new RectangleF(rect.X, rect.Y + topheight,
                            rect.Width, buttomheight);
                        LinearGradientBrush brushtop = new LinearGradientBrush(recttop, baselightcolor, basecolor, LinearGradientMode.Vertical);
                        LinearGradientBrush brushbuttom = new LinearGradientBrush(rectbuttom, basecolor, baselightcolor, LinearGradientMode.Vertical);
                        graphic.FillRectangle(brushbuttom, rectbuttom);
                        graphic.FillRectangle(brushtop, recttop);
                    }
                    break;
            }
        }

        protected virtual void PaintText(Graphics graphic)
        {
            #region Text
            if (!m_PaintText)
            {
                return;
            }
            SizeF size = graphic.MeasureString(this.Text, this.Font);

            PointF location = new PointF(this.Width / 2.0F - size.Width / 2.0F, this.Height / 2.0F - size.Height / 2.0F);

            switch (m_TextAlign)
            {
                case ContentAlignment.BottomCenter:
                    location = new PointF(this.Width / 2.0F - size.Width / 2.0F, this.Height - size.Height - 3);
                    break;
                case ContentAlignment.BottomLeft:
                    location = new PointF(3, this.Height - size.Height - 3);
                    break;
                case ContentAlignment.BottomRight:
                    location = new PointF(this.Width - size.Width, this.Height - size.Height - 3);
                    break;
                case ContentAlignment.MiddleCenter:
                    location = new PointF(this.Width / 2.0F - size.Width / 2.0F, this.Height / 2.0F - size.Height / 2.0F);
                    break;
                case ContentAlignment.MiddleLeft:
                    location = new PointF(3, this.Height / 2.0F - size.Height / 2.0F);
                    break;
                case ContentAlignment.MiddleRight:
                    location = new PointF(this.Width - size.Width - 3, this.Height / 2.0F - size.Height / 2.0F);
                    break;
                case ContentAlignment.TopCenter:
                    location = new PointF(this.Width / 2.0F - size.Width / 2.0F, 3);
                    break;
                case ContentAlignment.TopLeft:
                    location = new PointF(3, 3);
                    break;
                case ContentAlignment.TopRight:
                    location = new PointF(this.Width - size.Width - 3, 3);
                    break;
                default:
                    break;
            }
            graphic.DrawString(this.Text, Font, new SolidBrush(this.ForeColor), location);
            #endregion
        }

        protected virtual void PaintText(Graphics graphic, string text)
        {
            #region Text
            if (!m_PaintText)
            {
                return;
            }
            SizeF size = graphic.MeasureString(text, this.Font);

            PointF location = new PointF(this.Width / 2.0F - size.Width / 2.0F, this.Height / 2.0F - size.Height / 2.0F);

            switch (m_TextAlign)
            {
                case ContentAlignment.BottomCenter:
                    location = new PointF(this.Width / 2.0F - size.Width / 2.0F, this.Height - size.Height - 3);
                    break;
                case ContentAlignment.BottomLeft:
                    location = new PointF(3, this.Height - size.Height - 3);
                    break;
                case ContentAlignment.BottomRight:
                    location = new PointF(this.Width - size.Width, this.Height - size.Height - 3);
                    break;
                case ContentAlignment.MiddleCenter:
                    location = new PointF(this.Width / 2.0F - size.Width / 2.0F, this.Height / 2.0F - size.Height / 2.0F);
                    break;
                case ContentAlignment.MiddleLeft:
                    location = new PointF(3, this.Height / 2.0F - size.Height / 2.0F);
                    break;
                case ContentAlignment.MiddleRight:
                    location = new PointF(this.Width - size.Width - 3, this.Height / 2.0F - size.Height / 2.0F);
                    break;
                case ContentAlignment.TopCenter:
                    location = new PointF(this.Width / 2.0F - size.Width / 2.0F, 3);
                    break;
                case ContentAlignment.TopLeft:
                    location = new PointF(3, 3);
                    break;
                case ContentAlignment.TopRight:
                    location = new PointF(this.Width - size.Width - 3, 3);
                    break;
                default:
                    break;
            }
            graphic.DrawString(text, Font, new SolidBrush(this.ForeColor), location);
            #endregion
        }

        protected virtual void PaintBackGround(Graphics g)
        {
            PaintRectangle(g, Color.FromArgb(110, Color.LightGray), Color.FromArgb(200, Color.White));
            if (m_Enabled)
            {
                PaintRectangle(g, DarkColor, LightColor);
            }
            else
            {
                PaintRectangle(g, DisDarkColor, DisLightColor);
            }
        }

        protected virtual void SendMessage(uint msg, IntPtr lparam, IntPtr wparam)
        {
            UnsafeMethod.SendMessage(this.Handle, msg, wparam, lparam);
        }

        protected virtual void SendMessage(WindowsMessage msg, IntPtr lparam, IntPtr wparam)
        {
            UnsafeMethod.SendMessage(this.Handle, (uint)msg, wparam, lparam);
        }

        protected virtual void SetCapture()
        {
            UnsafeMethod.SetCapture(this.Handle);
        }

        protected virtual void ReleaseCapture()
        {
            UnsafeMethod.ReleaseCapture();
        }

        protected virtual IntPtr WndProc(IntPtr hWnd, WindowsMessage msg, IntPtr lParam, IntPtr wParam)
        {
            if (msg == WindowsMessage.SHOWWINDOW)
            {
                OnLoad(EventArgs.Empty);
            }
            if (msg == WindowsMessage.LBUTTONDBLCLK)
            {
                int x = (int)Win32Helper.UnsafeMethod.LOWORD(lParam);
                int y = (int)Win32Helper.UnsafeMethod.HIWORD(lParam);
                OnMouseDoubleClick(new MouseEventArgs(System.Windows.Forms.MouseButtons.Left, 2, x, y, 0));
            }
            return IntPtr.Zero;
        }

        #endregion

        protected override void OnParentChanged(EventArgs e)
        {
            base.OnParentChanged(e);
            if (this.Parent == null)
            {
                m_ParentHandle = IntPtr.Zero;
            }
            else
            {
                m_ParentHandle = this.Parent.Handle;
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            PaintBackGround(e.Graphics);

            PaintText(e.Graphics);
        }

        protected override void WndProc(ref Message m)
        {
            IntPtr resualt = WndProc(m.HWnd, (WindowsMessage)m.Msg, m.LParam, m.WParam);
            if (resualt == new IntPtr(2))
            {
                m.Result = new IntPtr(1);
                return;
            }
            base.WndProc(ref m);
        }

    }

    public enum ControlDirection : uint
    {
        None = 0x00,
        /// <summary>
        /// 竖向
        /// </summary>
        Vertical = 0x01,
        /// <summary>
        /// 横向
        /// </summary>
        Horizontal = 0x02,
    }
}

  

  

 

posted @ 2012-04-07 15:27  Bluefire  阅读(1976)  评论(4编辑  收藏  举报