You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 lines
7.0 KiB

Imports System
Imports System.Text
Imports System.Runtime.InteropServices
Imports System.Threading
Imports System.IO
Public Class ApplicationFileWatcher
#Region "Deklarationen"
Private m_isActive As Boolean
Public Event DocumentClosed()
Private m_Filename As String
Property Filename() As String
Get
Return m_Filename
End Get
Set(ByVal Value As String)
m_Filename = Value
End Set
End Property
Private m_ApplicationType As Integer
Property ApplicationType()
Get
Return m_ApplicationType
End Get
Set(ByVal Value)
m_ApplicationType = Value
End Set
End Property
Private m_Applicationname As String
Property Appname() As String
Get
Return m_Applicationname
End Get
Set(ByVal Value As String)
m_Applicationname = Value
End Set
End Property
Private m_WindowNameDC As String
Property WindowNameDC() As String
Get
Return m_WindowNameDC
End Get
Set(ByVal Value As String)
m_WindowNameDC = Value
End Set
End Property
Private m_WindowName As String
Property WindowName() As String
Get
Return m_WindowName
End Get
Set(ByVal Value As String)
m_WindowName = Value
End Set
End Property
Private m_WindowNamePreview As String
Property WindowNamePreview() As String
Get
Return m_WindowNamePreview
End Get
Set(ByVal Value As String)
m_WindowNamePreview = Value
End Set
End Property
Const STRING_BUFFER_LENGTH As Integer = 255
Dim WindowArray As New ArrayList()
Dim WithEvents MyTimer As New System.Timers.Timer(1000)
#End Region
#Region "Öffentliche Methoden"
Public Sub New()
End Sub
Public Sub New(ByVal typ As Integer, ByVal DocFileName As String)
MyBase.new()
End Sub
Public Sub Dispose()
Me.WindowArray = Nothing
End Sub
Public Sub Start()
AddHandler MyTimer.Elapsed, AddressOf TimerFired
SetWindowName()
MyTimer.Start()
End Sub
Public Sub Stopp()
Me.MyTimer.Stop()
End Sub
Public Sub BringWindowToTop()
Dim i As Integer
SetWindowName()
i = Win32API.FindWindow(vbNullString, Me.WindowName)
If i <> 0 Then
i = Win32API.SetForegroundWindow(i)
i = Win32API.ShowWindow(i, Win32API.SW_RESTORE)
End If
End Sub
Public Function doc_is_active() As Boolean
Dim I As Integer
Search_List()
For I = 0 To Me.WindowArray.Count - 1
If Me.WindowArray.Item(I) = Me.WindowName Then doc_is_active = True
Next
End Function
#End Region
#Region "Private Methoden"
Public Sub SetWindowName()
Select Case Me.ApplicationType
Case 1 'Word
Me.WindowName = Me.Filename + " - Microsoft Word"
Me.WindowNamePreview = Me.Filename + " (Seitenansicht) - Microsoft Word"
Me.WindowNameDC = Me.Filename + " - DC"
Case 2 'Excel
Me.WindowName = "Microsoft Excel - " & Me.Filename
Me.WindowNamePreview = "Microsoft Excel - " & Me.Filename
Me.WindowNameDC = Me.Filename + ""
Case 3
Me.WindowName = Me.Appname & Me.Filename
Me.WindowNamePreview = Me.Appname & Me.Filename
Me.WindowNameDC = Me.Filename + ""
Case 4
Me.WindowName = Me.WindowName
Me.WindowNamePreview = Me.WindowNamePreview
Me.WindowNameDC = Me.Filename + ""
Case Else
End Select
End Sub
Public Function getWindowName() As String
Return Me.WindowName
End Function
Public Sub TimerFired(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles MyTimer.Elapsed
MyTimer.Stop()
' Dim sW As New StreamWriter("C:\TRACE.LOG", True)
' sW.WriteLine(Now)
Dim DocFound As Boolean = False
Dim i As Integer
Dim zz As Integer = 0
'While zz < 3 And DocFound = False
Me.WindowArray.Clear()
Search_List()
For i = 0 To Me.WindowArray.Count - 1
' sW.WriteLine(Me.WindowName + ":" + Me.WindowArray.Item(i))
'If Me.WindowArray.Item(i) = Me.WindowName Then MsgBox("Ist aktiv")
If Me.WindowArray.Item(i) = Me.WindowName Then DocFound = True
If Me.WindowArray.Item(i) = Me.WindowNamePreview Then DocFound = True
If Me.WindowArray.Item(i) = Me.WindowNameDC Then DocFound = True
Next
'zz = zz + 1
' Thread.Sleep(300)
' 'End While
' Thread.Sleep(300)
If Not DocFound Then
Dim fn As String = Globals.Applikationsdaten.Rows(Globals.AppldataRow).Item("pfad_temporaer_dokumente") + "~$" + Microsoft.VisualBasic.Right(Me.Filename, Len(Me.Filename) - 2)
Dim fc As New FileInfo(fn)
If fc.Exists Then DocFound = True
If Not DocFound Then
' sW.WriteLine("Stop:" + Me.WindowName)
MyTimer.Stop()
RaiseEvent DocumentClosed()
Else
MyTimer.Start()
End If
Else
MyTimer.Start()
End If
DocFound = Nothing
'sW.WriteLine(Now)
'sW.Flush()
'sW.Close()
End Sub
Private Sub Search_List()
Win32API.EnumWindowsDllImport(New Win32API.EnumWindowsCallback(AddressOf _
FillActiveWindowsList), 0)
End Sub
Function FillActiveWindowsList(ByVal hWnd As Integer, ByVal lParam As Integer) As Boolean
Dim windowText As New StringBuilder(STRING_BUFFER_LENGTH)
Win32API.GetWindowText(hWnd, windowText, STRING_BUFFER_LENGTH)
If ProcessIsActiveWindow(hWnd) Then
Me.WindowArray.Add(windowText.ToString)
End If
Return True
End Function
Function ProcessIsActiveWindow(ByVal hWnd As Integer) As Boolean
Dim windowText As New StringBuilder(STRING_BUFFER_LENGTH)
Dim windowIsOwned As Boolean
Dim windowStyle As Integer
Win32API.GetWindowText(hWnd, windowText, STRING_BUFFER_LENGTH)
windowIsOwned = Win32API.GetWindow(hWnd, Win32API.GW_OWNER) <> 0
windowStyle = Win32API.GetWindowLong(hWnd, Win32API.GWL_EXSTYLE)
Return True
If Not Win32API.IsWindowVisible(hWnd) Then
Return False
End If
If windowText.ToString.Equals("") Then
Return False
End If
If Win32API.GetParent(hWnd) <> 0 Then
Return False
End If
If (windowStyle And Win32API.WS_EX_TOOLWINDOW) <> 0 And Not windowIsOwned Then
Return False
End If
If (windowStyle And Win32API.WS_EX_APPWINDOW) = 0 And windowIsOwned Then
Return False
End If
Return True
End Function
#End Region
End Class