因为vb.net没有画图所需的line固件,而且Timer函数写法和之前不一样且不会报错,网上也没有相关案例,所以完成这个任务确实一开始有那么一点难度,不过我还是利用分着设置x1,y1,x2,y2值的方式,把问题解决了
一、基础设置
首先,打开vs2019,右键打开新建项,选择vb winform项目
然后在项目里新建一个form
在新建一个这样的form后,你可以给这个form取一些名字,不过记住英文要放在前面
比如说Form画图这种
然后从工具箱内,拖拽几个固件出来,并重新命名,那个虚线小框是图片,表示指针自动旋转时固定的那个点
温馨提示:如果你改变了工具箱固件design的那个选项,而且在此之前你不慎点击进了相关的代码中,你需要手动改变代码函数的名字
如果你在改变之前就点进了代码,你看到的仍然会是之前的名字
所以需要手动改变代码函数为:
二、画静态的图
静态的图包括两个圆,一个指针,还有一个坐标系
他们都通过第一个点击事件:Button准备进行事件的触发
我在这里未改变design的选项
代码如下:
'''一开始就进行坐标系的加载
Private Sub Form更完善的自动画圆_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim gr As Graphics = GroupBox1.CreateGraphics '''利用Graphics在picturebox1里创建画布(groupbox也可以)
Dim p As New Pen(Brushes.Red, 3)
Dim p1 As New Pen(Color.Blue, 3)
gr.DrawLine(p, New Point(0, 0), New Point(200, 0)) '''画x轴,原点(0,0)
gr.DrawLine(p, New Point(0, 0), New Point(0, 200)) '''画y轴,原点(0,0)
End Sub
'''画圆并显示图片
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim gr As Graphics = GroupBox1.CreateGraphics '''利用Graphics在groupbox1里创建画布(picturebox1也可以)
Dim p As New Pen(Brushes.Red, 3)
Dim p1 As New Pen(Color.Blue, 3)
gr.DrawEllipse(p, 50, 100, 100, 100)
gr.DrawEllipse(p1, 0, 0, 200, 200) '''横坐标、纵坐标、宽度和高度(宽度和高度相同即为正圆)
PictureBox1.Image = My.Resources.支座
gr.DrawLine(p1, x1, y1, x2, y2) '''x1,y1,x2,y2
x1 = 100
y1 = 150
x2 = 150
y2 = 150
End Sub
这个是触发准备按钮之后的结果(点击一次时):
点击两次准备按钮时:
三、让指针动起来
我们通过timer函数完成这个功能,开始按钮进行延时事件的触发,绘制动态图的原理是设置每次的路径函数,并每隔一段时间进行曲线路径的清除
代码如下:
Private Sub Button开始_Click(sender As Object, e As EventArgs) Handles Button开始.Click
Timer1.Enabled = True
Me.Timer1.Interval = 1500
Me.Timer1.Start()
End Sub
Dim k As Integer
Dim phy, xb, yb As Double
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim gr As Graphics = GroupBox1.CreateGraphics '''利用Graphics在picturebox1里创建画布(groupbox也可以)
Dim p1 As New Pen(Color.Blue, 3)
k = k + 1
phy = k * 6 * 3.1416 / 180 '''一秒走六度(求度数)
xb = 50 * Cos(phy)
yb = 50 * Sin(phy)
x2 = x1 + xb
y2 = y1 + yb
gr.DrawLine(p1, x1, y1, x2, y2)
Threading.Thread.Sleep(2500)
GroupBox1.CreateGraphics.Clear(Button开始.BackColor)
End Sub
之后,当你摁下开始按钮,指针就会转动起来了
四、总体代码
Imports System.Math
Public Class Form更完善的自动画圆
Dim x1, y1, x2, y2 As Integer
Private Sub Button结束_Click(sender As Object, e As EventArgs) Handles Button结束.Click
GroupBox1.CreateGraphics.Clear(Button开始.BackColor)
Me.Hide()
End Sub
Private Sub Button开始_Click(sender As Object, e As EventArgs) Handles Button开始.Click
Timer1.Enabled = True
Me.Timer1.Interval = 1500
Me.Timer1.Start()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim gr As Graphics = GroupBox1.CreateGraphics '''利用Graphics在groupbox1里创建画布(picturebox1也可以)
Dim p As New Pen(Brushes.Red, 3)
Dim p1 As New Pen(Color.Blue, 3)
gr.DrawEllipse(p, 50, 100, 100, 100)
gr.DrawEllipse(p1, 0, 0, 200, 200) '''横坐标、纵坐标、宽度和高度(宽度和高度相同即为正圆)
PictureBox1.Image = My.Resources.支座
gr.DrawLine(p1, x1, y1, x2, y2) '''x1,y1,x2,y2
x1 = 100
y1 = 150
x2 = 150
y2 = 150
End Sub
Dim k As Integer
Dim phy, xb, yb As Double
Private Sub Form更完善的自动画圆_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim gr As Graphics = GroupBox1.CreateGraphics '''利用Graphics在picturebox1里创建画布(groupbox也可以)
Dim p As New Pen(Brushes.Red, 3)
Dim p1 As New Pen(Color.Blue, 3)
gr.DrawLine(p, New Point(0, 0), New Point(200, 0)) '''画x轴,原点(0,0)
gr.DrawLine(p, New Point(0, 0), New Point(0, 200)) '''画y轴,原点(0,0)
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim gr As Graphics = GroupBox1.CreateGraphics '''利用Graphics在picturebox1里创建画布(groupbox也可以)
Dim p1 As New Pen(Color.Blue, 3)
k = k + 1
phy = k * 6 * 3.1416 / 180 '''一秒走六度(求度数)
xb = 50 * Cos(phy)
yb = 50 * Sin(phy)
x2 = x1 + xb
y2 = y1 + yb
gr.DrawLine(p1, x1, y1, x2, y2)
Threading.Thread.Sleep(2500)
GroupBox1.CreateGraphics.Clear(Button开始.BackColor)
End Sub
End Class