主题:[原创]杨晖三角——窗体字符位置和大小控制
VERSION 5.00
Begin VB.Form Form1
AutoRedraw = -1 'True
Caption = "Form1"
ClientHeight = 3090
ClientLeft = 60
ClientTop = 450
ClientWidth = 4680
LinkTopic = "Form1"
ScaleHeight = 3090
ScaleWidth = 4680
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton Command1
Caption = "开始"
Height = 375
Left = 240
TabIndex = 0
Top = 120
Width = 1095
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Const s$ = "杨辉三角" '标题
Private Sub Command1_Click()
ReDim yh(8, 8) As Long
Dim a As Byte '阶数
Dim k As Integer '列宽
Dim i As Integer
Dim j As Integer
Cls
Do
a = Val(InputBox$("请输入你需要的阶数(3~27)", s$, 8))
If a > 27 Or a < 3 Then MsgBox "杨辉三角的阶数太大或非法!", , s$
Loop Until a < 28 And a > 2
ReDim yh(a, a)
Me.Caption = s$
FontSize = 14.25 - 0.01 * a * a
y0 = Command1.Height + Command1.Top + 200
Height = y0 + 500 + (a + 2) * FontSize * 24
Width = Screen.Width * a / 23
Top = (Screen.Height - Me.Height) / 2
Left = (Screen.Width - Me.Width) / 2
yh(1, 1) = 1
yh(2, 1) = 1
yh(2, 2) = 1
For i = 3 To a
yh(i, 1) = 1
For j = 2 To i - 1
yh(i, j) = yh(i - 1, j - 1) + yh(i - 1, j)
Next j
yh(i, i) = 1
Next i
CurrentX = (Width - TextWidth(s$)) / 2
CurrentY = y0
Print s$
Print
k = Width / a
For i = 1 To a
For j = 1 To i
CurrentX = (j - 1 + (a - i) / 2) * k + (k - TextWidth(Str(yh(i, j)))) / 2
Print yh(i, j);
Next j
Print
Next i
End Sub
Begin VB.Form Form1
AutoRedraw = -1 'True
Caption = "Form1"
ClientHeight = 3090
ClientLeft = 60
ClientTop = 450
ClientWidth = 4680
LinkTopic = "Form1"
ScaleHeight = 3090
ScaleWidth = 4680
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton Command1
Caption = "开始"
Height = 375
Left = 240
TabIndex = 0
Top = 120
Width = 1095
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Const s$ = "杨辉三角" '标题
Private Sub Command1_Click()
ReDim yh(8, 8) As Long
Dim a As Byte '阶数
Dim k As Integer '列宽
Dim i As Integer
Dim j As Integer
Cls
Do
a = Val(InputBox$("请输入你需要的阶数(3~27)", s$, 8))
If a > 27 Or a < 3 Then MsgBox "杨辉三角的阶数太大或非法!", , s$
Loop Until a < 28 And a > 2
ReDim yh(a, a)
Me.Caption = s$
FontSize = 14.25 - 0.01 * a * a
y0 = Command1.Height + Command1.Top + 200
Height = y0 + 500 + (a + 2) * FontSize * 24
Width = Screen.Width * a / 23
Top = (Screen.Height - Me.Height) / 2
Left = (Screen.Width - Me.Width) / 2
yh(1, 1) = 1
yh(2, 1) = 1
yh(2, 2) = 1
For i = 3 To a
yh(i, 1) = 1
For j = 2 To i - 1
yh(i, j) = yh(i - 1, j - 1) + yh(i - 1, j)
Next j
yh(i, i) = 1
Next i
CurrentX = (Width - TextWidth(s$)) / 2
CurrentY = y0
Print s$
k = Width / a
For i = 1 To a
For j = 1 To i
CurrentX = (j - 1 + (a - i) / 2) * k + (k - TextWidth(Str(yh(i, j)))) / 2
Print yh(i, j);
Next j
Next i
End Sub