Skip to content

Commit 5dd990b

Browse files
committed
Move window logging logic into separate module
Preparatory patch to refactor code in to more modular form. This will ease the subsequent tasks. - to add pre-processing hooks.
1 parent e452daf commit 5dd990b

File tree

3 files changed

+95
-64
lines changed

3 files changed

+95
-64
lines changed

ActiveWindowLogger/ActiveWindowLogger.vbproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
<DependentUpon>frmMain.vb</DependentUpon>
106106
<SubType>Form</SubType>
107107
</Compile>
108+
<Compile Include="loggingModule.vb" />
108109
<Compile Include="My Project\AssemblyInfo.vb" />
109110
<Compile Include="My Project\Application.Designer.vb">
110111
<AutoGen>True</AutoGen>

ActiveWindowLogger/frmMain.vb

Lines changed: 4 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ Imports System.Data.OleDb
44

55
Public Class frmMain
66

7-
Private Declare Function GetForegroundWindow Lib "user32.dll" () As IntPtr
8-
Private Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As IntPtr, ByRef lpdwProcessID As Integer) As Integer
9-
Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hWnd As IntPtr, ByVal WinTitle As String, ByVal MaxLength As Integer) As Integer
10-
Private Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Integer
11-
127
Private Declare Function WTSRegisterSessionNotification Lib "Wtsapi32" (ByVal hWnd As IntPtr, ByVal THISSESS As Long) As Long
138
Private Declare Function WTSUnRegisterSessionNotification Lib "Wtsapi32" (ByVal hWnd As IntPtr) As Long
149

@@ -28,68 +23,13 @@ Public Class frmMain
2823
SESSION_REMOTE_CONTROL = 9
2924
End Enum
3025

31-
' Set during app launch.
32-
' Used to track how long the app has been runnning.
33-
Dim AppLaunchTime As New Date
34-
3526
Private Sub tmrMinute_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrMinute.Tick
36-
lblStatus.Text = lvEntries.Items.Count.ToString + " windows" + vbCrLf + "since last " + DateDiff(DateInterval.Minute, AppLaunchTime, DateTime.UtcNow).ToString + " minutes"
27+
logStatus()
3728
End Sub
3829

3930
Private Sub tmrPoll_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrPoll.Tick
40-
41-
' Get the Handle to the Current Foreground Window
42-
Dim hWnd As IntPtr = GetForegroundWindow()
43-
If hWnd = IntPtr.Zero Then Exit Sub
44-
45-
' Find the Length of the Window's Title
46-
Dim TitleLength As Integer
47-
TitleLength = GetWindowTextLength(hWnd)
48-
49-
' Find the Window's Title
50-
Dim WindowTitle As String = StrDup(TitleLength + 1, "*")
51-
GetWindowText(hWnd, WindowTitle, TitleLength + 1)
52-
53-
' Find the PID of the Application that Owns the Window
54-
Dim pid As Integer = 0
55-
GetWindowThreadProcessId(hWnd, pid)
56-
If pid = 0 Then Exit Sub
57-
58-
' Get the actual PROCESS from the PID
59-
Dim proc As Process = Process.GetProcessById(pid)
60-
If proc Is Nothing Then Exit Sub
61-
62-
' Populate the textboxes with current data
63-
txtPID.Text = pid.ToString
64-
txtProcessName.Text = proc.ProcessName
65-
txtMainWindowTitle.Text = proc.MainWindowTitle
66-
txtWindowTitle.Text = WindowTitle
67-
68-
' Ignore own Window
69-
If txtWindowTitle.Text.Equals(Me.Text) Then
70-
txtWindowStatus.Text = "Ignoring " + Me.Text
71-
Exit Sub
72-
End If
73-
74-
' If WindowsTitles of new entry and last entry match, skip/consolidate
75-
If lvEntries.Items.Count Then
76-
If txtWindowTitle.Text.Equals(lvEntries.Items(0).SubItems(3).Text) Then
77-
txtWindowStatus.Text = "Same window already active"
78-
Exit Sub
79-
End If
80-
End If
81-
82-
' Populate the listview with new entry of above current data
83-
Dim newEntry As ListViewItem
84-
newEntry = lvEntries.Items.Insert(0, txtPID.Text)
85-
newEntry.SubItems.Add(txtProcessName.Text)
86-
newEntry.SubItems.Add(txtMainWindowTitle.Text)
87-
newEntry.SubItems.Add(txtWindowTitle.Text)
88-
newEntry.SubItems.Add(Format(Now, "yyyy/MM/dd HH:mm:ss"))
89-
txtWindowStatus.Text = "Started tracking active window"
90-
91-
lblStatus.Text = lvEntries.Items.Count.ToString + " windows" + vbCrLf + "since last " + DateDiff(DateInterval.Minute, AppLaunchTime, DateTime.UtcNow).ToString + " minutes"
92-
31+
logActiveWindow()
32+
logStatus()
9333
End Sub
9434

9535
Private Sub chkStatus_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkStatus.CheckedChanged
@@ -107,7 +47,7 @@ Public Class frmMain
10747
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
10848
WTSRegisterSessionNotification(Me.Handle, NOTIFY_FOR_ALL_SESSIONS)
10949
chkStatus.Checked = True
110-
AppLaunchTime = DateTime.UtcNow
50+
initAppLaunchTime()
11151
End Sub
11252

11353
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
Module loggingModule
2+
3+
Private Declare Function GetForegroundWindow Lib "user32.dll" () As IntPtr
4+
Private Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As IntPtr, ByRef lpdwProcessID As Integer) As Integer
5+
Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hWnd As IntPtr, ByVal WinTitle As String, ByVal MaxLength As Integer) As Integer
6+
Private Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Integer
7+
8+
9+
' Set during app launch.
10+
' Used to track how long the app has been runnning.
11+
Dim AppLaunchTime As New Date
12+
13+
Public Sub initAppLaunchTime()
14+
AppLaunchTime = DateTime.UtcNow
15+
End Sub
16+
17+
Public Sub logStatus()
18+
frmMain.lblStatus.Text = frmMain.lvEntries.Items.Count.ToString + " windows" + vbCrLf + "since last " + DateDiff(DateInterval.Minute, AppLaunchTime, DateTime.UtcNow).ToString + " minutes"
19+
End Sub
20+
21+
Public Sub logActiveWindow()
22+
23+
With frmMain
24+
25+
' Get the Handle to the Current Foreground Window
26+
Dim hWnd As IntPtr = GetForegroundWindow()
27+
If hWnd = IntPtr.Zero Then Exit Sub
28+
29+
' Find the Length of the Window's Title
30+
Dim TitleLength As Integer
31+
TitleLength = GetWindowTextLength(hWnd)
32+
33+
' Find the Window's Title
34+
Dim WindowTitle As String = StrDup(TitleLength + 1, "*")
35+
GetWindowText(hWnd, WindowTitle, TitleLength + 1)
36+
37+
' Find the PID of the Application that Owns the Window
38+
Dim pid As Integer = 0
39+
GetWindowThreadProcessId(hWnd, pid)
40+
If pid = 0 Then Exit Sub
41+
42+
' Get the actual PROCESS from the PID
43+
Dim proc As Process = Process.GetProcessById(pid)
44+
If proc Is Nothing Then Exit Sub
45+
46+
' Populate the textboxes with current data
47+
.txtPID.Text = pid.ToString
48+
.txtProcessName.Text = proc.ProcessName
49+
.txtMainWindowTitle.Text = proc.MainWindowTitle
50+
.txtWindowTitle.Text = WindowTitle
51+
52+
'****************************************************************
53+
' Ignore own app windows
54+
'****************************************************************
55+
If .txtWindowTitle.Text.Equals(frmMain.Text) Then
56+
.txtWindowStatus.Text = "Ignoring " + frmMain.Text
57+
Exit Sub
58+
End If
59+
60+
If .txtWindowTitle.Text.Equals(frmAbout.Text) Then
61+
.txtWindowStatus.Text = "Ignoring " + frmAbout.Text
62+
Exit Sub
63+
End If
64+
65+
'****************************************************************
66+
' Consolidate windowsTitles of new and last entries match.
67+
'****************************************************************
68+
If .lvEntries.Items.Count Then
69+
If .txtWindowTitle.Text.Equals(.lvEntries.Items(0).SubItems(3).Text) Then
70+
.txtWindowStatus.Text = "Same window already active"
71+
Exit Sub
72+
End If
73+
End If
74+
75+
'****************************************************************
76+
' Populate listview with the new entry of current active window
77+
'****************************************************************
78+
Dim newEntry As ListViewItem
79+
newEntry = .lvEntries.Items.Insert(0, .txtPID.Text)
80+
newEntry.SubItems.Add(.txtProcessName.Text)
81+
newEntry.SubItems.Add(.txtMainWindowTitle.Text)
82+
newEntry.SubItems.Add(.txtWindowTitle.Text)
83+
newEntry.SubItems.Add(Format(Now, "yyyy/MM/dd HH:mm:ss"))
84+
.txtWindowStatus.Text = "Started tracking active window"
85+
86+
End With
87+
88+
End Sub
89+
90+
End Module

0 commit comments

Comments
 (0)