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; using System.Drawing.Drawing2D; using System.Drawing.Imaging; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } #region サンプルコード用 Color[] _color = new Color[] { Color.Black, Color.Blue, Color.Red, Color.Green, Color.Gold, Color.Purple }; LineCap[] _cap = new LineCap[] { LineCap.Flat, LineCap.Round, LineCap.Triangle, }; DashStyle[] _style = new DashStyle[] { DashStyle.Solid, DashStyle.Dot, DashStyle.Dash, DashStyle.DashDot, DashStyle.DashDotDot }; Random rnd = new Random(); #endregion const int cw = 640; const int ch = 480; Bitmap[] bmp = new Bitmap[2]; int screen = 0; Layer _Layer = new Layer(); PictureBox pictureBox1 = new PictureBox(); ToolStrip toolStrip1 = new ToolStrip(); ToolStripButton toolStripButton1 = new ToolStripButton(); private void Form1_Load(object sender, EventArgs e) { this.Controls.Add(toolStrip1); this.Controls.Add(pictureBox1); pictureBox1.Dock = DockStyle.Fill; //toolStrip1.SendToBack(); toolStrip1.Items.Add(toolStripButton1); toolStripButton1.Text = "Line"; toolStripButton1.Click += new EventHandler(toolStripButton1_Click); bmp[0] = new Bitmap(cw, ch, PixelFormat.Format32bppArgb); bmp[1] = new Bitmap(cw, ch, PixelFormat.Format32bppArgb); this.ScreenUpdate(); } private void toolStripButton1_Click(object sender, EventArgs e) { Pen p = new Pen(_color[rnd.Next(0,_color.Length-1)]); p.Width = (float)rnd.Next(1, 20); p.StartCap = _cap[rnd.Next(0,_cap.Length-1)]; p.EndCap = _cap[rnd.Next(0,_cap.Length-1)]; p.DashStyle = _style[rnd.Next(0,_style.Length-1)]; _Layer.AddLine(p, new PointF( (float)rnd.Next(20, cw-20), (float)rnd.Next(20, ch-20)), new PointF( (float)rnd.Next(20, cw-20), (float)rnd.Next(20, ch-20)) ); this.ScreenUpdate(); } void ScreenUpdate() { using (Graphics g = Graphics.FromImage(bmp[screen])) { g.SmoothingMode = SmoothingMode.AntiAlias; g.FillRectangle(Brushes.White, 0, 0, cw, ch); _Layer.Draw(g); } pictureBox1.Image = bmp[screen]; screen ^= 1; } } public class Layer { List _Shapes = new List(); public Layer() { } public void AddLine(Pen p,PointF p1,PointF p2) { Shape s = new Shape(); s.Pen = p; s.StartPoint = p1; s.EndPoint = p2; _Shapes.Add(s); } public void Draw(Graphics g) { foreach (Shape s in _Shapes) { switch (s.Type) { case 0: g.DrawLine(s.Pen, s.StartPoint, s.EndPoint); break; default: break; } } } } public class Shape { public int Type = 0; public Pen Pen; public PointF StartPoint; public PointF EndPoint; public Shape() { } } }