1. 程式人生 > >在控件內實現簡單畫筆功能

在控件內實現簡單畫筆功能

box use from uic eve args class 窗體 down

/// <summary>
/// 實現窗體內簡單畫筆功能
/// </summary>
public class DrawClass
{
private Color __Color = new Color();
private Point __Start = new Point();
private Point __End = new Point();
private Form __frmMain = new Form();
private PictureBox __picMain = new PictureBox();
private Graphics __g = null;
private Pen __p = null;

public Color Color
{
get { return this.__Color; }
set { this.__Color = value; }
}
public Point Start
{
get { return this.__Start; }
set { this.__Start = value; }
}
public Point End
{
get { return this.__End; }
set { this.__End = value; }
}


public DrawClass(Form frmMain)
: this(frmMain, Color.Black)
{ }
public DrawClass(PictureBox picMian)
: this(picMian, Color.Red)
{ }

public DrawClass(Form frmMain, Color c)
{
this.__frmMain = frmMain;
this.__g = Graphics.FromHwnd(frmMain.Handle);
this.__Color = c;
this.__p = new Pen(this.__Color,2);
this.__frmMain.MouseDown += new MouseEventHandler(__frmMain_MouseDown);
this.__frmMain.MouseMove += new MouseEventHandler(__frmMain_MouseMove);
this.__frmMain.MouseUp += new MouseEventHandler(__frmMain_MouseUp);
}

public DrawClass(PictureBox frmMain, Color c)
{
this.__picMain = frmMain;
this.__g = Graphics.FromHwnd(frmMain.Handle);
this.__Color = c;
this.__p = new Pen(this.__Color,2);
this.__picMain.MouseDown += new MouseEventHandler(__frmMain_MouseDown);
this.__picMain.MouseMove += new MouseEventHandler(__frmMain_MouseMove);
this.__picMain.MouseUp += new MouseEventHandler(__frmMain_MouseUp);
}

private void __frmMain_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
__End.X = e.X;
__End.Y = e.Y;
__g.DrawLine(this.__p, __Start, __End);
__Start = __End;
}
}

private void __frmMain_MouseUp(object sender, MouseEventArgs e)
{

}

private void __frmMain_MouseDown(object sender, MouseEventArgs e)
{
this.__Start.X = e.X;
this.__Start.Y = e.Y;
}


}

DrawClass draw = new ZhuoHuiClass.DrawClass(this.pic); //調用畫筆功能

在控件內實現簡單畫筆功能