程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 在C#裡面畫直線的

在C#裡面畫直線的

編輯:C#入門知識

 

using System;

using System.Collections.Generic;

using System.Text;

using System.Drawing;

using System.ComponentModel;

using System.Windows.Forms;

 

namespace Line

{

   

    public class LineControl : Control

    {

        private LineType m_lineType = LineType.Horizontal;

        private Color m_lineColor = Color.Blue;

        private Color m_shadowColor = Color.Gray;

        private int m_lineWidth = 2;

        private Size m_shadowOffset = new Size(1, 1);

        public LineControl()

        {

            this.SetStyle(ControlStyles.ResizeRedraw | ControlStyles.UserPaint | ControlStyles.SupportsTransparentBackColor, true);

            this.SetStyle(ControlStyles.UserMouse |

                ControlStyles.Selectable |

                ControlStyles.StandardClick |

                ControlStyles.StandardDoubleClick, false);

        }

        [Category("線")]

        [Description("陰影的偏移量")]

        [DefaultValue(typeof(Size), "1,1")]

        public Size ShadowOffset

        {

            get { return m_shadowOffset; }

            set

            {

 

                m_shadowOffset = value;

                this.SetSize();

                this.Invalidate();

            }

        }

        [Category("線")]

        [Description("寬度")]

        [DefaultValue(2)]

        public int LineWidth

        {

            get { return m_lineWidth; }

            set

            {

                if (value < 1) value = 1;

                m_lineWidth = value;

                this.SetSize();

                this.Invalidate();

            }

        }

        [Category("線")]

        [Description("類型")]

        [DefaultValue(typeof(LineType), "Horizontal")]

        public LineType LineType

        {

            get { return m_lineType; }

            set

            {

                if (m_lineType != value)

                {

                    int linelen = 0;

                    if (m_lineType == LineType.Horizontal)

                    {

                        linelen = this.Width;

                    }

                    else

                    {

                        linelen = this.Height;

                    }

                    m_lineType = value;

                    if (value == LineType.Horizontal)

                    {

                        this.Width = linelen;

                    }

                    else

                    {

                        this.Height = linelen;

                    }

                    this.SetSize();

                    this.Invalidate();

                }

            }

        }

        [Category("線")]

        [Description("顏色")]

        [DefaultValue(typeof(Color), "Blue")]

        public Color LineColor

        {

            get { return m_lineColor; }

            set

            {

                m_lineColor = value;

                this.Invalidate();

            }

        }

        [Category("線")]

        [Description("陰影的顏色")]

        [DefaultValue(typeof(Color), "Gray")]

        public Color ShadowColor

        {

            get { return m_shadowColor; }

            set

            {

                m_shadowColor = value;

                this.Invalidate();

            }

        }

        protected override void OnPaint(PaintEventArgs e)

        {

            Pen p1 = new Pen(this.LineColor, this.LineWidth);

            Pen p2 = new Pen(this.ShadowColor, this.LineWidth);

            if (this.LineType == LineType.Horizontal)

            {

                e.Graphics.DrawLine(p2, new Point(ShadowOffset.Width, ShadowOffset.Height), new Point(this.Right, ShadowOffset.Height));

                e.Graphics.DrawLine(p1, Point.Empty, new Point(this.Right - ShadowOffset.Width, 0));

            }

            else

            {

                e.Graphics.DrawLine(p2, new Point(ShadowOffset.Width, ShadowOffset.Height), new Point(ShadowOffset.Width, this.Bottom));

                e.Graphics.DrawLine(p1, Point.Empty, new Point(0, this.Bottom - ShadowOffset.Height));

            }

            p1.Dispose();

            p2.Dispose();

        }

        protected override void OnSizeChanged(EventArgs e)

        {

            base.OnSizeChanged(e);

            SetSize();

        }

        private void SetSize()

        {

            if (this.LineType == LineType.Horizontal)

            {

                this.Height = LineWidth + ShadowOffset.Height;

            }

            else

            {

                this.Width = LineWidth + ShadowOffset.Width;

            }

        }

    }

    /// <summary>

    /// 線的類型

    /// </summary>

    public enum LineType

    {

        /// <summary>

        /// 橫線

        /// </summary>

        Horizontal,

        /// <summary>

        /// 豎線

        /// </summary>

        Vertical

 

    }

}

 

摘自 Allen Chen 的專欄

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved