I have the following class declared:
public partial class MainWindow : Window
And I need to get the actual handle of the window once the window has one. How can I do that and where should I put the query function.
What I tried so far was:
IntPtr hwnd = new WindowInteropHelper(this).Handle;
But the handle I get back is 0, which might be because it was planted in OnInitialized - maybe the window is not ready yet at that stage. And, yes - it is connected via WPF, thank you for pointing it out!
Thanks
The Win32 API provides no direct method for obtaining the window handle associated with a console application. However, you can obtain the window handle by calling FindWindow() . This function retrieves a window handle based on a class name or window name. Call GetConsoleTitle() to determine the current console title.
Once the Selenium WebDriver instance is instantiated, a unique alphanumeric id is assigned to the window. This is called window handle and is used to identify browser windows.
A handle is a unique identifier for an object managed by Windows. It's like a pointer, but not a pointer in the sence that it's not an address that could be dereferenced by user code to gain access to some data.
You can use EnumWindows and GetWindowThreadProcessId() functions as mentioned in this MSDN article.
In the OnInitialized
method the handle has not yet been created. But you are on the right track. If you put your call in the Loaded
event the handle will have been created and it should return the correct handle.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With