自定义的Line控件

 

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.ComponentModel.Design;
using System.Collections;
using Etonesoft.DataType.Extension;
using Etonesoft.Windows.Controls.Design;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Drawing.Design;
using System.Reflection;
using Etonesoft.Drawing.Design;

namespace Etonesoft.Windows.Controls.CustomControls
{
    /// <summary>
    /// Line控件
    /// </summary>
    [Description("在窗体等容器控件中分隔子控件,同时提供焦点管理、显示文本和图标等功能")]
    [ToolboxItemAttribute ( typeof ( ToolboxItemBase ) )]
    [Designer(typeof(LineDesigner))]   
    public partial class Line : Control 
    {
       
        const float PERCCOLORCHANGED=0.7f;
        const int IMAGEMARGIN=2;
       
       
        ControlStyles cstyle=ControlStyles.AllPaintingInWmPaint |
            ControlStyles.OptimizedDoubleBuffer |
            ControlStyles.ResizeRedraw |
            ControlStyles.UserPaint| ControlStyles.SupportsTransparentBackColor | ControlStyles.Selectable  ;

        #region 属性 CanSelect 定义

        /// <summary>
        /// 当属性CanSelect更改时发生
        /// </summary>
        public event EventHandler CanSelectChanged;

        /// <summary>
        /// 引发控件CanSelectChanged事件
        /// </summary>
        protected virtual void OnCanSelectChanged ( EventArgs e )
        {
            //如果事件委托链不为空且长度大于0,则触发事件
            if ( CanSelectChanged != null && CanSelectChanged.GetInvocationList ( ).Length > 0 )
            {
                CanSelectChanged ( this , e );
            }
        }
        /// <summary>
        /// 获取或设置控件是否可被选择,如果为true且当前显示文本,则在文本的周围会出现焦点虚线框
        /// </summary>
        [Browsable ( false )]       
        [DesignerSerializationVisibility(DesignerSerializationVisibility .Visible)]
        [Description ( "获取或设置控件是否可被选择,如果为true且当前显示文本,则在文本的周围会出现焦点虚线框" )]
        public new bool CanSelect
        {
            get
            {
                return base.CanSelect &&
                    (this.DisplayStyle== DisplayStyle.Text|| this.DisplayStyle== DisplayStyle.ImageAndText) ;
            }
        }

        #endregion
   
        #region 属性 ContextAlgin 定义

        /// <summary>
        /// 当属性TextAlgin更改时发生
        /// </summary>
        public event EventHandler TextAlginChanged;

        /// <summary>
        /// 引发控件TextAlginChanged事件
        /// </summary>
        protected virtual void OnTextAlginChanged ( EventArgs e )
        {
            //如果事件委托链不为空且长度大于0,则触发事件
            if ( TextAlginChanged != null && TextAlginChanged.GetInvocationList ( ).Length > 0 )
            {
                TextAlginChanged ( this , e );
            }
        }
        private StringAlignment m_TextAlgin = StringAlignment.Near;

        /// <summary>
        /// 获取或设置内容的对齐方式
        /// </summary>
        [Browsable ( true )]
        [Category ( "自定义" )]
        [DefaultValue ( typeof ( StringAlignment ) , "Near" )]
        [Description ( "获取或设置内容的对齐方式" )]
        public StringAlignment TextAlign
        {
            get
            {
                return m_TextAlgin;
            }
            set
            {
                if ( m_TextAlgin == value ) return;
                m_TextAlgin = value;
                //TODO:在下面可添加附加的操作
                this.Invalidate ( );
                OnTextAlginChanged ( new EventArgs ( ) );
            }
        }

        #endregion                

        #region 属性 DisplayStyle 定义


        /// <summary>
        /// 获取或设置控件上内容的显示方式
        /// </summary>
        [Browsable ( false )]
        [Category ( "自定义" )]
        [DefaultValue ( typeof ( DisplayStyle ) , "None" )]
        [Description ( "获取或设置控件上内容的显示方式" )]
        public DisplayStyle DisplayStyle
        {
            get
            {
                if ( String.IsNullOrEmpty ( this.Text ) )
                {
                    if ( this.Image == null )
                    {
                        return DisplayStyle.None;
                    }
                    else
                    {
                        return DisplayStyle.Image;
                    }
                }
                else
                {
                    if ( this.Image == null )
                    {
                        return DisplayStyle.Text;
                    }
                    else
                    {
                        return DisplayStyle.ImageAndText;
                    }
                }
            }
        }

