分享

屏幕笔(C#版)

 羊玉wngbx 2019-12-03

所谓屏幕笔就是可以直接在屏幕上绘图的工具,下面用C#给出一个简单的屏幕笔实现,虽然很简单,但是其中对多态(polymorphism)和工厂模式(factory pattern)的应用还是值得玩味的。


上图是在Visual Studio 2010中创建一个Windows应用程序,利用设计器放置了一个面板以及选择图形的单选按钮和其他相关的功能按钮。其中窗口设置为无边框的窗口,面板的Anchor属性设置为相对于窗口右上角固定。下面放置了一个颜色对话框和一个保存文件对话框分别用来为画笔选择颜色以及选择保存的文件。

由于绘制的图形有很多种,而且这些图形都有着共同的属性(坐标、颜色、粗细等)以及绘图的方法,因此可以定义一个继承结构,先设计抽象的图形,然后派生各种不同的图形,并对绘图的方法进行多态实现,代码如下所示:

  1. using System;
  2. using System.Drawing;
  3. namespace ScreenPen {
  4. /// <summary>
  5. /// 抽象图形类
  6. /// </summary>
  7. public abstract class Shape {
  8. /// <summary>
  9. /// 起点的横坐标
  10. /// </summary>
  11. public int StartX { set; get; }
  12. /// <summary>
  13. /// 起点的纵坐标
  14. /// </summary>
  15. public int StartY { set; get; }
  16. /// <summary>
  17. /// 终点的横坐标
  18. /// </summary>
  19. public int EndX { set; get; }
  20. /// <summary>
  21. /// 终点的纵坐标
  22. /// </summary>
  23. public int EndY { set; get; }
  24. /// <summary>
  25. /// 图形的颜色
  26. /// </summary>
  27. public Color Color { set; get; }
  28. /// <summary>
  29. /// 图形的粗细
  30. /// </summary>
  31. public float Thickness { set; get; }
  32. /// <summary>
  33. /// 绘制图形
  34. /// </summary>
  35. /// <param name="g">画笔</param>
  36. public abstract void Draw(Graphics g);
  37. }
  38. }

  1. using System;
  2. using System.Drawing;
  3. namespace ScreenPen {
  4. /// <summary>
  5. /// 线条类
  6. /// </summary>
  7. public class Line : Shape {
  8. public override void Draw(Graphics g) {
  9. Pen pen = new Pen(Color, Thickness);
  10. g.DrawLine(pen, StartX, StartY, EndX, EndY);
  11. }
  12. }
  13. }

  1. using System;
  2. using System.Drawing;
  3. namespace ScreenPen {
  4. /// <summary>
  5. /// 椭圆类
  6. /// </summary>
  7. public class Oval : Shape {
  8. public override void Draw(System.Drawing.Graphics g) {
  9. Pen pen = new Pen(Color, Thickness);
  10. int x = StartX < EndX ? StartX : EndX;
  11. int y = StartY < EndY ? StartY : EndY;
  12. int width = Math.Abs(StartX - EndX);
  13. int height = Math.Abs(StartY - EndY);
  14. g.DrawEllipse(pen, x, y, width, height);
  15. }
  16. }
  17. }

  1. using System;
  2. using System.Drawing;
  3. namespace ScreenPen {
  4. /// <summary>
  5. /// 矩形类
  6. /// </summary>
  7. public class Rectangle : Shape {
  8. public override void Draw(Graphics g) {
  9. Pen pen = new Pen(Color, Thickness);
  10. int x = StartX < EndX ? StartX : EndX;
  11. int y = StartY < EndY ? StartY : EndY;
  12. int width = Math.Abs(StartX - EndX);
  13. int height = Math.Abs(StartY - EndY);
  14. g.DrawRectangle(pen, x, y, width, height);
  15. }
  16. }
  17. }

接下来再设计一个简单工厂来创建这些图形对象,工厂模式的意图是将创建产品对象的过程封装起来,客户端可以跟具体的产品解耦合。
  1. using System;
  2. namespace ScreenPen {
  3. /// <summary>
  4. /// 图形工厂(静态工厂模式)
  5. /// </summary>
  6. public static class ShapeFactory {
  7. /// <summary>
  8. /// 创建图形对象
  9. /// </summary>
  10. /// <param name="type">图形的类型</param>
  11. /// <returns>图形对象</returns>
  12. public static Shape Create(string type) {
  13. Shape sh = null;
  14. switch (type) {
  15. case "线条": sh = new Line(); break;
  16. case "矩形": sh = new Rectangle(); break;
  17. case "椭圆": sh = new Oval(); break;
  18. }
  19. return sh;
  20. }
  21. }
  22. }

接下来是主窗口,通过处理鼠标事件来绘图
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. using System.Drawing.Imaging;
  6. namespace ScreenPen {
  7. public partial class FrmScreenPen : Form {
  8. private Shape currentShape = null; // 当前绘制的图形
  9. private string currentType = "线条"; // 当前选中的图形类型
  10. private Color currentColor = Color.Black; // 当前选中的绘图颜色
  11. private float currentThick = 1f; // 当前选中的图形粗细
  12. private bool isMouseLeftButtonDown = false; // 鼠标左键是否被按下
  13. private IList<Shape> list = new List<Shape>();
  14. public FrmScreenPen() {
  15. InitializeComponent();
  16. // 开启双缓冲
  17. this.SetStyle(ControlStyles.UserPaint, true);
  18. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  19. this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
  20. // 为单选按钮绑定事件
  21. foreach (Control c in panel1.Controls) {
  22. if (c is RadioButton) {
  23. (c as RadioButton).CheckedChanged += new EventHandler(ChangeShapeType);
  24. }
  25. }
  26. this.FormBorderStyle = FormBorderStyle.None; // 窗口无边框
  27. this.WindowState = FormWindowState.Maximized; // 窗口最大化
  28. this.TopMost = true; // 窗口置顶
  29. this.Opacity = 0.75; // 窗口不透明度
  30. }
  31. // 点击单选按钮
  32. private void ChangeShapeType(object sender, EventArgs e) {
  33. currentType = (sender as RadioButton).Text; // 记录选中图形的类型
  34. }
  35. // 鼠标按下
  36. private void FrmMain_MouseDown(object sender, MouseEventArgs e) {
  37. if (e.Button == MouseButtons.Left) {
  38. isMouseLeftButtonDown = true;
  39. currentShape = ShapeFactory.Create(currentType);
  40. if (currentShape != null) {
  41. currentShape.Color = currentColor;
  42. currentShape.Thickness = currentThick;
  43. currentShape.StartX = currentShape.EndX = e.X;
  44. currentShape.StartY = currentShape.EndY = e.Y;
  45. }
  46. }
  47. }
  48. // 鼠标移动(模拟鼠标拖拽[按下鼠标左键移动鼠标])
  49. private void FrmMain_MouseMove(object sender, MouseEventArgs e) {
  50. if (isMouseLeftButtonDown && currentShape != null) {
  51. currentShape.EndX = e.X;
  52. currentShape.EndY = e.Y;
  53. this.Refresh();
  54. }
  55. }
  56. // 鼠标松开
  57. private void FrmMain_MouseUp(object sender, MouseEventArgs e) {
  58. if (isMouseLeftButtonDown && currentShape != null) {
  59. isMouseLeftButtonDown = false;
  60. list.Add(currentShape);
  61. currentShape = null;
  62. }
  63. }
  64. // 重绘窗体
  65. private void FrmMain_Paint(object sender, PaintEventArgs e) {
  66. Graphics g = e.Graphics;
  67. foreach (Shape shape in list) {
  68. shape.Draw(g);
  69. }
  70. if (currentShape != null) {
  71. currentShape.Draw(g);
  72. }
  73. }
  74. // 退出程序
  75. private void btnExit_Click(object sender, EventArgs e) {
  76. Application.Exit();
  77. }
  78. // 打开调色板
  79. private void btnColor_Click(object sender, EventArgs e) {
  80. DialogResult result = colorDialog1.ShowDialog();
  81. if (result == DialogResult.OK) {
  82. currentColor = colorDialog1.Color;
  83. }
  84. }
  85. // 保存图形
  86. private void btnSave_Click(object sender, EventArgs e) {
  87. DialogResult result = saveFileDialog1.ShowDialog();
  88. if (result == DialogResult.OK) {
  89. string filename = saveFileDialog1.FileName;
  90. using (Image image = new Bitmap(this.Width, this.Height)) {
  91. Graphics g = Graphics.FromImage(image);
  92. foreach (Shape shape in list) {
  93. shape.Draw(g);
  94. }
  95. image.Save(filename, ImageFormat.Png);
  96. }
  97. }
  98. }
  99. // 清除窗体中的图形
  100. private void btnClear_Click(object sender, EventArgs e) {
  101. list.Clear();
  102. this.Refresh();
  103. }
  104. // 撤销上一次绘制的图形
  105. private void btnUndo_Click(object sender, EventArgs e) {
  106. if (list.Count > 0) {
  107. list.RemoveAt(list.Count - 1);
  108. this.Refresh();
  109. }
  110. }
  111. // 增加画笔的粗细
  112. private void btnAddThick_Click(object sender, EventArgs e) {
  113. if (currentThick < 10) {
  114. currentThick += 0.5f;
  115. }
  116. }
  117. // 减小画笔的粗细
  118. private void btnSubThick_Click(object sender, EventArgs e) {
  119. if (currentThick > 0.5) {
  120. currentThick -= 0.5f;
  121. }
  122. }
  123. }
  124. }

下面是Main方法,这个是IDE自动生成的代码。
  1. using System;
  2. using System.Windows.Forms;
  3. namespace ScreenPen {
  4. static class Program {
  5. /// <summary>
  6. /// 应用程序的主入口点。
  7. /// </summary>
  8. [STAThread]
  9. static void Main() {
  10. Application.EnableVisualStyles();
  11. Application.SetCompatibleTextRenderingDefault(false);
  12. Application.Run(new FrmScreenPen());
  13. }
  14. }
  15. }

运行一下,就可以看到下面的效果图:


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约