一个雪花屏幕保程序,它显示一个背景,雪花缓缓落下,单击鼠标或按任意键可以退出,主要用的是Graphics.FillEllipse方法,源码如下:
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Friend WithEvents Timer1 As System.Windows.Forms.Timer
Private components As System.ComponentModel.IContainer
'Required by the Windows Form Designer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents pb1 As System.Windows.Forms.PictureBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container()
Me.pb1 = New System.Windows.Forms.PictureBox()
Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
Me.SuspendLayout()
'
'pb1
'
Me.pb1.Location = New System.Drawing.Point(24, 16)
Me.pb1.Name = "pb1"
Me.pb1.Size = New System.Drawing.Size(360, 216)
Me.pb1.TabIndex = 0
Me.pb1.TabStop = False
'
'Timer1
'
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(456, 273)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.pb1})
Me.Name = "Form1"
Me.Text = "ScreenSaver"
Me.ResumeLayout(False)
End Sub
#End Region
'雪花的数量
Private amount As Integer
'每个雪花的横坐标、纵坐标、下落速度和大小
Private snowx() As Integer
Private snowy() As Integer
Private snowv() As Integer
Private snows() As Integer
Private r As Random = New Random()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'设置窗体的边框风格为没有边框(同时也没有标题栏)
Me.FormBorderStyle = FormBorderStyle.None
'设置WindowState为Maximized,可以覆盖任务栏
Me.WindowState = FormWindowState.Maximized
'设置pb1的属性
pb1.Location = Me.Location
pb1.Size = Me.Size
pb1.BorderStyle = System.Windows.Forms.BorderStyle.None
pb1.SizeMode = PictureBoxSizeMode.StretchImage
pb1.Image = Image.FromFile("背景.tif")
'初始化关于雪花的参数
Snow()
Timer1.Interval = 100
Timer1.Enabled = True
'隐藏光标
Me.Cursor.Hide()
End Sub
Private Sub Snow()
amount = 1500
ReDim snowx(amount - 1)
ReDim snowy(amount - 1)
ReDim snowv(amount - 1)
ReDim snows(amount - 1)
Dim i As Integer
For i = 0 To amount - 1
'初始化每个雪花
InitSnowflake(i)
Next
End Sub
Private Sub InitSnowflake(ByVal i As Integer)
'注意横坐标的最大取值为Me.Width-1,否则会发生越界的错误
snowx(i) = r.Next(0, Me.Width - 1)
'这是为了使雪花不要过于集中于屏幕底部
snowy(i) = r.Next(0, Me.Height * 3 / 4)
snowv(i) = r.Next(10, 30)
'这是为了使小雪花的数量多一些,大雪花相对少一些
snows(i) = (r.Next(1, 3) * 100 + r.Next(50, 180)) / 101
End Sub
Private Sub pb1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pb1.Paint
Dim g As Graphics = e.Graphics
Dim i As Integer
For i = 0 To amount - 1
g.FillEllipse(Brushes.White, snowx(i), snowy(i), snows(i), snows(i))
Next
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim i As Integer
For i = 0 To amount - 1
'获取雪花当前时刻的纵坐标
snowy(i) += snowv(i)
If snowy(i) >= Me.Height Then
'如果雪花已经下到屏幕底部,则重新对其进行初始化
InitSnowflake(i)
End If
Next
pb1.Invalidate()
End Sub
Private Sub pb1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pb1.MouseDown
Me.Close()
End Sub
Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
Me.Close()
End Sub
End Class