        #endregion

        #region 属性 Image 定义

        /// <summary>
        /// 当属性Image更改时发生
        /// </summary>
        public event EventHandler ImageChanged;

        /// <summary>
        /// 引发控件ImageChanged事件
        /// </summary>
        protected virtual void OnImageChanged ( EventArgs e )
        {
            //如果事件委托链不为空且长度大于0,则触发事件
            if ( ImageChanged != null && ImageChanged.GetInvocationList ( ).Length > 0 )
            {
                ImageChanged ( this , e );
            }
        }
        private Image m_Image = null;

        /// <summary>
        /// 获取或设置控件显示的图像
        /// </summary>
        [Browsable ( true )]
        [Category ( "自定义" )]
        [DefaultValue ( typeof ( Image ) , "(无)" )]
        [Description ( "获取或设置控件显示的图像" )]
        public Image Image
        {
            get
            {
                return m_Image;
            }
            set
            {
                if ( m_Image == value ) return;
                m_Image = value;
                //TODO:在下面可添加附加的操作
                AdjustControlSize ( this.Orientation == Orientation.Horizontal ? this.Width : this.Height );
                this.Refresh ( );
                OnImageChanged ( new EventArgs ( ) );
            }
        }

        #endregion

        #region 属性 ImageKey 定义

        /// <summary>
        /// 当属性ImageKey更改时发生
        /// </summary>
        public event EventHandler ImageKeyChanged;

        /// <summary>
        /// 引发控件ImageKeyChanged事件
        /// </summary>
        protected virtual void OnImageKeyChanged ( EventArgs e )
        {
            //如果事件委托链不为空且长度大于0,则触发事件
            if ( ImageKeyChanged != null && ImageKeyChanged.GetInvocationList ( ).Length > 0 )
            {
                ImageKeyChanged ( this , e );
            }
        }
        private Color m_ImageKey = Color.Fuchsia ;

        /// <summary>
        /// 获取或设置控件显示图像的透明色
        /// </summary>
        [Browsable ( true )]
        [Category ( "自定义" )]
        [DefaultValue ( typeof ( Color ) , "Fuchsia" )]
        [Description ( "获取或设置控件显示图像的透明色" )]
        public Color ImageKey
        {
            get
            {
                return m_ImageKey;
            }
            set
            {
                if ( m_ImageKey == value ) return;
                m_ImageKey = value;
                //TODO:在下面可添加附加的操作
                this.Invalidate ( getImageArea ( ) );
                OnImageKeyChanged ( new EventArgs ( ) );
            }
        }      

        #endregion      

        #region 属性 LineStyle 定义

        /// <summary>
        /// 当属性LineStyle更改时发生
        /// </summary>
        public event EventHandler LineStyleChanged;

        /// <summary>
        /// 引发控件LineStyleChanged事件
        /// </summary>
        protected virtual void OnLineStyleChanged ( EventArgs e )
        {
            //如果事件委托链不为空且长度大于0,则触发事件
            if ( LineStyleChanged != null && LineStyleChanged.GetInvocationList ( ).Length > 0 )
            {
                LineStyleChanged ( this , e );
            }
        }
        private LineStyle m_LineStyle =  LineStyle.Flat ;

        /// <summary>
        /// 获取或设置控件的三维样式
        /// </summary>
        [Browsable ( true )]
        [Category ( "自定义" )]
        [DefaultValue ( typeof ( LineStyle ) , "Flat" )]
        [Description ( "获取或设置控件的三维样式" )]
        public LineStyle LineStyle
        {
            get
            {
                return m_LineStyle;
            }
            set
            {
                if ( m_LineStyle == value ) return;
                m_LineStyle = value;
                //TODO:在下面可添加附加的操作
                AdjustControlSize ( this.Orientation== Orientation.Horizontal?this.Width:this.Height  );
                this.Refresh ( );
                OnLineStyleChanged ( new EventArgs ( ) );
            }
        }

        #endregion

        #region 属性 Orientation 定义

        /// <summary>
        /// 当属性Orientation更改时发生
        /// </summary>
        public event EventHandler OrientationChanged;

        /// <summary>
        /// 引发控件OrientationChanged事件
        /// </summary>
        protected virtual void OnOrientationChanged ( EventArgs e )
        {
            //如果事件委托链不为空且长度大于0,则触发事件
            if ( OrientationChanged != null && OrientationChanged.GetInvocationList ( ).Length > 0 )
            {
                OrientationChanged ( this , e );
            }
        }
        private Orientation  m_Orientation =  Orientation.Horizontal ;

