I created a new Windows Forms App in Visual Studio and added the following code to it:
new Form1().Show();
By calling this code, the Process.MainWindowHandle
of my process, reported by Process.GetProcessById(Environment.ProcessId).MainWindowHandle
will be changed to the new Form.
But if I activate the first created form again, the MainWindowHandle
will be reported with the initially created form. So, if I’m right, the Process.MainWindowHandle
always reports the active form handle and not the initially created form.
I’ve also tried to use a ApplicationContext
with defining its MainForm
property, but this does not change anything.
How and I define a MainForm
in .Net with C#, which handles will be reported by Process.MainWindowHandle
always, independent if it’s active or hidden?
2
Answers
Inspired by the comments to my question, I found this solution to get the handle of my MainForm:
With this code, I will find a windows handle of a process, which answers to a user defined Windows message:
... if the MainForm was found, the
mainFormHandle
is set to it's handle.To use this code, my MainForm has to answer the sent Windows message:
You could create a statically available
Map
between names and windows. So, if you want to perform something on a specific window, then you would be able to load that window by name from thisMap
. This of course means that whenever a window is created, it needs to be reflected into this Map and whenever a window is disposed of, then the Map needs to be adjusted as well.As about your actual question, look at this resource: https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.mainwindowhandle?view=net-8.0
particularly this part:
So, whatever refreshes the TopLevel property by focusing the form, such as the
Show
method that you called will change the main window handle accordingly. If you expect something different to occur instead, then you will need to implement that logic yourself.