C#上位机系列(5)—示波器二基础代码+线条绘制
创始人
2024-03-13 09:21:11
0

本文是讲解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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace WinForms_Start1
{public partial class FormScope : Form{//示波器参数PointF startPoint; //绘图原点PointF originPoint; //示波器框的原点float Slength_X, Slength_Y;private Pen TablePen = new Pen(Color.FromArgb(0x3c, 0x3c, 0x3c));//线条画笔,灰色public FormScope(){InitializeComponent();ScopeInit();}/// /// 示波器初始化函数/// public void ScopeInit(){startPoint.X = 30;startPoint.Y = 500;originPoint = ConvertPoint(startPoint, 10, 50);//示波器原点坐标Slength_X = 600;Slength_Y = 400;Invalidate();//刷新显示   }//画图函数private void Form_Paint(object sender, PaintEventArgs e)//画  { System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath();e.Graphics.FillRectangle(Brushes.Black, e.Graphics.ClipBounds);  //默认黑色//画示波器框线  (颜色,起点坐标,终点坐标)//横轴e.Graphics.DrawLine(TablePen, originPoint, ConvertPoint(originPoint, Slength_X, 0));e.Graphics.DrawLine(TablePen, ConvertPoint(originPoint,0,Slength_Y), ConvertPoint(originPoint, Slength_X, Slength_Y));//纵轴e.Graphics.DrawLine(TablePen, originPoint, ConvertPoint(originPoint, 0, Slength_Y));e.Graphics.DrawLine(TablePen, ConvertPoint(originPoint, Slength_X, 0), ConvertPoint(originPoint, Slength_X, Slength_Y));}/// /// 坐标转换函数/// /// /// /// /// 新坐标值/// PointF ConvertPoint(PointF point, float x, float y){PointF p = point;p.X += x;p.Y -= y;return p;}}
}

FormScope.Designer.cs

namespace WinForms_Start1
{partial class FormScope{/// /// Required designer variable./// private System.ComponentModel.IContainer components = null;/// /// Clean up any resources being used./// /// true if managed resources should be disposed; otherwise, false.protected override void Dispose(bool disposing){if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);}#region Windows Form Designer generated code/// /// Required method for Designer support - do not modify/// the contents of this method with the code editor./// private void InitializeComponent(){this.SuspendLayout();// // FormScope// this.AutoScaleDimensions = new System.Drawing.SizeF(12F, 24F);this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;this.ClientSize = new System.Drawing.Size(1774, 1129);this.Name = "FormScope";this.Text = "示波器";this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form_Paint);this.ResumeLayout(false);}#endregion}
}

相关内容

热门资讯

汽车油箱结构是什么(汽车油箱结... 本篇文章极速百科给大家谈谈汽车油箱结构是什么,以及汽车油箱结构原理图解对应的知识点,希望对各位有所帮...
美国2年期国债收益率上涨15个... 原标题:美国2年期国债收益率上涨15个基点 美国2年期国债收益率上涨15个基...
嵌入式 ADC使用手册完整版 ... 嵌入式 ADC使用手册完整版 (188977万字)💜&#...
重大消息战皇大厅开挂是真的吗... 您好:战皇大厅这款游戏可以开挂,确实是有挂的,需要了解加客服微信【8435338】很多玩家在这款游戏...
盘点十款牵手跑胡子为什么一直... 您好:牵手跑胡子这款游戏可以开挂,确实是有挂的,需要了解加客服微信【8435338】很多玩家在这款游...
senator香烟多少一盒(s... 今天给各位分享senator香烟多少一盒的知识,其中也会对sevebstars香烟进行解释,如果能碰...
终于懂了新荣耀斗牛真的有挂吗... 您好:新荣耀斗牛这款游戏可以开挂,确实是有挂的,需要了解加客服微信8435338】很多玩家在这款游戏...
盘点十款明星麻将到底有没有挂... 您好:明星麻将这款游戏可以开挂,确实是有挂的,需要了解加客服微信【5848499】很多玩家在这款游戏...
总结文章“新道游棋牌有透视挂吗... 您好:新道游棋牌这款游戏可以开挂,确实是有挂的,需要了解加客服微信【7682267】很多玩家在这款游...
终于懂了手机麻将到底有没有挂... 您好:手机麻将这款游戏可以开挂,确实是有挂的,需要了解加客服微信【8435338】很多玩家在这款游戏...