[VB.NET]随机图形

前端之家收集整理的这篇文章主要介绍了[VB.NET]随机图形前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
随机图形

实例说明

在本实例中,我们制作一个应用程序,通过选择不同的按钮来产生不同的随机图形。程序运行结果如图62-1所示。

图62-1 运行结果

技术要点

l DrawEllipse方法

l DrawLine方法

l DrawBezier方法

l DrawArc方法

实现过程

■ 新建项目

打开Visual Studio.NET,选择"新建项目",在项目类型窗口中选择"Visual Basic项目",在模板窗口中选择"Windows应用程序",在名称域中输入"RndPic",然后选择保存路径。单击"确认"。

添加窗体和控件

选择菜单"项目|添加Windows窗体",为项目添加一窗体frmRndPic;在主窗体frmmain上添加六个Button按钮。

■ 设置属性

对窗体上的控件设置属性,对frmmain而言,将Button按钮的Text属性改为和界面一致即可。在这里就不再列出。下面在表62-1中列出了窗体frmRndPic的主要属性

表62-1 窗体frmRndPic的主要属性

窗体/控件 属性

FrmRndPic BorderStyle 0-None

BackColor &H80000007

WindowState 2-Maximized

添加代码

'下面代码是在主窗体中添加

' 选择按钮,执行不同的画图操作

Private Sub btnLine_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles btnLine.Click

item = 5

frmRndpic.DefInstance.Show()

End Sub

Private Sub btnPoint_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles btnPoint.Click

item = 1

frmRndpic.DefInstance.Show()

End Sub

Private Sub btnCircle_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles btnCircle.Click

item = 2

frmRndpic.DefInstance.Show()

End Sub

Private Sub btnEllipse_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles btnEllipse.Click

item = 3

frmRndpic.DefInstance.Show()

End Sub

Private Sub btnArc_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles btnArc.Click

item = 4

frmRndpic.DefInstance.Show()

End Sub

Private Sub btnRectangle_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles btnRectangle.Click

item = 6

frmRndpic.DefInstance.Show()

End Sub

'这是在frmRndPic窗体上添加代码

Private Sub frmRndpic_KeyPress(ByVal eventSender As System.Object,ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress

Dim KeyAscii As Short = Asc(eventArgs.KeyChar)

' ESC的键值为27,当按下ESC键时,返回主界面

If KeyAscii = 27 Then

Me.Close()

frmmain.DefInstance.Show()

End If

If KeyAscii = 0 Then

eventArgs.Handled = True

End If

End Sub

' 执行划线操作

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)

Dim loopnum As Int16

Dim gp As Graphics = e.Graphics

Dim newpen As New Pen(Color.FromArgb(255 * Rnd(),255 * Rnd(),255 * Rnd()),2)

Select Case item

Case 1

'画贝塞尔线

For loopnum = 1 To 100

newpen = New Pen(Color.FromArgb(255 * Rnd(),2)

p.DrawBezier(newpen,Rnd() * Me.Height,Rnd() * Me.Width,Rnd() * Me.Width)

Next

'画随机

Case 2

Dim cirx,ciry As Short

For loopnum = 1 To 100

newpen = New Pen(Color.FromArgb(255 * Rnd(),2)

cirx = Rnd() * Me.Height : ciry = Rnd() * Me.Width

gp.DrawEllipse(newpen,cirx,ciry,cirx)

Next

'画随机椭圆

Case 3

For loopnum = 1 To 100

newpen = New Pen(Color.FromArgb(255 * Rnd(),2)

gp.DrawEllipse(newpen,Rnd() * Me.Height)

Next

Case 4

For loopnum = 1 To 100

newpen = New Pen(Color.FromArgb(255 * Rnd(),2)

gp.DrawArc(newpen,Rnd() * Me.Height)

Next

Case 5

'画随机直线

for loopnum = 1 To 100

newpen = New Pen(Color.FromArgb(255 * Rnd(),2)

'newpen.Color = Color.Black

gp.DrawLine(newpen,Rnd() * Me.Height)

Next

Case 6

'画随机矩形

For loopnum = 1 To 100

newpen = New Pen(Color.FromArgb(255 * Rnd(),2)

gp.DrawRectangle(newpen,Rnd() * Me.Width)

Next

End Select

End Sub

■ 运行结果

单击菜单"调试|启动"或单击 图标运行程序。

小结

本实例说明了绘图的几种方法,可以看出主要使用了画直线(DrawLine)方法、画椭圆(DrawEllipse)方法。在VB.NET中使用画图跟以前完全不一样。它使用封装的类system.Drawing来画图。

猜你在找的VB相关文章