Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is one way to get the topmost form that uses Win32 (not very elegant, but it works):</p> <pre><code>public const int GW_HWNDNEXT = 2; // The next window is below the specified window public const int GW_HWNDPREV = 3; // The previous window is above [DllImport("user32.dll")] static extern IntPtr GetTopWindow(IntPtr hWnd); [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool IsWindowVisible(IntPtr hWnd); [DllImport("user32.dll", CharSet = CharSet.Auto, EntryPoint = "GetWindow", SetLastError = true)] public static extern IntPtr GetNextWindow(IntPtr hwnd, [MarshalAs(UnmanagedType.U4)] int wFlag); /// &lt;summary&gt; /// Searches for the topmost visible form of your app in all the forms opened in the current Windows session. /// &lt;/summary&gt; /// &lt;param name="hWnd_mainFrm"&gt;Handle of the main form&lt;/param&gt; /// &lt;returns&gt;The Form that is currently TopMost, or null&lt;/returns&gt; public static Form GetTopMostWindow(IntPtr hWnd_mainFrm) { Form frm = null; IntPtr hwnd = GetTopWindow((IntPtr)null); if (hwnd != IntPtr.Zero) { while ((!IsWindowVisible(hwnd) || frm == null) &amp;&amp; hwnd != hWnd_mainFrm) { // Get next window under the current handler hwnd = GetNextWindow(hwnd, GW_HWNDNEXT); try { frm = (Form)Form.FromHandle(hwnd); } catch { // Weird behaviour: In some cases, trying to cast to a Form a handle of an object // that isn't a form will just return null. In other cases, will throw an exception. } } } return frm; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload