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