南昌航空大学科技学院学士学位论文
}
在这段代码中,绘制文字前,首先创建一个名为matrix的Matrix类型的对象,然后以文字的起点starPoint为中心旋转angle角度即可绘制任意方向的文字。而绘制的文字的大小则是通过控制字体的高度来实现的。 (2)在解决方案资源管理器中,添加一个名为ToolText.cs的类,使其从ToolObject类派生,主要代码如下;
class ToolText : ToolObject {
public override void OnMouseDown(Palette palette, MouseEventArgs e) {
base.OnMouseDown(palette, e);
DrawMyText w = new DrawMyText(e.X, e.Y, CC.textInfo.text, CC.textInfo.color, CC.ID);
AddNewObject(palette, w); isNewObjectAdded = true; }
public override 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, 2); }
palette.Refresh(); }
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); DrawMyText w = (DrawMyText)palette.graphics[index];
26
南昌航空大学科技学院学士学位论文
//x1,y1,x2,y2,旋转角度,文字内容,颜色,文字高,id
CC.me.SendToServer(string.Format(\{7},{8}\,
w.StartPoint.X, w.StartPoint.Y, w.EndPoint.X,
w.EndPoint.Y, w.Angle, w.Text, w.PenColor.ToArgb(), w.FontHeight, w.ID)); palette.graphics.Remove(CC.ID); }
在OnMouseDown事件中,将鼠标所在的点作为起点,同时创建DrawText对象。 在OnMouseMove事件中,捕获鼠标所在的点,当鼠标当前位置与起始点之间的距离能够显示文字时,将该点作为文字的终点,同时计算文字的角度。
(3)在解决方案管理器中添加一个名为TextDialog.cs的新窗体,提供文本输入和颜色的选择的功能,界面如图3.6所示;
图3.6 TextDialog的设计界面
在TextDialog.cs的代码中添加两个属性MyText和MyColor.其中,MyText属性表示文本框内的输入的内容,MyColor属性表示文本框内容的字体颜色。具体代码如下;
public partial class TextDialog : Form {
private string text; public string MyText {
get { return text; } set { text = value; } }
private Color _color; public Color MyColor {
get { return _color; } set { _color = value; } }
public TextDialog() {
InitializeComponent();
27
南昌航空大学科技学院学士学位论文
this.FormBorderStyle = FormBorderStyle.FixedDialog; this.AcceptButton = buttonOK; this.CancelButton = buttonCancel; this.ControlBox = false; this.ShowInTaskbar = false; }
private void TextForm_Load(object sender, EventArgs e) {
_color = Color.Black;
textBox1.Text = \南昌航空大学科技学院\; }
private void buttonFont_Click(object sender, EventArgs e) {
ColorDialog c = new ColorDialog();
if (c.ShowDialog() == DialogResult.OK) {
_color = c.Color;
textBox1.ForeColor = _color; } }
private void buttonOK_Click(object sender, EventArgs e) {
text = textBox1.Text; } } }
(4)在主程序窗体MultiDraw2的构造函数中,添加代码: Tools[(int)ToolType.Text]=new ToolText();
(5)在主窗体上,双击文字按钮,在其Click事件中添加如下代码;
TextDialog td = new TextDialog();
if (td.ShowDialog() == DialogResult.OK) {
CC.textInfo.text = td.MyText; CC.textInfo.color = td.MyColor;
CC.palette.ActiveTool = Palette.ToolType.Text; }
(6)运行程序,单击[文字]按钮,再弹出的窗口中输入绘制的文字,然后在右边
28
南昌航空大学科技学院学士学位论文
的框中用鼠标拖动绘制一些文字,运行效果如图3.7所示
a. 选择文字的颜色
b. 绘制任意大小和方向的文字
图3.7运行效果
29
南昌航空大学科技学院学士学位论文
3.2.5绘制任意大小的图像
(1)在解决方案资源管理器中,添加一个名为DrawImage.cs的类,使其从DrawRectangle类派生,主要代码如下; class DrawMyImage : TrackRectangle {
private Bitmap originalBitmap;
public override DrawObject Clone() {
DrawMyImage w = new DrawMyImage(); w.objRectangle = this.objRectangle;
w.originalBitmap = (Bitmap)this.originalBitmap.Clone(); AddOtherFields(w); return w; }
public DrawMyImage() { }
public DrawMyImage(int x, int y, int width, int height, Bitmap bitmap, int id) {
this.objRectangle = new Rectangle(x, y, width, height); this.originalBitmap = new Bitmap(bitmap); this.id = id; }
public override void Draw(Graphics g) {
if (originalBitmap == null) {
Pen p = new Pen(Color.Black, -1f); g.DrawRectangle(p, objRectangle); } else {
//将bitmap设置为对其默认的透明色透明 originalBitmap.MakeTransparent();
g.DrawImage(originalBitmap, objRectangle, 0, 0, originalBitmap.Width, originalBitmap.Height, GraphicsUnit.Pixel); } }
30