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<Shape> _Shapes = new List<Shape>();
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()
{
}
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uQ29sbGVjdGlvbnMuR2VuZXJpYzsKdXNpbmcgU3lzdGVtLkNvbXBvbmVudE1vZGVsOwp1c2luZyBTeXN0ZW0uRGF0YTsKdXNpbmcgU3lzdGVtLkRyYXdpbmc7CnVzaW5nIFN5c3RlbS5MaW5xOwp1c2luZyBTeXN0ZW0uVGV4dDsKdXNpbmcgU3lzdGVtLldpbmRvd3MuRm9ybXM7CnVzaW5nIFN5c3RlbS5EcmF3aW5nLkRyYXdpbmcyRDsKdXNpbmcgU3lzdGVtLkRyYXdpbmcuSW1hZ2luZzsKCm5hbWVzcGFjZSBXaW5kb3dzRm9ybXNBcHBsaWNhdGlvbjEKewogICAgcHVibGljIHBhcnRpYWwgY2xhc3MgRm9ybTEgOiBGb3JtCiAgICB7CiAgICAgICAgcHVibGljIEZvcm0xKCkKICAgICAgICB7CiAgICAgICAgICAgIEluaXRpYWxpemVDb21wb25lbnQoKTsKICAgICAgICB9CiAgICAgICAgI3JlZ2lvbiDjgrXjg7Pjg5fjg6vjgrPjg7zjg4nnlKgKICAgICAgICBDb2xvcltdIF9jb2xvciA9IG5ldyBDb2xvcltdCiAgICAgICAgewogICAgICAgICAgICBDb2xvci5CbGFjaywKICAgICAgICAgICAgQ29sb3IuQmx1ZSwKICAgICAgICAgICAgQ29sb3IuUmVkLAogICAgICAgICAgICBDb2xvci5HcmVlbiwKICAgICAgICAgICAgQ29sb3IuR29sZCwKICAgICAgICAgICAgQ29sb3IuUHVycGxlCiAgICAgICAgfTsKICAgICAgICBMaW5lQ2FwW10gX2NhcCA9IG5ldyBMaW5lQ2FwW10KICAgICAgICB7CiAgICAgICAgICAgIExpbmVDYXAuRmxhdCwKICAgICAgICAgICAgTGluZUNhcC5Sb3VuZCwKICAgICAgICAgICAgTGluZUNhcC5UcmlhbmdsZSwKICAgICAgICB9OwogICAgICAgIERhc2hTdHlsZVtdIF9zdHlsZSA9IG5ldyBEYXNoU3R5bGVbXQogICAgICAgIHsKICAgICAgICAgICAgRGFzaFN0eWxlLlNvbGlkLAogICAgICAgICAgICBEYXNoU3R5bGUuRG90LAogICAgICAgICAgICBEYXNoU3R5bGUuRGFzaCwKICAgICAgICAgICAgRGFzaFN0eWxlLkRhc2hEb3QsCiAgICAgICAgICAgIERhc2hTdHlsZS5EYXNoRG90RG90CiAgICAgICAgfTsKICAgICAgICBSYW5kb20gcm5kID0gbmV3IFJhbmRvbSgpOwogICAgICAgICNlbmRyZWdpb24KCiAgICAgICAgY29uc3QgaW50IGN3ID0gNjQwOwogICAgICAgIGNvbnN0IGludCBjaCA9IDQ4MDsKICAgICAgICBCaXRtYXBbXSBibXAgPSBuZXcgQml0bWFwWzJdOwogICAgICAgIGludCBzY3JlZW4gPSAwOwogICAgICAgIExheWVyIF9MYXllciA9IG5ldyBMYXllcigpOwogICAgICAgIFBpY3R1cmVCb3ggcGljdHVyZUJveDEgPSBuZXcgUGljdHVyZUJveCgpOwogICAgICAgIFRvb2xTdHJpcCB0b29sU3RyaXAxID0gbmV3IFRvb2xTdHJpcCgpOwogICAgICAgIFRvb2xTdHJpcEJ1dH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IwKTsKICAgICAgICAgICAgcC5TdGFydENhcCA9IF9jYXBbcm5kLk5leHQoMCxfY2FwLkxlbmd0aC0xKV07CiAgICAgICAgICAgIHAuRW5kQ2FwID0gX2NhcFtybmQuTmV4dCgwLF9jYXAuTGVuZ3RoLTEpXTsKICAgICAgICAgICAgcC5EYXNoU3R5bGUgPSBfc3R5bGVbcm5kLk5leHQoMCxfc3R5bGUuTGVuZ3RoLTEpXTsKICAgICAgICAgICAgX0xheWVyLkFkZExpbmUocCwKICAgICAgICAgICAgICAgIG5ldyBQb2ludEYoCiAgICAgICAgICAgICAgICAgICAgKGZsb2F0KXJuZC5OZXh0KDIwLCBjdy0yMCksCiAgICAgICAgICAgICAgICAgICAgKGZsb2F0KXJuZC5OZXh0KDIwLCBjaC0yMCkpLAogICAgICAgICAgICAgICAgbmV3IFBvaW50RigKICAgICAgICAgICAgICAgICAgICAoZmxvYXQpcm5kLk5leHQoMjAsIGN3LTIwKSwKICAgICAgICAgICAgICAgICAgICAoZmxvYXQpcm5kLk5leHQoMjAsIGNoLTIwKSkKICAgICAgICAgICAgICAgICAgICApOwogICAgICAgICAgICB0aGlzLlNjcmVlblVwZGF0ZSgpOwogICAgICAgIH0KICAgICAgICB2b2lkIFNjcmVlblVwZGF0ZSgpCiAgICAgICAgewogICAgICAgICAgICB1c2luZyAoR3JhcGhpY3MgZyA9IEdyYXBoaWNzLkZyb21JbWFnZShibXBbc2NyZWVuXSkpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGcuU21vb3RoaW5nTW9kZSA9IFNtb290aGluZ01vZGUuQW50aUFsaWFzOwogICAgICAgICAgICAgICAgZy5GaWxsUmVjdGFuZ2xlKEJydXNoZXMuV2hpdGUsIDAsIDAsIGN3LCBjaCk7CiAgICAgICAgICAgICAgICBfTGF5ZXIuRHJhdyhnKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBwaWN0dXJlQm94MS5JbWFnZSA9IGJtcFtzY3JlZW5dOwogICAgICAgICAgICBzY3JlZW4gXj0gMTsKICAgICAgICB9CiAgICB9CiAgICBwdWJsaWMgY2xhc3MgTGF5ZXIKICAgIHsKICAgICAgICBMaXN0PFNoYXBlPiBfU2hhcGVzID0gbmV3IExpc3Q8U2hhcGU+KCk7CiAgICAgICAgcHVibGljIExheWVyKCkKICAgICAgICB7CiAgICAgICAgfQogICAgICAgIHB1YmxpYyB2b2lkIEFkZExpbmUoUGVuIHAsUG9pbnRGIHAxLFBvaW50RiBwMikKICAgICAgICB7CiAgICAgICAgICAgIFNoYXBlIHMgPSBuZXcgU2hhcGUoKTsKICAgICAgICAgICAgcy5QZW4gPSBwOwogICAgICAgICAgICBzLlN0YXJ0UG9pbnQgPSBwMTsKICAgICAgICAgICAgcy5FbmRQb2ludCA9IHAyOwogICAgICAgICAgICBfU2hhcGVzLkFkZChzKTsKICAgICAgICB9CiAgICAgICAgcHVibGljIHZvaWQgRHJhdyhHcmFwaGljcyBnKQogICAgICAgIHsKICAgICAgICAgICAgZm9yZWFjaCAoU2hhcGUgcyBpbiBfU2hhcGVzKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBzd2l0Y2ggKHMuVHlwZSkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBjYXNlIDA6CiAgICAgICAgICAgICAgICAgICAgICAgIGcuRHJhd0xpbmUocy5QZW4sIHMuU3RhcnRQb2ludCwgcy5FbmRQb2ludCk7CiAgICAgICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgICAgIGRlZmF1bHQ6CiAgICAgICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAgcHVibGljIGNsYXNzIFNoYXBlCiAgICB7CiAgICAgICAgcHVibGljIGludCBUeXBlID0gMDsKICAgICAgICBwdWJsaWMgUGVuIFBlbjsKICAgICAgICBwdWJsaWMgUG9pbnRGIFN0YXJ0UG9pbnQ7CiAgICAgICAgcHVibGljIFBvaW50RiBFbmRQb2ludDsKICAgICAgICBwdWJsaWMgU2hhcGUoKQogICAgICAgIHsKICAgICAgICB9CiAgICB9Cn0K