在控件内实现简单画笔功能

Posted _小马哥

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在控件内实现简单画笔功能相关的知识,希望对你有一定的参考价值。

/// <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);                              //调用画笔功能

 

以上是关于在控件内实现简单画笔功能的主要内容,如果未能解决你的问题,请参考以下文章

将 GeometryDrawing 画笔绑定到自定义控件依赖属性

蓝桥杯选拔赛真题33Scratch画笔功能 少儿编程scratch蓝桥杯选拔赛真题讲解

为啥画笔窗格被锁定?

3D建模教程之Zbrush雕刻笔刷简单介绍

一个画板demo

canvas实现简单的画图工具中画笔效果