Hi All,
I am confused. I have tried to use the EnumWindows API to get the handles to the windows in my computer. When the code runs, I cannot find the handle of an opened window in the list of handles that EnumWindows returned. So now, I would like to know the kind of handles that EnumWindows API returns. Why is the window handle of a window that was opened via Process.Start("NotePad.exe") not listed?
Please, run the code if you can. Check the list and see if the NotePad handle is in it. Then, please, tell me what is wrong with the approach. I am looking to get the proper handle to a window so that I can position the window on a preferred parent. I do not want to use the MainWindowHandle property because some processes such as PDF do not return non-zero main window handle in my computer (Windows 7 Pro SP1, X64).
I am confused. I have tried to use the EnumWindows API to get the handles to the windows in my computer. When the code runs, I cannot find the handle of an opened window in the list of handles that EnumWindows returned. So now, I would like to know the kind of handles that EnumWindows API returns. Why is the window handle of a window that was opened via Process.Start("NotePad.exe") not listed?
Please, run the code if you can. Check the list and see if the NotePad handle is in it. Then, please, tell me what is wrong with the approach. I am looking to get the proper handle to a window so that I can position the window on a preferred parent. I do not want to use the MainWindowHandle property because some processes such as PDF do not return non-zero main window handle in my computer (Windows 7 Pro SP1, X64).
Code:
Imports System
Imports System.Runtime.InteropServices
Public Delegate Function CallBack(ByVal hwnd As Integer, ByVal lParam As Integer) As Boolean
Public Module EnumReportApp
Dim cnt As Integer = 0
Declare Function EnumWindows Lib "user32" (ByVal x As CallBack, ByVal y As Integer) As Integer
Public Sub Main()
EnumWindows(AddressOf EnumReportApp.Report, 0)
Dim p As Process = Process.Start("NotePad.exe")
p.WaitForInputIdle()
p.Refresh()
Console.WriteLine()
Console.WriteLine("The current number of windows is: " & cnt)
Console.WriteLine()
Console.WriteLine("NotePad's current handle is: " & p.MainWindowHandle.ToString)
Console.ReadLine()
End Sub 'Main
Public Function Report(ByVal hwnd As Integer, ByVal lParam As Integer) As Boolean
Console.Write("Window handle is: ")
Console.WriteLine(hwnd)
cnt = cnt + 1
Return True
End Function 'Report
End Module 'EnumReportApp