        /// <summary>
        /// 获取或设置控件的方向
        /// </summary>
        [Browsable ( true )]
        [Category ( "自定义" )]
        [DefaultValue ( typeof ( Orientation  ) , "Horizontal" )]
        [Description ( "获取或设置控件的方向" )]
        public Orientation  Orientation
        {
            get
            {
                return m_Orientation;
            }
            set
            {
                if ( m_Orientation == value ) return;
                Orientation  oldOrientation = m_Orientation ;
                m_Orientation = value;
                //根据方向变化重新计算控件的大小
                DockStyle lds=Dock;
                Dock = DockStyle.None;
                AdjustControlSize ( oldOrientation == Orientation.Horizontal ? this.Width : this.Height );
                //更新控件停靠样式
                Dock = lds;
                Anchor = Anchor;
                TypeDescriptor.Refresh ( this );
                OnOrientationChanged ( new EventArgs ( ) );
            }
        }

        #endregion
       
        #region 重写父类属性
        [Browsable(true)]
        public override RightToLeft RightToLeft
        {
            get
            {
                return base.RightToLeft;
            }
            set
            {
                base.RightToLeft = value;
            }
        }

        /// <summary>
        /// 获取或设置控件关联的文本
        /// </summary>
        [Browsable ( true )]       
        public override  String Text
        {
            get
            {
                //if ( base.Text == "" ) return this.GetType().Name ;
                return base.Text;
            }
            set
            {
                if ( base.Text == value ) return;
                base.Text = value;
                AdjustControlSize ( this.Orientation == Orientation.Horizontal ? this.Width : this.Height );
                this.Refresh ( );
            }
        }

        AnchorStyles anchorStyle= AnchorStyles.Top | AnchorStyles.Left;
        /// <summary>
        /// 获取或设置控件如何与父控件边缘锚定
        /// </summary>
        /// <remarks>
        /// 因其取值依赖于Orientation属性,所以该属性隐藏了Control控件的同名属性
        /// </remarks>
        [Browsable ( true )]
        [Category ( "隐藏" )]
        [DefaultValue ( typeof ( AnchorStyles ) , "Top,Left" )]
        [Description ( "获取或设置控件如何与父控件边缘锚定" )]
        public new  AnchorStyles Anchor
        {
            get
            {
                return anchorStyle;
            }
            set
            {
                anchorStyle = value;
                if(this.Orientation== Orientation.Horizontal )
                {
                    if ( value.Coutains ( AnchorStyles.Top | AnchorStyles.Bottom ) ) value = value.Remove ( AnchorStyles.Bottom );
                }
                else
                {
                    if ( value.Coutains ( AnchorStyles.Left | AnchorStyles.Right ) ) value = value.Remove ( AnchorStyles.Right );
                }
                base.Anchor = value;
            }
        }

        DockStyle dockStyle= DockStyle.None;
        /// <summary>
        /// 获取或设置控件如何与父控件停靠
        /// </summary>
        /// <remarks>
        /// 因其取值依赖于Orientation属性,所以该属性隐藏了Control控件的同名属性
        /// </remarks>
        [Browsable ( true )]
        [Category ( "重写" )]
        [DefaultValue ( typeof ( DockStyle ) , "None" )]
        [Description ( "获取或设置控件如何与父控件停靠" )]
        public new DockStyle Dock
        {
            get
            {
                return dockStyle;
            }
            set
            {
                dockStyle = value;
                if ( this.Orientation == Orientation.Horizontal )
                {
                    if ( value.Coutains ( DockStyle.Left ) || value.Coutains ( DockStyle.Right ) ) return;
                }
                else
                {
                    if ( value.Coutains ( DockStyle.Top ) || value.Coutains ( DockStyle.Bottom ) ) return;
                }
                base.Dock = value;
            }
        }

        [Browsable(false)]
        [EditorBrowsable(  EditorBrowsableState.Never)]
        public override bool AllowDrop
        {
            get
            {
                return base.AllowDrop ;
            }
            set
            {
                base.AllowDrop = value  ;
            }
        }
        [Browsable ( false )]
        [EditorBrowsable ( EditorBrowsableState.Never )]
        public override Point AutoScrollOffset
        {
            get
            {
                return base.AutoScrollOffset;
            }
            set
            {
                base.AutoScrollOffset = value;
            }
        }
        //[Browsable ( false )]
        //[EditorBrowsable ( EditorBrowsableState.Never )]
        //public override Color BackColor
        //{
        //    get
        //    {
        //        return base.BackColor;
        //    }
        //    set
        //    {
        //        base.BackColor = value;
        //    }
        //}
       
