//絵の書けるピクチャーボックスクラス
//マウスのボタンにより、直線や円などが書けます
//また絵をファイルに保存も出来ます
//PanelX mePanel;
//private void Form1_Load(object sender, System.EventArgs e)
//{
// mePanel = new PanelX();
// mePanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
// mePanel.Location = new System.Drawing.Point(24, 32);
// mePanel.Size = new System.Drawing.Size(400, 400);
// this.Controls.Add(mePanel);
//}
using System;
public class PanelX : System.Windows.Forms.PictureBox
{
public PanelX()
{
this.Paint += new System.Windows.Forms.PaintEventHandler(this._Paint);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this._MouseMove);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this._MouseDown);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this._MouseUp);
}
const int Line = 10;
const int Pai =20;
[Serializable]
struct data{
public data(data pos)
{
this.i_DrowMode = pos.i_DrowMode;
this.i_stX = pos.i_stX;
this.i_stY = pos.i_stY;
this.i_enX = pos.i_enX;
this.i_enY = pos.i_enY;
}
public int i_DrowMode;
public int i_stX,i_stY;
public int i_enX,i_enY;
}
data pos=new data();
System.Collections.ArrayList ar = new System.Collections.ArrayList();
private void _MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
pos.i_stX = e.X;
pos.i_stY = e.Y;
}
private void _MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.Button ==System.Windows.Forms.MouseButtons.Left )
{
pos.i_DrowMode =Line;
ar.Add(new data(pos));
this.Refresh();
}
else if(e.Button ==System.Windows.Forms.MouseButtons.Middle)
{
pos.i_DrowMode =Pai;
ar.Add(new data(pos));
this.Refresh();
}
}
private void _MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.Button ==System.Windows.Forms.MouseButtons.Left )
{
pos.i_DrowMode =Line;
pos.i_enX = e.X;
pos.i_enY = e.Y;
}
else if(e.Button ==System.Windows.Forms.MouseButtons.Right)
{
pos.i_DrowMode =Line;
pos.i_enX = e.X;
pos.i_enY = e.Y;
ar.Add(new data(pos));
pos.i_stX = e.X;
pos.i_stY = e.Y;
}
else if(e.Button ==System.Windows.Forms.MouseButtons.Middle)
{
pos.i_DrowMode =Pai;
pos.i_enX = e.X;
pos.i_enY = e.Y;
}
this.Refresh();
}
private void _Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
if(pos.i_DrowMode ==Line){
System.Drawing.Pen pen=new System.Drawing.Pen(System.Drawing.Color.Red );
System.Drawing.Point pt1=new System.Drawing.Point(pos.i_stX,pos.i_stY);
System.Drawing.Point pt2=new System.Drawing.Point(pos.i_enX,pos.i_enY);
e.Graphics.DrawLine(pen,pt1,pt2);
}
else if(pos.i_DrowMode ==Pai)
{
System.Drawing.Pen pen=new System.Drawing.Pen(System.Drawing.Color.Red );
System.Drawing.Rectangle rect=new System.Drawing.Rectangle();
rect.X=pos.i_stX;
rect.Y=pos.i_stY;
rect.Height =pos.i_enY-pos.i_stY;
rect.Width =pos.i_enX-pos.i_stX;
e.Graphics.DrawEllipse(pen,rect);
}
for(int i=0;i<ar.Count;i++){
data d_pos=(data)ar[i];
if(d_pos.i_DrowMode ==Line)
{
System.Drawing.Pen pen=new System.Drawing.Pen(System.Drawing.Color.Black );
System.Drawing.Point pt1=new System.Drawing.Point(d_pos.i_stX,d_pos.i_stY);
System.Drawing.Point pt2=new System.Drawing.Point(d_pos.i_enX,d_pos.i_enY);
e.Graphics.DrawLine(pen,pt1,pt2);
}
else if(d_pos.i_DrowMode ==Pai)
{
System.Drawing.Pen pen=new System.Drawing.Pen(System.Drawing.Color.Black );
System.Drawing.Rectangle rect=new System.Drawing.Rectangle();
rect.X=d_pos.i_stX;
rect.Y=d_pos.i_stY;
rect.Height =d_pos.i_enY-d_pos.i_stY;
rect.Width =d_pos.i_enX-d_pos.i_stX;
e.Graphics.DrawEllipse(pen,rect);
}
}
System.Diagnostics.Trace.WriteLine(ar.Count);
}
public void SaveFile(System.String filename)
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
System.IO.FileStream fs = new System.IO.FileStream(filename,System.IO.FileMode.Create);
bf.Serialize(fs, ar);
fs.Close();
}
public void LoadFile(System.String filename)
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
System.IO.FileStream fs = new System.IO.FileStream(filename,System.IO.FileMode.Open);
ar=(System.Collections.ArrayList)bf.Deserialize(fs);
fs.Close();
this.Refresh();
}
}
▲トップページ