首页 > 学院 > 开发设计 > 正文

winfrom-绘制十字线

2019-11-11 03:17:15
字体:
来源:转载
供稿:网友

using System;using System.Collections.Generic;using System.Drawing;using System.Drawing.Drawing2D;using System.Windows.Forms;namespace crossline{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();            ResizeRedraw = true; //改变大小重绘            DoubleBuffered = true;            SetStyle(ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);            this.Paint += new PaintEventHandler(Form1_Paint);            this.MouseClick += new MouseEventHandler(Form1_MouseClick);            this.MouseMove += new MouseEventHandler(Form1_MouseMove);            this.Resize += new EventHandler(this.Form1_Resize);        }        PRivate void Form1_Paint(object sender, PaintEventArgs e)        {            Pen pen = new Pen(Color.Black);            e.Graphics.SmoothingMode = SmoothingMode.HighQuality;            e.Graphics.Clear(SystemColors.Control);            foreach (var p in dicPoint)            {                e.Graphics.DrawLine(pen, p[0], p[1]);                e.Graphics.DrawLine(pen, p[2], p[3]);            }            var lineP = GetPoint();            e.Graphics.DrawLine(pen, lineP[0], lineP[1]);            e.Graphics.DrawLine(pen, lineP[2], lineP[3]);            if (clickTag)            {                dicPoint.Add(lineP);                clickTag = false; //重置标记            }        }        private Point[] GetPoint()        {            Point[] p = new Point[4];            var w = this.Width;            var h = this.Height;            p[0] = new Point(0, clickTag ? clickPoint.Y : mousePoint.Y);            p[1] = new Point(w, clickTag ? clickPoint.Y : mousePoint.Y);            p[2] = new Point(clickTag ? clickPoint.X : mousePoint.X, 0);            p[3] = new Point(clickTag ? clickPoint.X : mousePoint.X, h);            return p;        }        bool clickTag = false;        Point clickPoint, mousePoint;        readonly List<Point[]> dicPoint = new List<Point[]>();        private void Form1_MouseClick(object sender, MouseEventArgs e)        {            //if (e.Button == MouseButtons.Left)            clickTag = true;            clickPoint = new Point(e.X, e.Y);        }        private void Form1_Resize(object sender, EventArgs e)        {            this.label1.Text = string.Format("{0},{1}", Width, Height);            foreach (var p in dicPoint)            {                p[1].X = this.Width;                p[3].Y = this.Height;            }        }        private void Form1_MouseMove(object sender, MouseEventArgs e)        {            mousePoint = e.Location;            Invalidate();        }        //...    }}


上一篇:winfrom-绘制十字线

下一篇:火柴棒等式

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表