        [Browsable ( false )]
        [EditorBrowsable ( EditorBrowsableState.Never )]
        public new bool CausesValidation
        {
            get { return base.CausesValidation; }
            set { base.CausesValidation = value; }
        }
        [Browsable ( false )]
        [EditorBrowsable ( EditorBrowsableState.Never )]
        public new Size Size
        {
            get { return base.Size; }
            set { base.Size = value; }
        }

        [Browsable ( false )]
        [EditorBrowsable ( EditorBrowsableState.Never )]
        public new ImeMode ImeMode
        {
            get { return base.ImeMode; }
            set { base.ImeMode = value; }
        }
        [Browsable ( false )]
        [EditorBrowsable ( EditorBrowsableState.Never )]
        public new Padding Margin
        {
            get { return base.Margin; }
            set { base.Margin = value; }
        }
        [Browsable ( false )]
        [EditorBrowsable ( EditorBrowsableState.Never )]
        public new Padding Padding
        {
            get { return base.Padding; }
            set { base.Padding = value; }
        }
        #endregion

  
        /// <summary>
        /// 构造函数,构造Line新实例
        /// </summary>
        public Line()
        {
            InitializeComponent();
            this.SetStyle ( cstyle , true );
            this.Size = new Size ( 100 ,18 );           
        }
        /// <summary>
        /// 调整控件大小
        /// </summary>
        /// <param name="length">控件调整之前的长度</param>
        protected virtual void AdjustControlSize ( int length )
        {           
            int width=18;
            this.SuspendLayout ( );           
            switch ( DisplayStyle  )
            {               
                case DisplayStyle.Text:
                    if(!String.IsNullOrEmpty(this.Text))width = Math.Max ( 18 , ( int ) this.Font.Height );
                   
                    break;
                case DisplayStyle.Image:
                    if ( Image != null )
                    {
                        width = Math.Max ( width , this.Orientation == Orientation.Horizontal ? Image.Width : Image.Height );
                    }
                    break;
                case DisplayStyle.ImageAndText:
                    if ( !String.IsNullOrEmpty ( this.Text ) ) width = Math.Max ( 18 , ( int ) this.Font.Height );
                    if ( Image != null )
                    {
                        width = Math.Max ( width , this.Orientation == Orientation.Horizontal ? Image.Width : Image.Height );
                    }
                    break;
                default:                   
                    break;
            }           
            switch ( this.Orientation )
            {
                case Orientation.Horizontal:
                    this.Height = width;
                    this.Width = length;
                    break;
                case Orientation.Vertical:
                    this.Width = width;
                    this.Height = length;
                    break;
                default:
                    break;
            }

            this.ResumeLayout ( false );           
        }

