WinForm自定义控件开发(1)

类图

1)   具有渐变色的Label控件MyLabel:具有渐变色的标签

2)   实现坐标系控件 MyCoordinate

 

代码

1)         MyLabel

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Drawing;

using System.ComponentModel;

using System.Drawing.Drawing2D;

 

 

namespace WindowsFormsControlLibrary

{

    /// <summary>

    /// 自定义Label

    /// </summary>

    /// <remarks>

    /// 渐变背景的Label

    /// </remarks>

    public class MyLabel:Control

    {

        /// <summary>

        /// 背景渐变颜色2

        /// </summary>

        private Color backColor2;

 

        /// <summary>

        /// 背景渐变颜色2

        /// </summary>

        public Color BackColor2

        {

            get

            {

                return backColor2;

            }

            set

            {

                //重绘控件

                this.Invalidate();

                backColor2 = value;

            }

        }

 

        public override string Text

        {

            get

            {

                return base.Text;

            }

            set

            {

                //重绘控件

                this.Invalidate();

                base.Text = value;

            }

        }

 

        protected override void OnPaint(PaintEventArgs e)

        {

            base.OnPaint(e);

 

            LinearGradientBrush currentBrush = new LinearGradientBrush

            (

            new Point(0, 0),

            new Point(0, Height),

            BackColor, backColor2

            );

 

            e.Graphics.FillRectangle(currentBrush, ClientRectangle);

 

            //测量字体所占的矩形大小

            SizeF textSize = e.Graphics.MeasureString(Text, Font);

 

            Brush foreBrush = new SolidBrush(ForeColor);

 

            e.Graphics.DrawString(Text, Font, foreBrush,

                new PointF(

                    (Width - textSize.Width) / 2,

                    (Height - textSize.Height) / 2

                    )

                    );

 

            currentBrush.Dispose();

            foreBrush.Dispose();

        }

 

        private void InitializeComponent()

        {

            this.SuspendLayout();

            this.ResumeLayout(false);

 

        }

    }

}

 

2)   MyCoordinate

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Drawing;

using System.ComponentModel;

using System.Drawing.Drawing2D;

 

namespace WindowsFormsControlLibrary

{

    public class MyCoordinate:Control

    {

 

        //坐标轴X与控件边缘的距离

        private const int xPad = 80;

 

        //坐标轴Y与控件边缘的距离

        private const int yPad = 60;

 

        /// <summary>

        /// X坐标标签间距

        /// </summary>

        private int coordinateLablePadX = 20;

        public int CoordinateLablePadX

        {

            get

            {

                return coordinateLablePadX;

            }

 

            set

            {

                Invalidate();

                coordinateLablePadX = value;

            }

        }

       

 

        /// <summary>

        /// Y坐标标签间距

        /// </summary>

        private int coordinateLablePadY = 30;

        public int CoordinateLablePadY

        {

            get

            {

                return coordinateLablePadY;

            }

 

            set

            {

                Invalidate();

                coordinateLablePadY = value;

            }

        }

 

        /// <summary>

        /// y坐标轴起点坐标

        /// </summary>

        public Point YCoordinateStartPoint

        {

            get

            {

                return new Point(xPad, this.Height - yPad);

 

            }

        }

 

       

        /// <summary>

        /// y坐标轴终点坐标

        /// </summary>

        public Point YCoordinateEndPoint

        {

            get

            {

                return new Point(xPad, yPad);

            }

        }

 

 

        /// <summary>

        /// x坐标轴起点坐标

        /// </summary>

        public Point XCoordinateStartPoint

        {

            get

            {

                return YCoordinateStartPoint;

            }

        }

 

        /// <summary>

        /// x坐标轴终点坐标

        /// </summary>

        public Point XCoordinateEndPoint

        {

            get

            {

                Point current = new Point();

                current.X = Width-xPad ;

                current.Y = Height - yPad ;

                return current;

            }

        }

 

        /// <summary>

        /// 当前鼠标的位置坐标

        /// </summary>

        private Point currentMouseLocation = new Point();

 

        private void InitializeComponent()

        {

            this.SuspendLayout();

            //

            // MyCoordinate

            //

 

            this.ResumeLayout(false);

 

        }

 

        /// <summary>

        /// 重载绘制事件

        /// </summary>

        /// <param name="e"></param>

        protected override void OnPaint(PaintEventArgs e)

        {

            base.OnPaint(e);

            Pen currentPen=new Pen(Color.Black ) ;

 

            currentPen.EndCap = LineCap.ArrowAnchor ;

 

            //绘制Y轴

            e.Graphics.DrawLine(

                currentPen,

                YCoordinateStartPoint,

                YCoordinateEndPoint

                );

 

            //绘制Y轴

            e.Graphics.DrawLine(

                currentPen,

                XCoordinateStartPoint,

                XCoordinateEndPoint

                );

            currentPen.Dispose();

 

            //绘制X,Y 标签

            DrawCoordinateLabel(e.Graphics, YCoordinateStartPoint, YCoordinateEndPoint,XCoordinateStartPoint, XCoordinateEndPoint);

           

 

        }

 

