電腦默認一段時間會關閉監視器或者硬盤,即便設置電源都選擇從不也好像不太好使,導致遠程的時候時常出問題,比如qq自動遠程協助沒反應。於是我想到用vb寫一個小工具不停的移動鼠標,讓電腦以爲一直有人在使用,這樣就不會鎖屏或者待機了。
原理就是使用windows的api函數移動鼠標,本案例是基於clswindow開發的,這個是vb下的一個框架,主要是用於控制第三方程序的。主要代碼:
Private Sub Command1_Click()
Dim w As New clsWindow
Randomize
Do
x1 = Screen.Width / 15 * Rnd
y1 = Screen.Height / 15 * Rnd
w.SetCursor x1, y1 '隨機移動到屏幕內任意一個點
w.Wait 5000 '等待5秒鐘
Loop
End Sub
花樣搞多點,隨機畫圓、畫線啥的:
Option Explicit
Dim isDraw As Boolean
Private Sub Command1_Click()
Command1.Enabled = False
Command2.Enabled = True
isDraw = True
Dim w As New clsWindow
Dim i%
Randomize
Do While isDraw
If Int(Rnd * 100) Mod 2 = 0 Then
drawACircle
Else
DrawALine
End If
w.Wait Val(Text1.Text) * 1000 '等待N秒鐘
Loop
Command1.Enabled = True
Command2.Enabled = False
End Sub
'隨機畫個圓
Private Sub drawACircle()
Dim w As New clsWindow
Dim x As Double, y As Double
Dim sW&, sH&
Dim k As Single
Dim R As Double
sW = Screen.Width \ 15
sH = Screen.Height \ 15
Const pi As Single = 3.14159
Randomize
x = (sW - 300) * Rnd + 300
y = (sH - 500) * Rnd + 500
R = sH * Rnd + sH / 4
Me.Caption = R
Do While k < 2 * pi
w.SetCursor Cos(k) * R / 4 + x, Sin(k) * R / 4 + y, , , 5
k = k + pi / 180
DoEvents
Loop
End Sub
'隨機畫根線
Private Sub DrawALine()
Dim w As New clsWindow
Dim x1&, y1&, xPad&, yPad&, lngWidth&, i&, intRndType%, intRndType2%
Dim sW&, sH&
sW = Screen.Width \ 15
sH = Screen.Height \ 15
Randomize
x1 = sW * Rnd
y1 = sH * Rnd
xPad = IIf(x1 > sW / 2, -1, 1)
yPad = IIf(y1 > sH / 2, -1, 1)
lngWidth = sH * Rnd / 2 + sH / 4
intRndType = Int(Rnd * 2)
intRndType2 = Int(Rnd * 2)
For i = 1 To lngWidth
If intRndType = 0 Then
If intRndType2 = 0 Then
x1 = x1 + xPad
Else
y1 = y1 + yPad
End If
Else
x1 = x1 + xPad
y1 = y1 + yPad
End If
w.SetCursor x1, y1
w.Wait 5
Next
End Sub
Private Sub Command2_Click()
isDraw = False
End Sub
clswindow使用手冊:https://www.kancloud.cn/sysdzw/clswindow/1514782
如果不懂vb或者不懂編程的小夥伴可以直接到下面的github下載,裏面的exe是編譯好的,可直接運行。