• C#上位机系列(5)—示波器二基础代码+线条绘制


    本文是讲解C#.net平台的Winform框架下的第四个内容,手把手介绍上位机项目的创建方式以及一些写软件时常用的功能,讲解从零开始的每一个步骤。

    接上一节的介绍,本次内容为上位机项目中示波功能代码的基本函数和画图功能

    目的是通过几行代码实现简单线条的绘制

    所有代码附后

    1.GDI绘图

    GDI是.NET Framework中提供二维图形,图像处理等功能。

    GDI绘图的核心是Graphics对象,所有的绘图函数功能包括其中。

    需要引用System.Drawing;

    下面根据项目所需依次解释一些常用函数功能。

    2.示波器界面参数

    为了使窗口可以灵活改变,首先确定如下参数。

    定义绘图原点startPoint(这里将上文讲到的计算机界面坐标转换成传统笛卡尔坐标原点,后续确定坐标点采用笛卡尔坐标),整个图形界面的原点。

    定义坐标原点originPoint,即示波器框的原点。该参数设定为距离绘图原点相对的距离。

    定义示波器的X,Y轴长度Slength_X,Slength_Y,控制示波器框的大小。

    创建一个画笔TablePen用来画线条。

    定义坐标转换函数Pointconvert()

    画图函数Form_Paint()

    首先在设计函数里添加如下代码

     回到FormScope.cs定义函数

    定义初始化函数ScopeInit()

     效果如下:

     通过修改参数,可以方便的移动示波器边框的位置。

    本节代码附下,分为函数窗口FormScope.cs和设计窗口FormScope.Designer.cs文件

    FormScope.cs

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Threading.Tasks;
    9. using System.Windows.Forms;
    10. namespace WinForms_Start1
    11. {
    12. public partial class FormScope : Form
    13. {
    14. //示波器参数
    15. PointF startPoint; //绘图原点
    16. PointF originPoint; //示波器框的原点
    17. float Slength_X, Slength_Y;
    18. private Pen TablePen = new Pen(Color.FromArgb(0x3c, 0x3c, 0x3c));//线条画笔,灰色
    19. public FormScope()
    20. {
    21. InitializeComponent();
    22. ScopeInit();
    23. }
    24. /// <summary>
    25. /// 示波器初始化函数
    26. /// </summary>
    27. public void ScopeInit()
    28. {
    29. startPoint.X = 30;
    30. startPoint.Y = 500;
    31. originPoint = ConvertPoint(startPoint, 10, 50);//示波器原点坐标
    32. Slength_X = 600;
    33. Slength_Y = 400;
    34. Invalidate();//刷新显示
    35. }
    36. //画图函数
    37. private void Form_Paint(object sender, PaintEventArgs e)//
    38. {
    39. System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath();
    40. e.Graphics.FillRectangle(Brushes.Black, e.Graphics.ClipBounds); //默认黑色
    41. //画示波器框线 (颜色,起点坐标,终点坐标)
    42. //横轴
    43. e.Graphics.DrawLine(TablePen, originPoint, ConvertPoint(originPoint, Slength_X, 0));
    44. e.Graphics.DrawLine(TablePen, ConvertPoint(originPoint,0,Slength_Y), ConvertPoint(originPoint, Slength_X, Slength_Y));
    45. //纵轴
    46. e.Graphics.DrawLine(TablePen, originPoint, ConvertPoint(originPoint, 0, Slength_Y));
    47. e.Graphics.DrawLine(TablePen, ConvertPoint(originPoint, Slength_X, 0), ConvertPoint(originPoint, Slength_X, Slength_Y));
    48. }
    49. /// <summary>
    50. /// 坐标转换函数
    51. /// </summary>
    52. /// <param name="point"></param>
    53. /// <param name="x"></param>
    54. /// <param name="y"></param>
    55. /// <returns>新坐标值</returns>
    56. ///
    57. PointF ConvertPoint(PointF point, float x, float y)
    58. {
    59. PointF p = point;
    60. p.X += x;
    61. p.Y -= y;
    62. return p;
    63. }
    64. }
    65. }

    FormScope.Designer.cs

    1. namespace WinForms_Start1
    2. {
    3. partial class FormScope
    4. {
    5. /// <summary>
    6. /// Required designer variable.
    7. /// </summary>
    8. private System.ComponentModel.IContainer components = null;
    9. /// <summary>
    10. /// Clean up any resources being used.
    11. /// </summary>
    12. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    13. protected override void Dispose(bool disposing)
    14. {
    15. if (disposing && (components != null))
    16. {
    17. components.Dispose();
    18. }
    19. base.Dispose(disposing);
    20. }
    21. #region Windows Form Designer generated code
    22. /// <summary>
    23. /// Required method for Designer support - do not modify
    24. /// the contents of this method with the code editor.
    25. /// </summary>
    26. private void InitializeComponent()
    27. {
    28. this.SuspendLayout();
    29. //
    30. // FormScope
    31. //
    32. this.AutoScaleDimensions = new System.Drawing.SizeF(12F, 24F);
    33. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    34. this.ClientSize = new System.Drawing.Size(1774, 1129);
    35. this.Name = "FormScope";
    36. this.Text = "示波器";
    37. this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form_Paint);
    38. this.ResumeLayout(false);
    39. }
    40. #endregion
    41. }
    42. }

  • 相关阅读:
    线程锁(ReentrantLock、synchronized)为何不能用作分布式锁
    CLIP论文解读
    leetcode 1382. 将二叉搜索树变平衡
    MySQL 中的锁类型有哪些
    系统架构师2022年案例分析考前冲刺
    IIFE立即执行函数表达式使用
    市场人员被 CEO、销售要求去证明“数量指标”作用的现象越来越常见
    龙芯loongarch64麒麟服务器配置yum源
    SpringMvc--文件上传下载
    001SQL语句分析
  • 原文地址:https://blog.csdn.net/qq_49552487/article/details/128067788