        /// <summary>
        /// 获取控件中图像的显示区域
        /// </summary>
        protected  Rectangle getImageArea ( )
        {
            using ( Graphics g= this.CreateGraphics ( ) )
            {
                return getImageArea ( g );
            }
        }
        /// <summary>
        /// 获取控件中图像的显示区域
        /// </summary>
        /// <param name="g">代表Graphics对象</param>
        protected  Rectangle getImageArea (Graphics g )
        {
            Rectangle rect=getContextArea ( g );
            switch ( DisplayStyle  )
            {
                case DisplayStyle.Image:
                    return rect;
                case DisplayStyle.ImageAndText:                   
                    break;
                default:
                    return Rectangle.Empty;
            }
           
            int x=0,y=0,w=0,h=0;
            w = h = this.Orientation == Orientation.Horizontal ? this.Height : this.Width;

            if ( ( this.TextAlign == StringAlignment.Far ) ^ ( this.RightToLeft == RightToLeft.Yes ) )
            {       //右对齐
                x = rect.Right  - w;
                y = rect.Bottom  - h;
            }
            else
            {       //左对齐
                x = rect.Left;
                y = rect.Top;
            }           
            Rectangle rectImage=new Rectangle ( x, y , w , h );
            rectImage.Inflate ( -IMAGEMARGIN , -IMAGEMARGIN );
            return rectImage;           
        }
        /// <summary>
        /// 获取或内容显示区矩形
        /// </summary>
        /// <param name="g">代表Graphics对象</param>
        /// <returns>返回内容显示的区域对象</returns>
        protected Rectangle getContextArea ( Graphics g)
        {
            bool showText=false,showImage=false;
            switch ( this.DisplayStyle )
            {
                case DisplayStyle.Text:
                    showText = true;
                    break;
                case DisplayStyle.Image:
                    showImage = true;
                    break;
                case DisplayStyle.ImageAndText:
                    showText = true;
                    showImage = true;
                    break;
                default:
                    break;
            }
            if ( !showText && !showImage ) return Rectangle.Empty;

            int x=0,y=0,w=0,h=0;
            if ( showText )
            {
                using ( StringFormat sf=new StringFormat ( StringFormatFlags.NoClip | StringFormatFlags.NoWrap ) )
                {
                    if ( this.Orientation == Orientation.Vertical )
                        sf.FormatFlags = sf.FormatFlags.Apply( StringFormatFlags.DirectionVertical );
                    SizeF sz = g.MeasureString ( this.Text , this.Font , 0 , sf );
                    if ( this.Orientation == Orientation.Horizontal )
                    {
                        w = ( int ) sz.Width + 1;
                        h = this.Height ;
                    }
                    else
                    {
                        w = this.Width ;
                        h = ( int ) sz.Height + 1;
                    }
                }
            }
           
            if ( showImage )
            {               
                if ( this.Orientation == Orientation.Horizontal )
                {
                    w += this.Height ;
                    h = this.Height;
                }
                else
                {
                    w = this.Width;
                    h += this.Width; ;
                }
            }
           
            if ( this.TextAlign == StringAlignment.Center )
            {
                x = ( this.Width - w ) / 2;
                y = ( this.Height - h ) / 2;
            }
            else if ( (this.TextAlign == StringAlignment.Far) ^ (this.RightToLeft == RightToLeft.Yes) )
            {       //右对齐
                x = this.Width - w;
                y = this.Height - h;
            }
            else
            {       //左对齐
                x = 0;
                y = 0;
            }
           return new Rectangle ( x , y , w , h );

        }

        /// <summary>
        /// 获取控件中的文本显示区
        /// </summary>
        /// <param name="g">用于测绘文本大小的Graphics对象。</param>
        /// <returns>如果控件当前显示文本,且控件文本不为null,则返回相应的Rectangle对象,否则返回Rectangle.Empty。</returns>
        private  Rectangle getTextArea ( Graphics g )
        {
            Rectangle rect=getContextArea ( g );
            switch ( DisplayStyle  )
            {
                case DisplayStyle.Text:
                    return rect;                   
                case DisplayStyle.ImageAndText:
                    break;
                default:
                    return Rectangle.Empty;
            }

           
            int x=0,y=0,w=0,h=0;

               
                if ( this.Orientation == Orientation.Horizontal )
                {
                    w = rect.Width-rect.Height;
                    h=rect.Height;
                }
                else
                {
                    w=rect.Width;
                    h = rect.Height-rect.Width ;
                }

                if ( ( this.TextAlign == StringAlignment.Far ) ^ ( this.RightToLeft == RightToLeft.Yes ) )
            {       //右对齐
                x = rect.Left ;
                y = rect.Top;
            }
            else
            {       //左对齐
                x = rect.Right - w;
                y = rect.Bottom - h;
            }
           return new Rectangle ( x , y , w , h );
           
        }

        /// <summary>
        /// 获取控件中的文本显示区
        /// </summary>
        /// <returns>如果控件当前显示文本,且控件文本不为null,则返回相应的Rectangle对象,否则返回Rectangle.Empty。</returns>
        private Rectangle getTextArea ( )
        {
            using ( Graphics g=this.CreateGraphics ( ) )
            {
                return getTextArea ( g );
            }
        }


