c#畫一條橫線


VS中沒有VB的橫線控件,沒關系,動手做一個豐衣足食,順便還實現了豎線,斜線的功能

image

代碼比較簡單,創建一個繼承Control的控件,編寫代碼如下:

    public partial class Line : Control
    {
        public Line()
        {
            InitializeComponent();
        }

        private LineTypeEnum lineType = LineTypeEnum.Horizontal;
        [Category("自定義"), Description("線型")]
        public LineTypeEnum LineType
        {
            set
            {
                lineType = value;
                this.Refresh();
            }
            get
            {
                return lineType;
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
            e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            e.Graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

            if (lineType == LineTypeEnum.Horizontal)
            {
                e.Graphics.DrawLine(Pens.Gray, 0, 0, this.Width, 0);
                e.Graphics.DrawLine(Pens.White, 0, 1, this.Width, 1);
            }
            else if (lineType == LineTypeEnum.Vertical)
            {
                e.Graphics.DrawLine(Pens.Gray, 0, 0, 0, this.Height);
                e.Graphics.DrawLine(Pens.White, 1, 0, 1, this.Height);
            }
            else if (lineType == LineTypeEnum.LeftTopToRightBottom)
            {
                e.Graphics.DrawLine(Pens.Gray, 0, 0, this.Width, this.Height);
                e.Graphics.DrawLine(Pens.White, 0, 1, this.Width, this.Height + 1);
            }
            else
            {
                e.Graphics.DrawLine(Pens.Gray, 0,this.Height, this.Width, 0);
                e.Graphics.DrawLine(Pens.White,0, this.Height+1, this.Width, 1 );

            }
        }
        /// <summary>
        /// 線型
        /// </summary>
        public enum LineTypeEnum
        {
            Horizontal,
            Vertical,
            LeftTopToRightBottom,
            LeftBottomToRightTop
        }
    }

 

使用時,將控件拖到窗體上,設置控件Width,Height,設置屬性線性LineType即可


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM