''' The MoveWindow function changes the position and dimensions of the specified window. For a top-level window, the position and dimensions are relative to the upper-left corner of the screen. For a child window, they are relative to the upper-left corner of the parent window's client area.
'''
'''
<param name="hWnd" />Handle to the window.
'''
<param name="X" />Specifies the new position of the left side of the window.
'''
<param name="Y" />Specifies the new position of the top of the window.
'''
<param name="nWidth" />Specifies the new width of the window.
'''
<param name="nHeight" />Specifies the new height of the window.
'''
<param name="bRepaint" />Specifies whether the window is to be repainted. If this parameter is TRUE, the window receives a message. If the parameter is FALSE, no repainting of any kind occurs. This applies to the client area, the nonclient area (including the title bar and scroll bars), and any part of the parent window uncovered as a result of moving a child window.
''' If the function succeeds, the return value is nonzero.
''' If the function fails, the return value is zero. To get extended error information, call GetLastError.
_
Public Shared Function MoveWindow(ByVal hWnd As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal bRepaint As Boolean) As Boolean
End Function
_
Private Shared Function SetWindowPos(ByVal hWnd As IntPtr, _
ByVal hWndInsertAfter As IntPtr, _
ByVal X As Integer, _
ByVal Y As Integer, _
ByVal cx As Integer, _
ByVal cy As Integer, _
ByVal uFlags As UInteger) As Boolean
End Function
Public Const SWP_NOSIZE As Integer = &H1
Public Const SWP_NOMOVE As Integer = &H2
Public Const SWP_NOZORDER As Integer = &H4
Public Const SWP_NOREDRAW As Integer = &H8
Public Const SWP_NOACTIVATE As Integer = &H10
Public Const SWP_DRAWFRAME As Integer = &H20
Public Const SWP_FRAMECHANGED As Integer = &H20
Public Const SWP_SHOWWINDOW As Integer = &H40
Public Const SWP_HIDEWINDOW As Integer = &H80
Public Const SWP_NOCOPYBITS As Integer = &H100
Public Const SWP_NOOWNERZORDER As Integer = &H200
Public Const SWP_NOREPOSITION As Integer = &H200
Public Const SWP_NOSENDCHANGING As Integer = &H400
Public Const SWP_DEFERERASE As Integer = &H2000
Public Const SWP_ASYNCWINDOWPOS As Integer = &H4000
Public Shared HWND_TOPMOST As IntPtr = (-1)
Public Delegate Function CallBack( _
ByVal hwnd As Integer, ByVal lParam As Integer) As Boolean
_
Private Shared Function GetWindowText(ByVal hwnd As IntPtr, ByVal lpString As StringBuilder, ByVal cch As Integer) As Integer
End Function
_
Private Shared Function SetWindowText(ByVal hwnd As IntPtr, ByVal lpString As String) As Boolean
End Function
_
Private Shared Function GetWindowThreadProcessId(ByVal hwnd As IntPtr, _
ByRef lpdwProcessId As Integer) As Integer
End Function
_
Private Shared Function GetWindowTextLength(ByVal hwnd As IntPtr) As Integer
End Function
_
Private Shared Function GetWindowRect(ByVal hWnd As HandleRef, ByRef lpRect As RECT) As Boolean
End Function
_
Public Structure RECT
Public Left As Integer
' x position of upper-left corner
Public Top As Integer
' y position of upper-left corner
Public Right As Integer
' x position of lower-right corner
Public Bottom As Integer
' y position of lower-right corner
End Structure
Declare Function EnumWindows Lib "user32" ( _
ByVal x As CallBack, ByVal y As Integer) As Integer
Private Const SLEEP_TIMEOUT = 1000
'''
''' The process name to search
'''
'''
'''
'''
Public Property SearchForProcessName As String
'''
''' The rename text. If empty no renming will be done
'''
'''
'''
'''
Public Property RenameWindowText As String
Private _WindowsPositionCenterPoint As Point
'''
''' The center point for the replace to
'''
'''
'''
'''
Public Property WindowPositionCenterPoint As Point
Get
Return _WindowsPositionCenterPoint
End Get
Set(ByVal value As Point)
_WindowsPositionCenterPoint = value
MustChange = True
End Set
End Property
'the cancel var. (The thread can be stopped by setting the var to true
Private CancelationPending As Boolean = False
'the scan thread we use for scanning and checking the drives
Private ScanThread As Thread
''do we need to change the next step
Private MustChange As Boolean = True
'our process id to search the windows for
Private Shared ProcessId As IntPtr
Public Sub New(ByVal processName As String, ByVal positionCenter As Point, ByVal renameText As String)
Me.SearchForProcessName = processName
Me.WindowPositionCenterPoint = positionCenter
Me.RenameWindowText = renameText
End Sub
'''
''' Starts the detector
'''
'''
Public Sub StartDetecting()
'only start when not starting yet
If ScanThread Is Nothing Then
CancelationPending = False
ScanThread = New Thread(AddressOf DetectWorker)
'start the thread
ScanThread.Start()
End If
End Sub
Public Sub StopDetecting()
If ScanThread IsNot Nothing Then
'set the cancel var
Me.CancelationPending = True
'and wait for the thread to finish
Try
Me.ScanThread.Join()
Catch
End Try
ScanThread = Nothing
End If
End Sub
'''
''' Our thread worker
'''
'''
Private Sub DetectWorker()
While Not CancelationPending
If Not WindowPositionCenterPoint.X.Equals(Double.NaN) And Not WindowPositionCenterPoint.Y.Equals(Double.NaN) Then
'we need to get the process id to scan for
For Each proc In Process.GetProcessesByName(SearchForProcessName)
'we got it
ProcessId = proc.Id
'now we need to test all windws (
EnumWindows(Function(hwnd, lParam)
Dim iProcessID As IntPtr
'get the process id and compare it to the one found before
GetWindowThreadProcessId(hwnd, iProcessID)
If iProcessID = ProcessId Then
'get the window text
Dim normalText As String = RenameWindowText
If Not String.IsNullOrEmpty(RenameWindowText) Then
Dim length As Integer
If hwnd <= 0 Then
Return True
End If
length = GetWindowTextLength(hwnd)
If length = 0 Then
Return True
End If
Dim sb As New System.Text.StringBuilder("", length + 1)
GetWindowText(hwnd, sb, sb.Capacity)
normalText = sb.ToString
End If
'chekc the text (also a variable (we have moved the window already or ot)
If normalText <> RenameWindowText Or MustChange Then
If Not String.IsNullOrEmpty(RenameWindowText) Then
'rename it
SetWindowText(hwnd, RenameWindowText)
End If
'raise our event
Dim alternativeCenterPoint As New Point(Double.NaN, Double.NaN)
Application.Current.Dispatcher.Invoke(Sub()
RaiseEvent BeforeShow(alternativeCenterPoint)
End Sub)
If Not alternativeCenterPoint.X.Equals(Double.NaN) And Not alternativeCenterPoint.Y.Equals(Double.NaN) Then
WindowPositionCenterPoint = alternativeCenterPoint
End If
'we need to postions of the window
Dim rct As RECT
Dim wdth = rct.Right - rct.Left + 1
Dim height = rct.Bottom - rct.Top + 1
'now position the window in the center
SetWindowPos(hwnd, HWND_TOPMOST, CUInt(Math.Max(Me.WindowPositionCenterPoint.X - wdth / 2, 0)), CUInt(Math.Max(Me.WindowPositionCenterPoint.Y - height / 2, 0)), 0, 0, SWP_NOSIZE)
MustChange = False
End If
End If
Return True
End Function, 0)
Next
End If
'wait for the next
Thread.Sleep(SLEEP_TIMEOUT)
End While
End Sub
Public Event BeforeShow(ByRef centerPoint As Point)
End Class |