        /// <summary>
        /// 引发基类OnPaint事件
        /// </summary>
        /// <param name="e">包含事件数据的PaintEventArgs对象</param>       
        protected override void OnPaint ( PaintEventArgs e )
        {
            Graphics g=e.Graphics;
                      
            Point ptStart,ptEnd,ptStart_,_ptEnd;
            Color baseColor=this.BackColor ,
                clrTopLeft=Color.Transparent ,
                clrRightBottom=Color.Transparent;
            Pen pen;
            Brush bsh;
            StringFormat sf;

            ptStart = getStartPoint ( );
            ptEnd = getEndPoint ( );
            baseColor = getLineColor ( this );
            switch ( LineStyle )
            {
                case LineStyle.Flat:
                    clrTopLeft = ControlPaint.DarkDark ( baseColor );
                    break;
                case LineStyle.Raised:
                    clrTopLeft = ControlPaint.Light ( baseColor , PERCCOLORCHANGED );//.LightLight(baseColor);//
                    clrRightBottom = ControlPaint.Dark ( baseColor , PERCCOLORCHANGED );//.DarkDark(baseColor);//
                    break;
                case LineStyle.Sunken:
                    clrTopLeft = ControlPaint.Dark ( baseColor , 0.3F );
                    clrRightBottom = ControlPaint.Light ( baseColor , 0.3F );
                    break;
                default:
                    clrTopLeft = Color.Transparent;
                    clrRightBottom = Color.Transparent;
                    break;
            }
            pen = new Pen ( clrTopLeft );

            Rectangle rectContext=getContextArea ( g );
                if ( this.TextAlign == StringAlignment.Center )
                {
                    ptStart_ =ptStart;
                    _ptEnd=ptEnd;
                    if ( this.Orientation == Orientation.Horizontal )
                    {
                        ptStart_.Offset ( rectContext.Left - 2 , 0 );
                        _ptEnd.Offset ( rectContext.Right - this.Right + 2 , 0 );
                    }
                    else
                    {
                        ptStart_.Offset ( 0 , rectContext.Top - 2 );
                        _ptEnd.Offset ( 0 , rectContext.Right - this.Right + 2 );
                    }
                   
                    _ptEnd = new Point ( rectContext.Right+1 , ptEnd.Y );
                    g.DrawLine ( pen , ptStart , ptStart_ );
                    g.DrawLine ( pen , _ptEnd , ptEnd );
                    if ( this.LineStyle != LineStyle.Flat )
                    {
                        pen.Color = clrRightBottom;
                        ptStart.Offset ( 1 , 1 );
                        ptEnd.Offset ( 1 , 1 );
                        ptStart_.Offset ( 1 , 1 );
                        _ptEnd.Offset ( 1 , 1 );
                        g.DrawLine ( pen , ptStart , ptStart_ );
                        g.DrawLine ( pen , _ptEnd , ptEnd );
                    }
                }
                else
                {
                    g.DrawLine ( pen , ptStart , ptEnd );
                    if ( this.LineStyle != LineStyle.Flat )
                    {
                        pen.Color = clrRightBottom;
                        ptStart.Offset ( 1 , 1 );
                        ptEnd.Offset ( 1 , 1 );
                        g.DrawLine ( pen , ptStart , ptEnd );
                    }
                }
                pen.Dispose ( );
           

            //
            // 绘制文本***********************
            //
            if ( DisplayStyle == DisplayStyle.Text || DisplayStyle == DisplayStyle.ImageAndText )
            {
                Rectangle rectText=getTextArea ( );
                if ( this.CanSelect )
                {
                    if ( this.Focused )
                    {
                        bsh = new SolidBrush ( SystemColors.ActiveCaption );
                    }
                    else
                    {
                        bsh = new SolidBrush ( SystemColors.InactiveCaption );
                    }
                }
                else
                {
                    bsh = new SolidBrush ( this.ForeColor );
                }
                sf = new StringFormat ( );
                sf.LineAlignment = StringAlignment.Center;
                if ( this.Orientation == Orientation.Vertical )
                {
                    sf.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.NoClip | StringFormatFlags.DirectionVertical;
                }
                else
                {
                    sf.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.NoClip;
                }
                sf.Trimming = StringTrimming.EllipsisCharacter;
                if ( this.Focused ) ControlPaint.DrawFocusRectangle ( g , rectText , this.ForeColor , this.BackColor );
                if ( this.Enabled )
                {
                    g.DrawString ( this.Text , this.Font , bsh , rectText , sf );
                }
                else
                {
                    ControlPaint.DrawStringDisabled ( g , this.Text , this.Font , Color.Transparent , rectText , sf );
                }
                bsh.Dispose ( );
                sf.Dispose ( );
            }
            //显示图像
            Image img=null;
            System.Drawing.Imaging.ImageAttributes ia=new System.Drawing.Imaging.ImageAttributes ( );
            if ( DisplayStyle == DisplayStyle.Image || DisplayStyle == DisplayStyle.ImageAndText )
            {
                img = this.Image;
                ia.SetColorKey ( this.ImageKey , this.ImageKey , System.Drawing.Imaging.ColorAdjustType.Bitmap );
            }
            if ( img == null ) return;
            Rectangle rectImage=getImageArea ( );
            if ( this.Enabled )
            {
                g.DrawImage ( img , rectImage , 0 , 0 , img.Width , img.Height , GraphicsUnit.Pixel , ia );
            }
            else
            {
                ControlPaint.DrawImageDisabled ( g , img , 0 , 0 , this.ImageKey );
            }
           
        }

