在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

    }
}
**********************************************************************************************************

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值