Public Class Form1
Public k As Integer, g As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Randomize()
k = My.Computer.Screen.WorkingArea.Width '取电脑屏幕像素宽度
g = My.Computer.Screen.WorkingArea.Height '取电脑屏幕像素高度
Me.Left = Rnd() * (k - Me.Width)
Me.Top = Rnd() * (g - Me.Height)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Close()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'MousePosition.Y 是鼠标在屏幕上的的纵坐标
If MousePosition.Y > Me.Top And MousePosition.Y < Me.Height + Me.Top Then
If MousePosition.X > Me.Left And MousePosition.X < Me.Left + Me.Width Then
Me.Left = Rnd() * (k - Me.Width)
Me.Top = Rnd() * (g - Me.Height)
End If
End If
End Sub
End Class
要点:
- 时钟周期设置10毫秒的话……谁都没这个能耐吧~
- 在界面上画个按钮,按钮单击事件设置为关闭程序,这样可以检验你是否点到按钮了
- 把初始窗口设置设置成没有边框的那种,然后缩成只有按钮的大小。