        protected override void OnMouseClick ( MouseEventArgs e )
        {
            base.OnMouseClick ( e );
            this.Focus ( );
        }

        protected override void OnFontChanged ( EventArgs e )
        {
            base.OnFontChanged ( e );
            AdjustControlSize ( this.Orientation == Orientation.Horizontal ? this.Width : this.Height );
        }

        protected override void OnEnter ( EventArgs e )
        {
            base.OnEnter ( e );
           
            if ( this.CanSelect  )
            {
                this.Invalidate ( getTextArea ( ) );
            }
            else
            {
                if ( this.Parent != null ) ( this.Parent as ContainerControl ).SelectNextControl ( this , true , true , true , true );
            }
           
        }
        protected override void OnLeave ( EventArgs e )
        {
            base.OnLeave ( e );           
            this.Invalidate ( getTextArea ( ) );
        }

        protected override void OnEnabledChanged ( EventArgs e )
        {
            base.OnEnabledChanged ( e );
            this.Invalidate ( getTextArea ( ) );
        }
        /// <summary>
        /// 取得有效的线条颜色,通常为控件的背景色,但如果其背景色为Transparent,
        /// 则使用父控件的背景色,依此类推,若其顶层控件的背景色仍为Transparent,
        /// 则使用默认的Color.ActiveBorder颜色
        /// </summary>
        /// <returns>一个有效的Color结构</returns>
        private Color getLineColor ( Control parent)
        {
            if ( parent!=null )
            {
                if ( parent.BackColor!=Color.Transparent  )
                {
                    return parent.BackColor;
                }
                else
                {
                    return getLineColor ( parent.Parent );
                }
            }
            else
            {
                return SystemColors.ActiveBorder;
            }
        }

        /// <summary>
        /// 取得控件开始的Point对象
        /// </summary>
        private Point getStartPoint ( )
        {
            int x,y;
            Rectangle rect;
            using ( Graphics g=this.CreateGraphics() )
            {
                rect=getContextArea ( g );
            }
           
            if ( rect==Rectangle.Empty  )
            {   //如果当前不显示内容。。。
                if ( this.Orientation== Orientation.Horizontal  )
                {
                    return new Point ( 0 , this.ClientRectangle.Height / 2 );
                }
                else
                {
                    return new Point ( this.ClientRectangle.Width / 2 , 0 );
                }               
            }
            //如果当前显示内容。。。
            if ( this.Orientation == Orientation.Horizontal )
            {
                x = ( ( this.TextAlign == StringAlignment.Near ) ^ ( this.RightToLeft == RightToLeft.Yes ) ) ?
                    rect.Right:0;
                y = rect.Height / 2;
            }
            else
            {
                y = ( ( this.TextAlign == StringAlignment.Near ) ^ ( this.RightToLeft == RightToLeft.Yes ) ) ?
                    rect.Bottom:0;
                x = rect.Width / 2;
            }
            return new Point ( x , y );
        }

        /// <summary>
        /// 取得控件结束的Point对象
        /// </summary>
        private Point getEndPoint ( )
        {
            int x,y;
            Rectangle rect;
            using ( Graphics g=this.CreateGraphics ( ) )
            {
                rect = getContextArea ( g );
            }           
            if ( rect == Rectangle.Empty )
            {   //如果当前不显示文本。。。
                if ( this.Orientation== Orientation.Horizontal  )
                {
                    return new Point ( this.ClientRectangle.Right , this.ClientRectangle.Bottom / 2 );
                }
                else
                {
                    return new Point ( this.ClientRectangle.Right / 2 , this.ClientRectangle.Bottom );
                }
            }
            //如果当前显示内容。。。
            if ( this.Orientation == Orientation.Horizontal )
            {
                x = ( ( this.TextAlign == StringAlignment.Far ) ^ ( this.RightToLeft == RightToLeft.Yes ) ) ?
                    rect.Left : this.Right ;               
                y = rect.Height / 2;
            }
            else
            {
                y = ( ( this.TextAlign == StringAlignment.Far ) ^ ( this.RightToLeft == RightToLeft.Yes ) ) ?
                        rect.Top:this.Bottom ;
                x = rect.Width / 2;
            }
            return new Point ( x , y );
        }

    }

