Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I am using the following but i dont think have defined the process name correctly on line 31.</p> <pre><code>using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Diagnostics; using System.Runtime.InteropServices; namespace test_winform_app { public partial class Form2 : Form { public Form2() { InitializeComponent(); } [DllImport("user32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); [DllImport("user32.dll")] private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); [DllImport("user32.dll")] private static extern bool IsIconic(IntPtr hWnd); private void button1_Click(object sender, EventArgs e) { Process yourProcess = Process.GetProcessesByName("notepad"); Dictionary&lt;IntPtr, string&gt; windows = (Dictionary&lt;IntPtr, string&gt;)WindowEnumerator.GetOpenWindowsFromPID(yourProcess.Id); IntPtr mainWindowHandle = IntPtr.Zero; foreach (KeyValuePair&lt;IntPtr, string&gt; pair in windows) { if (pair.Value.ToUpperInvariant() == "Main Window Title") { mainWindowHandle = pair.Key; break; } } if (mainWindowHandle != IntPtr.Zero) { if (IsIconic(mainWindowHandle)) { ShowWindow(mainWindowHandle, 9); } SetForegroundWindow(mainWindowHandle); } } delegate bool EnumWindowsProc(IntPtr hWnd, int lParam); public static class WindowEnumerator { [DllImport("user32.dll", SetLastError = true)] private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); [DllImport("USER32.DLL")] private static extern bool EnumWindows(EnumWindowsProc enumFunc, int lParam); [DllImport("USER32.DLL")] private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount); [DllImport("USER32.DLL")] private static extern int GetWindowTextLength(IntPtr hWnd); [DllImport("USER32.DLL")] private static extern bool IsWindowVisible(IntPtr hWnd); [DllImport("USER32.DLL")] private static extern IntPtr GetShellWindow(); public static IDictionary&lt;IntPtr, string&gt; GetOpenWindowsFromPID(int processID) { IntPtr hShellWindow = GetShellWindow(); Dictionary&lt;IntPtr, string&gt; dictWindows = new Dictionary&lt;IntPtr, string&gt;(); EnumWindows(delegate(IntPtr hWnd, int lParam) { if (hWnd == hShellWindow) return true; if (!IsWindowVisible(hWnd)) return true; int length = GetWindowTextLength(hWnd); if (length == 0) return true; uint windowPid; GetWindowThreadProcessId(hWnd, out windowPid); if (windowPid != processID) return true; StringBuilder stringBuilder = new StringBuilder(length); GetWindowText(hWnd, stringBuilder, length + 1); dictWindows.Add(hWnd, stringBuilder.ToString()); return true; }, 0); return dictWindows; } } } } </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. This table or related slice is empty.
    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