static void SaveWindowLayout(string layoutName, bool interactive) { User32.WNDENUMPROC callback = new WNDENUMPROC(WindowCallBack); User32.EnumWindows(callback, new IntPtr(0)); AddProcessInformationToMap(); List <App> forConfigFile = new List <App>(openWindowHandleMap.Values.Where(app => !String.IsNullOrEmpty(app.FileName))); List <App> saveTheseWindows = new List <App>(); if (interactive) { foreach (App app in forConfigFile) { string prompt = "Do you want to save the window with \nfilename = {0} and \nwindow title = {1}? Y for yes or anything else for no."; Console.WriteLine(String.Format(prompt, app.FileName, app.ShortWindowTitle)); string input = Console.ReadLine(); if (input.ToLower().StartsWith("y")) { saveTheseWindows.Add(app); } } } else { saveTheseWindows = forConfigFile; } File.WriteAllText(layoutName, JsonConvert.SerializeObject(saveTheseWindows)); }
//调用GetCurrentWindowHandle()即可返回当前进程的主窗口句柄,如果获取失败则返回IntPtr.Zero public static IntPtr GetCurrentWindowHandle() { IntPtr ptrWnd = IntPtr.Zero; uint uiPid = (uint)Process.GetCurrentProcess().Id; // 当前进程 ID object objWnd = processWnd[uiPid]; if (objWnd != null) { ptrWnd = (IntPtr)objWnd; if (ptrWnd != IntPtr.Zero && IsWindow(ptrWnd)) // 从缓存中获取句柄 { return ptrWnd; } else { ptrWnd = IntPtr.Zero; } } WNDENUMPROC callback=new WNDENUMPROC(EnumWindowsProc); bool bResult = EnumWindows(callback, uiPid); GC.KeepAlive(callback); // 枚举窗口返回 false 并且没有错误号时表明获取成功 if (!bResult && Marshal.GetLastWin32Error() == 0) { objWnd = processWnd[uiPid]; if (objWnd != null) { ptrWnd = (IntPtr)objWnd; } } return ptrWnd; }
public WindowInfo[] GetAllDesktopWindows() { //用来保存窗口对象 列表 List <WindowInfo> wndList = new List <WindowInfo>(); WNDENUMPROC pro = delegate(IntPtr hWnd, int lParam) { WindowInfo wnd = new WindowInfo(); StringBuilder sb = new StringBuilder(256); //get hwnd wnd.hWnd = hWnd; //get window name GetWindowTextW(hWnd, sb, sb.Capacity); wnd.szWindowName = sb.ToString(); //get window class GetClassNameW(hWnd, sb, sb.Capacity); wnd.szClassName = sb.ToString(); //add it into list wndList.Add(wnd); return(true); }; //enum all desktop windows EnumWindows(pro, 0); return(wndList.ToArray()); }
public static List <IntPtr> GetAllWindowsOfProcess(Process process) { var handles = new List <IntPtr>(0); var callback = new WNDENUMPROC((hWnd, _) => { handles.Add(hWnd); return(true); }); foreach (ProcessThread thread in process.Threads) { PInvoke.EnumThreadWindows((uint)thread.Id, callback, default);
public static List <WindowInfo> GetAllDesktopWindows() { wndList.Clear(); WNDENUMPROC ewp = new WNDENUMPROC(MyENUMPROC); //enum all desktop windows EnumWindows(ewp, 0); return(wndList); }
static void EnumWindowsTopToDown(IntPtr hwnd, WNDENUMPROC proc, long param) { IntPtr currentWindow = GetTopWindow(hwnd); if (currentWindow == IntPtr.Zero) { return; } if ((currentWindow = GetWindow(hwnd, 1)) == IntPtr.Zero) { return; } while (proc(currentWindow, param) && (currentWindow = GetWindow(currentWindow, 3)) != IntPtr.Zero) { ; } }
public static void Main() { List <UInt32> hWnds = new List <UInt32>(); // // Prevent the managed object (hWnds) from being collected // by the garbage collector: // GCHandle objHandle = GCHandle.Alloc(hWnds); // // Create an instance of a delegate in order to // provide a «callback» for EnumWindows: // WNDENUMPROC enumProc = new WNDENUMPROC(callback); // // Get an internal representation for the gc handle // so as to be able to pass it to the second parameter // of EnumWindows: // IntPtr objHandlePtr = GCHandle.ToIntPtr(objHandle); WinAPI.EnumWindows( // Let Windows iterate over each window and enumProc, // call enumProc (which is «initialized» for the method callback) objHandlePtr // and pass this pointer to the method ); // // Free the handle of the object so that // the object can be collected and // thus to prevent memory leaks: // objHandle.Free(); StringBuilder title = new StringBuilder(256); foreach (UInt32 hWnd in hWnds) { WinAPI.GetWindowText(hWnd, title, 256); Console.WriteLine(" {0,-8}: {1}", hWnd, title); } }
public static extern bool EnumWindows([MarshalAs(UnmanagedType.FunctionPtr)] WNDENUMPROC lpEnumFunc, IntPtr lParam);
public static extern bool EnumChildWindows(IntPtr hwndParent, WNDENUMPROC lpEnumFunc, int lParam);
private static extern bool EnumChildWindows(IntPtr hWndParent, WNDENUMPROC lpEnumFunc, IntPtr lParam);
static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, IntPtr lParam);
[DllImport("user32.dll")] public static extern bool EnumChildWindows(IntPtr hwnd, WNDENUMPROC func, IntPtr lParam);
private static extern int EnumDesktopWindows(int hDesktop, WNDENUMPROC lpfn, IntPtr lParam);
public static extern bool EnumChildWindows(IntPtr hWnd, WNDENUMPROC lpEnumFunc, uint LParam);
public static extern bool EnumThreadWindows(uint dwThreadId, WNDENUMPROC lpfn, IntPtr lParam);
/// <summary> /// If the mainwindowhandle is equal to 0 , its mean NO GRAPHICAL INTERFACE, /// It means the process is probably a background or a zombie process /// </summary> /// <returns>the list of active processes with a graphic interface</returns> public static List<Window> GetApplications() { List<Window> lstWindows = new List<Window>(); Process[] procList; WNDENUMPROC edw_cb = new WNDENUMPROC(EnumDesktopWindows_cb); GCHandle gch = GCHandle.Alloc(lstWindows); try { procList = Process.GetProcesses(); foreach (Process p in procList) { if (p.MainWindowHandle != System.IntPtr.Zero) { foreach (System.Diagnostics.ProcessThread t in p.Threads) { EnumThreadWindows(t.Id, edw_cb, GCHandle.ToIntPtr(gch)); } } } gch.Free(); } catch (Exception e) { Logging.LogException(e); Misc.KwmTellUser("Unable to get list of running processes. (" + e.ToString() + ")"); } return lstWindows; }
public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, System.IntPtr lParam);
public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.SysInt)] int lParam);
public static extern BOOL EnumWindows(WNDENUMPROC lpEnumFunc, nint lParam);
public static extern bool EnumChildWindows([System.Runtime.InteropServices.InAttribute()] System.IntPtr hWndParent, WNDENUMPROC lpEnumFunc, int lParam);
private static extern int EnumThreadWindows(int dwThreadId, WNDENUMPROC lpfn, IntPtr lParam);
public static extern int EnumWindows(WNDENUMPROC lpEnumFunc, IntPtr lParam);
public static extern bool EnumThreadWindows( //枚举线程窗口 int dwThreadId, WNDENUMPROC lpEnumFunc, int lParam );
internal static extern bool EnumWindows( WNDENUMPROC cb, // WNDENUMPROC lpEnumFiunc IntPtr manageObject // LPARAM lParam (the managed object)) );
[DllImport("user32.dll")] public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, IntPtr lparam);
internal static extern bool EnumWindows(WNDENUMPROC enumFunc, ref WNDENUMPARAMS lParam);
public static extern bool EnumWindows(WNDENUMPROC func, IntPtr lParam);
private static extern int EnumWindows(WNDENUMPROC lpEnumWindow, uint lParam);
private static extern int EnumChildWindows( IntPtr hWndParent, WNDENUMPROC lpEnumFunc, ref IntPtr lParam);
public static extern bool EnumWindows( [In] WNDENUMPROC lpEnumFunc, [In] IntPtr lParam );
public static extern bool EnumDesktopWindows(SafeDesktopHandle hDesktop, WNDENUMPROC lpfn, IntPtr lParam);
private static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, uint lParam);
public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, int lParam);
public static extern int EnumChildWindows(IntPtr hWndParent, WNDENUMPROC lpfn, int lParam);
private static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, IntPtr lParam);
private static extern int EnumChildWindows(IntPtr hWnd, WNDENUMPROC lpEnumFunc, int lParam);
private static extern bool EnumChildWindows(IntPtr hWndParent, WNDENUMPROC lpEnumFunc, int lParam);
static public extern int EnumChildWindows(IntPtr hWndParent, WNDENUMPROC lpEnumFunc, int lParam);