1、轻轻松松学VB第一期自己动手做称手工具软件轻轻松松学VB,第一期:自己动手做称手工具软件大家有没有在电子制作的时候一个公式要反复用好几遍,参数改好几次,计算更是想吐,现成的软件又不好找,要不就是用着不顺手,其实,大家可以学一点VB知识,就完全可以打造自己的计算工具。我在假期闲的无聊就练练手,自己动手做了几个小工具软件,在此我就抛砖引玉了,把制作过程及代码贴上来,欢迎大家拍砖。首先是熟悉VB的编程环境,就是常用VB6.0.这个相关只是就先暂不补充了,感兴趣的看看书半个小时就能上手。下面是其中的几个界面:2011-10-3 14:25:46 上传下载附件 (51.93 KB) 2011-10-3
2、14:25:57 上传下载附件 (31.66 KB) 2011-10-3 14:26:10 上传下载附件 (40.88 KB) 在计算电阻分压这一块关键的是协调数据使用条件,比如用一些提示信息来提醒你哪些必须输入哪些可以输入,这里我用了几个if语句解决的,代码有点笨,见谅。Private Sub Command1_Click()Dim a, b, c, d, e As Singlea = Val(Text2.Text)b = Val(Text3.Text)c = Val(Text1.Text)d = Val(Text4.Text)If Text1.Text And Text2.Text And
3、 Text3.Text And Text4.Text ThenMsgBox 请保证输入的数据只有三个已知量!, vbYes, 注意ElseIf d c And c 0 ThenMsgBox 请设置输入电压高于输出电压, vbYes, 注意ElseIf c = d 0 Thena = 0Text3.Text = Text2.Text = 0ElseIf Text2.Text = Thene = b * (c - d) / dText2.Text = eElseIf Text3.Text = Thene = a * d / (c - d)Text3.Text = eElseIf Text1.Tex
4、t = Thene = d * (a + b) / bText1.Text = eElseIf Text4.Text = Thene = b * c / (a + b)Text4.Text = eEnd IfEnd IfEnd IfEnd IfEnd IfEnd IfEnd IfEnd SubPrivate Sub Command2_Click()Text1.Text = Text2.Text = Text3.Text = Text4.Text = End SubPrivate Sub Command3_Click()MsgBox 本工具由小猛为结婚的电感制作,版权没有,翻版不究,本计算为知三
5、求一形式,输入变量时请输入三个,再次计算请先清除或者保证一个变量为空,输入电压务必大于输出电压。数值为单精度,正数范围在1.401298E-45到3.402823E38之间,不填写数值默认为0, vbYes + 0, 工具说明End SubPrivate Sub Command4_Click()EndEnd Sub习惯不好,没加注释,大家忍着看吧,是不是感觉重复的语句很多,其实就这么简单,不用很牛B的句子,毕竟咱们是玩电的,不是专业程序员。下面说一下解方程的。这个一般的程序书上算法都有。话不多说,直接上代码:Private Sub Command1_Click()Form1.ShowForm2
6、.HideEnd SubPrivate Sub Command2_Click()Dim a As Single, b As Single, c As Single Dim D As Single, X1 As Single, X2 As Single Dim R As Single, P As Single a = Val(Text1.Text) 取数据a b = Val(Text2.Text) 取数据b c = Val(Text3.Text) 取数据c If a = 0 Then 当a=0 Text4.Text = 不是二次方程 Text5.Text = 不是二次方程 Else a0 D =
7、 b * b - 4 * a * c R = -b / (2 * a)If D = 0 Then D=0 Text4.Text = R Text5.Text = R ElseIf D 0 Then D0X1 = (-b + Sqr(D) / (2 * a) X2 = (-b - Sqr(D) / (2 * a) Text4.Text = X1 Text5.Text = X2 Else D0P = Sqr(-D) / (2 * a) Text4.Text = R & + & P & i Text5.Text = R & - & P & i End If End IfEnd SubPrivate
8、Sub Command3_Click()Text1.Text = Text2.Text = Text3.Text = Text4.Text = Text5.Text = End SubPrivate Sub Command4_Click()Dim a1, b1, c1, m1, n1, t1, x11, y11 As Singlea1 = Val(Text6.Text)b1 = Val(Text7.Text)c1 = Val(Text8.Text)m1 = Val(Text10.Text)n1 = Val(Text11.Text)t1 = Val(Text12.Text)If Text6.Te
9、xt And Text7.Text And Text8.Text And Text10.Text And Text11.Text And Text12.Text And Text6.Text Text10.Text And Text7.Text Text11.Text Theny11 = (m1 * c1 - a1 * t1) / (a1 * n1 - b1 * m1)x11 = (-c1 - b1 * y11) / a1Text9.Text = x11Text13.Text = y11ElseIf Text6.Text = Text10.Text Or Text7.Text = Text11
10、.Text ThenMsgBox 不为二元方程组!, vbYes, 注意ElseIf Text6.Text = Or Text7.Text = Or Text8.Text = And Text10.Text = Or Text11.Text = Or Text12.Text = ThenMsgBox 请保证输入数据的完整性!, vbYes, 注意End IfEnd IfEnd IfEnd SubPrivate Sub Command5_Click()Text6.Text = Text7.Text = Text8.Text = Text9.Text = Text10.Text = Text11.
11、Text = Text12.Text = Text13.Text = End Sub大家慢慢看吧下面就把波形绘制的贴出来,主要实现方法就是峰值叠加跟折线描图,大家都懂得,废话我就不多说了Option ExplicitConst Pi As Single = 3.14159265358979Dim a()As Single, b() As Single, c() As SingleDim YMax As Single, AZero As BooleanDim w As Integer, h, aa As IntegerSub Drawsin(a As Single, b As Single, c
12、 As Single) Dim iAs Integer, x As Single, y As Single, j As Single Dim oi As Single, oj As Single For i = 0 To w Step 1 x = i / w * 6 * Pi y = a * Sin(b * x + c) j = -h * (y - YMax) / (2 * YMax) If i = 0 Then Picture1.PSet (i, j), RGB(255, 0, 0) Else Picture1.Line (oi, oj)-(i, j), RGB(255, 0, 0) End
13、 If oi = i oj = j NextEnd SubSub Drawaaddsin() Dim iAs Integer, x As Single, y As Single, j As Single Dim oi As Single, oj As Single Dim kAs Integer For i = 0 To w Step 1 x = i / w * 6 * Pi y = 0 For k = 1 To UBound(a) y = a(k) * Sin(b(k) * x + c(k) + y Next j = -h * (y - YMax) / (2 * YMax) If i = 0
14、 Then Picture2.PSet (i, j), RGB(0, 0, 255) Else Picture2.Line (oi, oj)-(i, j), RGB(0, 0, 255) End If oi = i oj = j NextEnd SubSub DrawXY() With Picture1 Picture1.Line (0, h / 2)-(w, h / 2) Picture1.Line (0, 0)-(0, h) .CurrentX = 0 .CurrentY = h / 2 Picture1.Print 0 .CurrentX = w / 3 .CurrentY = h /
15、2 Picture1.Print 2 .CurrentX = 2 * w / 3 .CurrentY = h / 2 Picture1.Print 4 .CurrentX = w - 15 .CurrentY = h / 2 Picture1.Print 6 End WithWith Picture2 Picture2.Line (0, h / 2)-(w, h / 2) Picture2.Line (0, 0)-(0, h) .CurrentX = 0 .CurrentY = h / 2 Picture2.Print 0 .CurrentX = w / 3 .CurrentY = h / 2
16、 Picture2.Print 2 .CurrentX = 2 * w / 3 .CurrentY = h / 2 Picture2.Print 4 .CurrentX = w - 15 .CurrentY = h / 2 Picture2.Print 6 End WithEnd SubPrivate Sub Command1_Click() Dim i As Integer If Val(Text1.Text) = 0 Then AZero = True Else ReDim Preserve a(UBound(a) + 1) ReDim Preserve b(UBound(b) + 1)
17、ReDim Preserve c(UBound(c) + 1) a(UBound(a) = Val(Text1.Text) b(UBound(b) = Val(Text2.Text) c(UBound(c) = Val(Text3.Text) YMax = YMax + Abs(a(UBound(a) Picture1.Cls Picture2.Cls DrawXY For i = 1 To UBound(a) Drawsin a(i), b(i), c(i) Next Drawaaddsin End If Text5.Text = YMax aa = aa + 1 Text4.Text =
18、aa If AZero Then Picture1.Line (0, h / 2)-(w, h / 2), RGB(255, 0, 0) Picture2.Line (0, h / 2)-(w, h / 2), RGB(255, 0, 0) End SubPrivate Sub Command2_Click() Picture1.ClsPicture2.Cls Form_Load Text1.Text = Text2.Text = Text3.Text = Text5.Text = End SubPrivate Sub Command3_Click()Form1.ShowForm111.Hid
19、eEnd SubPrivate Sub Form_Load() ReDim a(0), b(0), c(0) YMax = 0 aa = 0 AZero = False With Picture1 .AutoRedraw = True .ScaleMode = vbPixels w = .ScaleWidth h = .ScaleHeight End WithWith Picture2 .AutoRedraw = True .ScaleMode = vbPixels w = .ScaleWidth h = .ScaleHeight End With DrawXY Text4.Text = 0End Sub
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1