TA的每日心情 | 无聊 2019-4-21 13:02 |
---|
签到天数: 3 天 [LV.2]圆转纯熟
|
目录
最近看到了一个日文版的监控电脑活动记录的软件,又在win 32 APi中看到了GetForegroundWindow函数,于是决定动动小手用vb写个监控电脑活动记录的小程序。
主要函数
函数名 | 参数 | 返回值 | GetForegroundWindow(void) | 无 | 当前窗口的句柄 | GetWindowText(HWND hWnd,LPTSTR lpString,Int nMaxCount) | hWnd:窗口句柄
lpString:接收窗口标题文本的缓冲区的指针
nMaxCount:指定缓冲区中的最大字符数 | 如果成功则返回标题字符串的字符个数。如果窗口无标题栏或文本,或标题栏为空,或窗口或控制的句柄无效,则返回值为零。 |
实现
循环获取当前焦点所在窗口的标题,然后写入到日志文件中。最后设置开启自启动,隐藏命令行窗口。- 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表示隐藏命令行窗口):- 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脚本
运行
重启电脑后我们可以再任务管理器中看到运行的脚本
然后查看日志文件C:\log.txt
需要关闭结束进程即可
以上就是如何使用vb 监控电脑活动记录的详细内容,更多关于vb 监控电脑活动记录的资料请关注咔叽论坛其它相关文章!
原文地址:https://www.jb51.net/article/207792.htm |
|