南昌航空大学科技学院学士学位论文
{
get { return penColor; } set { penColor = value; } }
protected int penWidth = 2; ///
get { return penWidth; } set { penWidth = value; } }
protected int id; public int ID {
get { return id; } set { id = value; } }
然后再添加一个名为DrawMyRectangle的类文件,表示要绘制的矩形对象,让该类从DrawObject继承,并重写DrawObject类的Draw方法。主要代码如下;
class DrawMyRectangle : TrackRectangle {
public DrawMyRectangle() { }
public DrawMyRectangle(int x, int y, int width, int height, Color penColor, int id) {
this.objRectangle = new Rectangle(x, y, width, height); this.penColor = penColor; this.id = id; }
public override void Draw(Graphics g) {
Pen pen = new Pen(penColor);
g.DrawRectangle(pen, objRectangle); pen.Dispose(); }
} }
16
南昌航空大学科技学院学士学位论文
(2)在解决方案管理器中,添加一个名为CC.cs的类,提供系统公共的属性和方法。CC类中提供了为图形图像对象分配ID的方法。具体代码请看附录的源代码; (3)分别添加名为ToolObject.cs和ToolRectangle.cs的类文件,其中,ToolObject表示封装鼠标事件的操作基类,ToolRectangle实现绘制矩形是的鼠标操作。ToolObject类的主要代码如下; class ToolObject {
protected bool isNewObjectAdded = false;
public virtual void OnMouseDown(Palette palette, MouseEventArgs e) {
isNewObjectAdded = false; CC.SetNewID(); }
public virtual void OnMouseMove(Palette palette, MouseEventArgs e) {
if (isNewObjectAdded == false) {
return; }
Point point = new Point(e.X, e.Y);
int index = CC.myService.FindObjectIndex(CC.ID); if (e.Button == MouseButtons.Left) {
DrawObject w = palette.graphics[index]; w.MoveHandleTo(point, 5); }
palette.Refresh(); }
public virtual void OnMouseUp(Palette palette, MouseEventArgs e) {
palette.Capture = false; palette.Refresh();
isNewObjectAdded = false; }
///
/// 添加新的图形对象 ///
protected void AddNewObject(Palette palette, DrawObject w) {
palette.graphics.UnselectAll(); w.Selected = true;
palette.graphics.Add(w); palette.Capture = true; palette.Refresh();
17
南昌航空大学科技学院学士学位论文
}
}
让ToolRectangle类从ToolObject继承,并重写基类的OnMouseDown和OnMouseMove事件。主要代码如下; class ToolRectangle : ToolObject {
public ToolRectangle() { }
public override void OnMouseDown(Palette palette, MouseEventArgs e) {
base.OnMouseDown(palette, e);
DrawMyRectangle w = new DrawMyRectangle(e.X, e.Y, 15, 15, Color.Red, CC.ID);
AddNewObject(palette, w); isNewObjectAdded = true; }
public override void OnMouseUp(Palette palette, MouseEventArgs e) {
if (isNewObjectAdded == false) {
return; }
base.OnMouseUp(palette, e);
if (CC.userState != UserState.SingleUser) {
int index = CC.myService.FindObjectIndex(CC.ID); DrawMyRectangle w =
(DrawMyRectangle)palette.graphics[index];
//左上角x坐标,左上角y坐标,宽,高,颜色,id
CC.me.SendToServer(string.Format(\\,
w.ObjRectangle.X, w.ObjRectangle.Y,
w.ObjRectangle.Width, w.ObjRectangle.Height, w.PenColor.ToArgb(), CC.ID)); palette.graphics.Remove(CC.ID); } } } }
(4)在MultiDraw2.cs中定义一个ToolType枚举,表示绘制图形图像的类型;再定义一个ToolObject类型的activeTool对象,表示当前的活动对象;然后定义
18
南昌航空大学科技学院学士学位论文
一个ToolObject类型的tools数组,表示所有鼠标操作类。主要代码如下; public enum ToolType {
Pointer, Rectangle, Text, Curve, Image, Line };
private ToolType activeTool;
在主程序窗体MultiDraw2的构造函数中,进行初始化操作。 CC.panel=this.panell;
tools[(int)ToolType.Rectangle]=newToolRectangle();
(5)双击主程序窗体上的矩形按钮,在其Click事件中添加代码; activeTool=ToolType.Rectangle;
(6)单击主窗体上的Panel控件,分别找到其MouseDown,MouseMove和MouseUp事件及Paint事件,在对应的事件中添加下面的代码;
private void Palette_MouseDown(object sender, MouseEventArgs e) {
this.Capture = false;
if (e.Button == MouseButtons.Left) {
tools[(int)activeTool].OnMouseDown(this, e); }
else if (e.Button == MouseButtons.Right) {
ActiveTool = Palette.ToolType.Pointer; }
}
private void Palette_MouseMove(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left || e.Button == MouseButtons.None) {
tools[(int)activeTool].OnMouseMove(this, e); } else {
this.Cursor = Cursors.Default; } }
19
南昌航空大学科技学院学士学位论文
private void Palette_MouseUp(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
tools[(int)activeTool].OnMouseUp(this, e); }
}
(7)运行程序,单击矩形按钮,在Panel内绘制多个任意大小的矩形,运行效果如图3.3所示
图3.3 任意大小的矩形绘制
3.2.2曲线的绘制
(1)在解决方案资源管理器中,添加一个名为DrawCurve的类,使其从DrawObjiect类中派生,添加相应的代码。在DrawCurve.cs类中添加List
20