    #region 扩展设计时支持
    class LineDesigner : Etonesoft.Windows.Controls.Design.ControlDesigner<Line >//; ControlDesigner
    {
         public override void Initialize ( IComponent component )
         {
             base.Initialize ( component );
             this.ActionList = new LineDesignerActionList ( this.Component );
         }
        public override System.Windows.Forms.Design.SelectionRules SelectionRules
        {

            get
            {
                Line l=this.Control as Line;
                if ( l.Orientation== Orientation.Horizontal  )
                {
                    return System.Windows.Forms.Design.SelectionRules.Visible |
                    System.Windows.Forms.Design.SelectionRules.Moveable |
                    System.Windows.Forms.Design.SelectionRules.LeftSizeable |
                    System.Windows.Forms.Design.SelectionRules.RightSizeable;
                }
                else
                {
                    return System.Windows.Forms.Design.SelectionRules.Visible |
                    System.Windows.Forms.Design.SelectionRules.Moveable |
                    System.Windows.Forms.Design.SelectionRules.TopSizeable |
                    System.Windows.Forms.Design.SelectionRules.BottomSizeable;
                }

            }
        }
    }
    class LineDesignerActionList : CommonDesignerActionList<Line>
     {
         public bool ShowImageKey { get { return Image != null; } }
         public bool ShowContextAlign { get { return !string.IsNullOrEmpty ( Text ) || Image != null; } }
         public bool Vertical { get { return Control.Orientation == Orientation.Vertical; } }
         public bool Horizontal { get { return Control.Orientation == Orientation.Horizontal; } }

         [ActionListItem ( "控件方向" , "常规" )]
         public Orientation Orientation
         {
             get { return Control.Orientation; }
             set
             {
                 Control.Orientation = value;
                 RegainsControl ( );
             }
         }
         [ActionListItem ( "线条样式" , "常规" )]
         public LineStyle LineStyle
         {
             get { return Control.LineStyle; }
             set { Control.LineStyle = value; }
         }

         [ActionListItem ( "显示文本" , "文本" )]
         public string Text
         {
             get { return Control.Text; }
             set { Control.Text = value; RegainsControl ( ); }
         }
         [ActionListItem ( "显示颜色" , "图像" )]
         public Image Image
         {
             get { return Control.Image; }
             set { Control.Image = value; RegainsControl ( ); }
         }
         [ActionListItem ( "透明颜色" , "图像" )]
         [ActionListDesplayCondition ( "ShowImageKey" )]
         public Color ImageKey
         {
             get { return Control.ImageKey; }
             set { Control.ImageKey = value; }
         }
         [ActionListItem ( "对齐方式" , "布局" )]
         [ActionListDesplayCondition ( "ShowContextAlign" )]
         public StringAlignment ContextAlign
         {
             get { return Control.TextAlign; }
             set { Control.TextAlign = value; }
         }

         public LineDesignerActionList ( IComponent component ) : base ( component ) { base.Flags = 2; }

         [ActionListItem ( "与容器控件左上边缘停靠" , "布局" , "与容器控件左/上边缘停靠" , true )]
         [ActionListDesplayCondition ( 2 )]
         [ActionListDesplayCondition ( "Vertical" )]
         public void DockToTopLeft ( )
         {
             Control.Dock = Orientation == Orientation.Horizontal ? DockStyle.Top : DockStyle.Left;
             RegainsControl ( );
             Flags = 1;
         }
         [ActionListItem ( "与容器控件右下边缘停靠" , "布局" , "与容器控件右/下边缘停靠" , true )]
         [ActionListDesplayCondition ( 2 )]
         [ActionListDesplayCondition ( "Horizontal" )]
         public void DockToBottomRight ( )
         {
             Control.Dock = Orientation == Orientation.Horizontal ? DockStyle.Bottom : DockStyle.Right;
             RegainsControl ( );
             Flags = 1;
         }
         [ActionListItem ( "取消与容器停靠" , "布局" , "取消与容器停靠" , true )]
         [ActionListDesplayCondition ( 1 )]
         public void CancelDock ( )
         {
             Control.Dock = DockStyle.None;
             RegainsControl ( );
             Flags = 2;
         }
     }
#endregion
}

posted @ 2009-04-23 22:07  蒋启磊  阅读(387)  评论(0编辑  收藏  举报