C# 绘制50000个矩形框

来源:互联网 发布:英迈零售软件 编辑:程序博客网 时间:2024/05/30 23:02

using System;using System.Collections.Generic;using System.Drawing;using System.Linq;using System.Windows.Forms;namespace WindowsFormsApplication3{    public class LineControl : Control     {        public LineControl()        {            base.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);        }          Bitmap bmp = null;        protected override void OnSizeChanged(EventArgs e)        {            Random rnd = new Random(100);            bmp = new Bitmap(this.Width, this.Height);            Graphics g = Graphics.FromImage(bmp);            System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath();            for (int i = 0; i < 5000; i++)            {                Rectangle rect = new Rectangle();                rect.Width = 10;                rect.Height = 10;                rect.X = rnd.Next(0, this.Width);                rect.Y = rnd.Next(0, this.Height);                gp.AddRectangle(rect);            }            g.DrawPath(Pens.Red, gp);            base.OnSizeChanged(e);            this.Invalidate();        }        public Point pt = Point.Empty;        protected override void OnMouseMove(MouseEventArgs e)        {            pt = e.Location;            this.Invalidate();            base.OnMouseMove(e);        }        protected override void OnPaint(PaintEventArgs e)        {            e.Graphics.DrawImage(bmp, 0, 0, this.Width, this.Height);            e.Graphics.DrawLine(Pens.RoyalBlue, pt.X, 0, pt.X, this.Height);            e.Graphics.DrawLine(Pens.RoyalBlue, 0, pt.Y, this.Width, pt.Y);            base.OnPaint(e);        }    }}

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace WindowsFormsApplication3{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();            base.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);        }        protected override void OnMouseMove(MouseEventArgs e)        {            this.Invalidate();            base.OnMouseMove(e);        }     }}

namespace WindowsFormsApplication3{    partial class Form1    {        /// <summary>        /// 必需的设计器变量。        /// </summary>        private System.ComponentModel.IContainer components = null;        /// <summary>        /// 清理所有正在使用的资源。        /// </summary>        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>        protected override void Dispose(bool disposing)        {            if (disposing && (components != null))            {                components.Dispose();            }            base.Dispose(disposing);        }        #region Windows 窗体设计器生成的代码        /// <summary>        /// 设计器支持所需的方法 - 不要        /// 使用代码编辑器修改此方法的内容。        /// </summary>        private void InitializeComponent()        {            this.lineControl1 = new WindowsFormsApplication3.LineControl();            this.SuspendLayout();            //             // lineControl1            //             this.lineControl1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)             | System.Windows.Forms.AnchorStyles.Left)             | System.Windows.Forms.AnchorStyles.Right)));            this.lineControl1.Location = new System.Drawing.Point(12, 1);            this.lineControl1.Name = "lineControl1";            this.lineControl1.Size = new System.Drawing.Size(941, 474);            this.lineControl1.TabIndex = 0;            this.lineControl1.Text = "lineControl1";            //             // Form1            //             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;            this.ClientSize = new System.Drawing.Size(965, 487);            this.Controls.Add(this.lineControl1);            this.Name = "Form1";            this.Text = "Form1";            this.ResumeLayout(false);        }        #endregion        private LineControl lineControl1;    }}







原创粉丝点击