From my MainWindow I am trying to open another window knowing only its name. In my MainWindow ViewModel, I have:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (lpClassName As String, lpWindowName As String) As Long
Private Declare Function ShowWindow Lib "user32" (HWND As Long, ncmdShow As Long) As Boolean
Private ReadOnly Property _OpenWindow As New RelayCommand(Of Object)(AddressOf DoOpenWindow)
Public ReadOnly Property OpenWindow As RelayCommand(Of Object)
Get
Return _OpenWindow
End Get
End Property
Private Sub DoOpenWindow(obj As Object)
Dim WinName As String = CStr(obj)
Dim XhWnd As Long = FindWindow(WinName, WinName)
Debug.WriteLine(XhWnd)
Dim x As Boolean = ShowWindow(XhWnd, 5)
Debug.WriteLine(x)
End Sub
For testing purposes the Class, Name and Title of the window to be opened are all set to "NewWindow". The first Debug statement returns 0 so the function FindWindow is not working. Where is the error?
Additional questions:
- Since this is a 64 bit application, is the use of 32 bit functions an issue?
- More broadly, is there a better way to do this? I have been unable to find one.