        /// <summary>

        /// 重载鼠标移动事件

        /// </summary>

        /// <param name="e"></param>

        protected override void OnMouseMove(MouseEventArgs e)

        {

            base.OnMouseMove(e);

            currentMouseLocation = e.Location;

            this.Invalidate();

        }

 

      

        /// <summary>

        /// 绘制坐标标记

        /// </summary>

        private void DrawCoordinateLabel(Graphics gp,Point startYPoint,Point endYPoint,Point startXPoint,Point endXPoint)

        {

            //int y = YCoordinateStartPoint .Y - YCoordinateEndPoint.Y;

 

            int currentValue=-1;

            float labelX = 0,labelY=0;

            int pad = 5;

 

            //绘制Y轴

            for (int current = YCoordinateStartPoint.Y; current >YCoordinateEndPoint.Y ; current -= CoordinateLablePadY)

            {

                currentValue++;

 

                SizeF textSize= gp.MeasureString(currentValue.ToString(), Font);

               

                labelX=YCoordinateStartPoint.X - xPad / 2;

                labelY=current-textSize.Height/2;

 

                //绘制label

                gp.DrawString(currentValue.ToString () ,Font,Brushes.Black,

                    new PointF(labelX ,labelY)

                        );

 

                //绘制|-

                gp.DrawLine(Pens.Black,new Point(YCoordinateStartPoint.X,current),new Point(YCoordinateStartPoint.X+pad,current ));

                    

            }

 

            currentValue = -1;

            //绘制X轴

            for (int current = XCoordinateStartPoint.X ; current < XCoordinateEndPoint.X ; current += CoordinateLablePadX)

            {

                currentValue++;

 

                SizeF textSize = gp.MeasureString(currentValue.ToString(), Font);

 

                labelY = XCoordinateStartPoint.Y + yPad / 2;

                labelX = current;

 

                //绘制label

                gp.DrawString(currentValue.ToString(), Font, Brushes.Black,

                    new PointF(labelX, labelY)

                        );

 

                //绘制_|_

                gp.DrawLine(Pens.Black,

                    new Point(current, XCoordinateStartPoint.Y ),

                new Point(current, XCoordinateStartPoint.Y-pad )

                );

              

 

            }

 

            //绘制交叉线

            List<Point> list = GetCurrentArrowLinesPoints(currentMouseLocation);

            gp.DrawLine(Pens.Blue , list[0], list[1]);

            gp.DrawLine(Pens.Blue, list[2], list[3]);

 

            //绘制交叉点的坐标

 

            DrawMousePointLabel(gp, currentMouseLocation);

 

           

        }

 

        /// <summary>

        /// 绘制当前鼠标所指的坐标位置

        /// </summary>

        /// <param name="ph"></param>

        /// <param name="current"></param>

        private void DrawMousePointLabel(Graphics ph,Point current)

        {

            int margin = 15;

            current = LimitMouseLocation(current);

 

 

            string text = string.Format("X:{0} Y:{1}",

                (current.X -xPad)/ CoordinateLablePadX,

                ((YCoordinateStartPoint.Y-YCoordinateEndPoint.Y)/CoordinateLablePadY)-(current.Y-yPad)/CoordinateLablePadY

                );

           

            ph.DrawString(text, Font, Brushes.Black , new PointF((float)current.X+margin , (float)current.Y));

        }

 

       

 

        /// <summary>

        /// 当前鼠标经过位置点的交叉线坐标

        /// </summary>

        /// <param name="p"></param>

        /// <returns></returns>

        private List<Point> GetCurrentArrowLinesPoints(Point p)

        {

            List<Point> list = new List<Point>();

 

            p = LimitMouseLocation(p);

 

           

            Point px1 = new Point(p.X, YCoordinateStartPoint.Y);

 

            list.Add(px1);

 

            Point px2 = new Point(p.X, YCoordinateEndPoint.Y);

            list.Add(px2);

 

 

            Point py1 = new Point(XCoordinateStartPoint.X ,p.Y );

 

            list.Add(py1);

 

            Point py2 = new Point(XCoordinateEndPoint.X, p.Y );

            list.Add(py2);

 

            return list;

 

           

        }

 

        /// <summary>

        /// 修正鼠标位置

        /// </summary>

        /// <param name="p"></param>

        /// <returns></returns>

        private Point LimitMouseLocation(Point p)

        {

            //X越界后修正

            if (p.X > XCoordinateEndPoint.X)

            {

                p.X = XCoordinateEndPoint.X;

            }

            else if (p.X < XCoordinateStartPoint.X)

            {

                p.X = XCoordinateStartPoint.X;

            }

 

            //Y越界后修正

            if (p.Y < YCoordinateEndPoint.Y)

            {

                p.Y = YCoordinateEndPoint.Y;

            }

            else if (p.Y > YCoordinateStartPoint.Y)

            {

                p.Y = YCoordinateStartPoint.Y;

            }

            return p;

        }

 

       

       

    }

 

}

 

运行效果

 



转载于:https://www.cnblogs.com/hbb0b0/archive/2010/11/24/1886858.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值