如何用VB程序在屏幕范围内随机画出边长10-15像素的正方形?
2个回答
展开全部
'下面是我写的一个能随机生成任意长度、颜色、位置正方形的代码
'在form中添加一个shape,然后运行,单击窗体就可以看到效果。
Option Base 1 '数组上界为1
Private Sub Form_Click()
Dim Color(8) '定义颜色数组
Randomize '程序随机化
'shape设置为不可见
Shape1.Visible = False
'颜色数组初始化
Color(1) = vbRed: Color(2) = vbGreen: Color(3) = vbBlack: Color(4) = vbWhite
Color(5) = vbBlue: Color(6) = vbMagenta: Color(7) = vbYellow: Color(8) = vbCyan
'边框颜色
Shape1.BorderColor = Color(Int(1 + Rnd * 7))
'正方形边长度
Shape1.Width = 100 + Rnd * 2500
'随机位置
Shape1.Top = Rnd * (Form1.Height - Shape1.Height)
Shape1.Left = Rnd * (Form1.Width - Shape1.Width)
'调整结束,shape设置为可见
Shape1.Visible = True
End Sub
Private Sub Form_Load()
Shape1.Shape = 1
End Sub
'在form中添加一个shape,然后运行,单击窗体就可以看到效果。
Option Base 1 '数组上界为1
Private Sub Form_Click()
Dim Color(8) '定义颜色数组
Randomize '程序随机化
'shape设置为不可见
Shape1.Visible = False
'颜色数组初始化
Color(1) = vbRed: Color(2) = vbGreen: Color(3) = vbBlack: Color(4) = vbWhite
Color(5) = vbBlue: Color(6) = vbMagenta: Color(7) = vbYellow: Color(8) = vbCyan
'边框颜色
Shape1.BorderColor = Color(Int(1 + Rnd * 7))
'正方形边长度
Shape1.Width = 100 + Rnd * 2500
'随机位置
Shape1.Top = Rnd * (Form1.Height - Shape1.Height)
Shape1.Left = Rnd * (Form1.Width - Shape1.Width)
'调整结束,shape设置为可见
Shape1.Visible = True
End Sub
Private Sub Form_Load()
Shape1.Shape = 1
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Private Sub Command1_Click()
Shape1.Visible = True
Timer1.Enabled = True
End Sub
Private Sub Command2_Click()
Timer1.Enabled = False
Shape1.Visible = False
End Sub
Private Sub Command3_Click()
End
End Sub
Private Sub Form_Load()
Form1.ScaleMode = 1
Timer1.Enabled = False
Shape1.FillStyle = 0
Shape1.Shape = 1
Shape1.Visible = True
Form1.Left = 0
Form1.Top = 0
Form1.Height = Screen.Height
Form1.Width = Screen.Width
End Sub
Private Sub Timer1_Timer()
Shape1.FillColor = RGB(CInt(255 * Rnd), CInt(255 * Rnd), CInt(255 * Rnd))
Shape1.Left = CInt(Screen.Width * Rnd)
Shape1.Top = CInt(Screen.Height * Rnd)
Shape1.Height = Rnd * 5 + 10
'Shape1.Refresh
End Sub
Shape1.Visible = True
Timer1.Enabled = True
End Sub
Private Sub Command2_Click()
Timer1.Enabled = False
Shape1.Visible = False
End Sub
Private Sub Command3_Click()
End
End Sub
Private Sub Form_Load()
Form1.ScaleMode = 1
Timer1.Enabled = False
Shape1.FillStyle = 0
Shape1.Shape = 1
Shape1.Visible = True
Form1.Left = 0
Form1.Top = 0
Form1.Height = Screen.Height
Form1.Width = Screen.Width
End Sub
Private Sub Timer1_Timer()
Shape1.FillColor = RGB(CInt(255 * Rnd), CInt(255 * Rnd), CInt(255 * Rnd))
Shape1.Left = CInt(Screen.Width * Rnd)
Shape1.Top = CInt(Screen.Height * Rnd)
Shape1.Height = Rnd * 5 + 10
'Shape1.Refresh
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询