如何使用vb 监控电脑活动记录


最近看到了一个日文版的监控电脑活动记录的软件,�在win 32 APi中看到了GetForegroundWindow函数,于是决定动动�手用vb写个监控电脑活动记录的�程��/p>

主�函数

函数�/th> �数 返回�/th>
GetForegroundWindow(void) �/td> 当�窗�的��/td>
GetWindowText(HWND hWnd,LPTSTR lpString,Int nMaxCount) hWnd:窗��柄
lpString:接收窗�标题文本的缓冲区的指�br /> nMaxCount:指定缓冲区中的最大字符数
如果�功则返回标题字符串的字符个数。如果窗�无标题�或文本,或标题�为空,或窗�或控制的�柄无效,则返回值为零�/td>

实现

循环获�当�焦点所在窗�的标题,然�写入到日志文件中。最�设置开�自�动,��命令行窗��/p>

Imports System
Imports System.io

Module Module1
 private Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
 'Win32 Api
 Private Declare Function GetForegroundWindow Lib "user32" () As Long
 Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
 
 Sub Main()
	 
  Dim bt As Boolean = True
	 ' �存标题文本
  Dim stext As String
	 ' �存上一个窗���
	 Dim hwnd As Long
	 ' �存当�窗��柄
	 Dim curHwnd As Long
	 ' 书写�写入日志文�
	 Dim sw As StreamWriter
	 ' 日志文件�存路径
	 Dim path As String = "c:log.txt"
	 
	 ' 如果存在日志文件则跳过,�则创建一个日志文�
	 If Not File.Exists(path) Then
		 File.Create(path)
	 End If
	 
	 sleep(3000)
	 
	 ' 这里是个死循�
	 While bt
	 
	  stext = Space(255)
		 ' 获�当�窗��柄
		 hwnd = GetForegroundWindow
		 
		 ' 如果当�是新窗�则写入新窗�标题
		 If hwnd <> curHwnd Then 
		  curHwnd = hwnd
				' 获�窗�标题
				GetWindowText(hwnd,stext,255)
				
				sw = System.IO.File.AppendText(path)
				
				' 写入新窗�标�格� yyyy年mm月dd�hh:hh:ss + 标题
				Using sw
					sw.WriteLine(String.Format("{0:F}", DateTime.Now) +" "+ stext) 
					sw.Flush() 
				End Using
				
		
		 End If
   sleep(2000)
		 
  End While
	 
 End Sub
End Module

开�自�动

新建一个listener.vbs文件(其中C:listener.exe是vb编译�的文件路径,Run�数0表示��命令行窗�)�/p>

Dim ws 
set ws = WScript.createObject("WScript.shell") 
ws.Run "C:listener.exe", 0, TRUE

1. �行 -> shell:startup

2. 开始��-> 程� -> �动

3. �行 -> gpedit.msc

4. �动 -> 开机中添加listener.vbs脚本

�行

��电脑�我们�以�任务管�器中看到�行的脚�/p>

然�查看日志文件C:log.txt

需�关闭结�进程��/p>

以上就是如何使用vb 监控电脑活动记录的详细内容,更多关于vb 监控电脑活动记录的资料请关注脚本之家其它相关文章�/p>