C# 画横线控件

功能:画水平横线

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;

namespace kongjian1
{
    public partial class kongjian2 : UserControl        //部分类定义
    {
        public kongjian2()                              //构造函数,在构造函数中进行必要的初始化
        {
            InitializeComponent();
            this.lineColor = this.ForeColor;
            this.lineWidth = 1;
        }
        #region 属性定义                               
        private Color lineColor;                        //通过#region和#endregion实现分类
        private int lineWidth;                          //进行私有成员,字段的定义

        public Color LineColor                          //进行属性定义,可以保护私有字段
        {
            get
            {
                return this.lineColor;                  //获取属性,返回lineColor
            }
            set
            {
                this.lineColor = value;                 //设置属性,通过value传入
                PaintEventArgs ep = new PaintEventArgs(this.CreateGraphics(),this.ClientRectangle);//用指定的图形和剪辑矩形框来初始化PaintEventArgs对象
                this.kongjian2_Paint(this,ep);          //字段修改后,执行Paint方法,这样的话,可以实现对象的重绘
            }
        }
        public int LineWidth                            //同上
        {
            get
            {
                return this.lineWidth;
            }
            set
            {
                this.lineWidth = value;
                PaintEventArgs ep = new PaintEventArgs(this.CreateGraphics(),this.ClientRectangle);
                this.kongjian2_Paint(this,ep);
            }
        }

        #endregion
        /// <summary>
        /// 重新绘制控件时,发生Paint事件,PaintEventArgs指定绘制控件所使用的Graphics以及绘制控件所在的ClipRectangle。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void kongjian2_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen p = new Pen(this.lineColor);
            p.Width = this.lineWidth*2;
            this.Height = this.LineWidth;
            g.DrawLine(p,0,0,e.ClipRectangle.Right,0);
        }

        private void kongjian2_Resize(object sender, EventArgs e)   //窗体改变时触发
        {
            this.Height = this.lineWidth;
        }
    }
}

posted @ 2013-03-20 20:56  在路上的人  阅读(2462)  评论(0)    收藏  举报