Example #1
0
        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));
        }
Example #2
0
        //调用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;
        }
Example #3
0
        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());
        }
Example #4
0
        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);
Example #5
0
        public static List <WindowInfo> GetAllDesktopWindows()
        {
            wndList.Clear();
            WNDENUMPROC ewp = new WNDENUMPROC(MyENUMPROC);

            //enum all desktop windows
            EnumWindows(ewp, 0);

            return(wndList);
        }
Example #6
0
        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)
            {
                ;
            }
        }
Example #7
0
    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);
        }
    }
Example #8
0
 public static extern bool EnumWindows([MarshalAs(UnmanagedType.FunctionPtr)] WNDENUMPROC lpEnumFunc, IntPtr lParam);
Example #9
0
 public static extern bool EnumChildWindows(IntPtr hwndParent, WNDENUMPROC lpEnumFunc, int lParam);
Example #10
0
 private static extern bool EnumChildWindows(IntPtr hWndParent, WNDENUMPROC lpEnumFunc, IntPtr lParam);
Example #11
0
 static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, IntPtr lParam);
Example #12
0
 [DllImport("user32.dll")] public static extern bool EnumChildWindows(IntPtr hwnd, WNDENUMPROC func, IntPtr lParam);
Example #13
0
 private static extern int EnumDesktopWindows(int hDesktop, WNDENUMPROC lpfn, IntPtr lParam);
Example #14
0
 public static extern bool EnumChildWindows(IntPtr hWnd, WNDENUMPROC lpEnumFunc, uint LParam);
 public static extern bool EnumThreadWindows(uint dwThreadId, WNDENUMPROC lpfn, IntPtr lParam);
Example #16
0
        /// <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;
        }
Example #17
0
 public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, System.IntPtr lParam);
Example #18
0
 public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.SysInt)]
                                       int lParam);
Example #19
0
 public static extern BOOL EnumWindows(WNDENUMPROC lpEnumFunc, nint lParam);
Example #20
0
 public static extern bool EnumChildWindows([System.Runtime.InteropServices.InAttribute()] System.IntPtr hWndParent, WNDENUMPROC lpEnumFunc, int lParam);
Example #21
0
 private static extern int EnumThreadWindows(int dwThreadId, WNDENUMPROC lpfn, IntPtr lParam);
Example #22
0
        //调用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;
        }
Example #23
0
 public static extern int EnumWindows(WNDENUMPROC lpEnumFunc, IntPtr lParam);
Example #24
0
 public static extern bool EnumThreadWindows(            //枚举线程窗口
     int dwThreadId,
     WNDENUMPROC lpEnumFunc,
     int lParam
     );
Example #25
0
 internal static extern bool EnumWindows(
     WNDENUMPROC cb,                       // WNDENUMPROC lpEnumFiunc
     IntPtr manageObject                   // LPARAM      lParam (the managed object))
     );
Example #26
0
 [DllImport("user32.dll")] public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, IntPtr lparam);
 public static extern bool EnumThreadWindows(uint dwThreadId, WNDENUMPROC lpfn, IntPtr lParam);
Example #28
0
 internal static extern bool EnumWindows(WNDENUMPROC enumFunc, ref WNDENUMPARAMS lParam);
Example #29
0
 public static extern bool EnumWindows(WNDENUMPROC func, IntPtr lParam);
Example #30
0
 private static extern int EnumWindows(WNDENUMPROC lpEnumWindow, uint lParam);
 private static extern int EnumChildWindows(
     IntPtr hWndParent,
     WNDENUMPROC lpEnumFunc,
     ref IntPtr lParam);
Example #32
0
 public static extern bool EnumWindows(
     [In] WNDENUMPROC lpEnumFunc,
     [In] IntPtr lParam
     );
Example #33
0
 public static extern bool EnumDesktopWindows(SafeDesktopHandle hDesktop, WNDENUMPROC lpfn, IntPtr lParam);
Example #34
0
 public static extern bool EnumWindows(WNDENUMPROC func, IntPtr lParam);
Example #35
0
 private static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, uint lParam);
Example #36
0
 public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, int lParam);
Example #37
0
 public static extern int EnumChildWindows(IntPtr hWndParent, WNDENUMPROC lpfn, int lParam);
Example #38
0
 private static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, IntPtr lParam);
Example #39
0
 public static extern bool EnumChildWindows(IntPtr hWnd, WNDENUMPROC lpEnumFunc, uint LParam);
Example #40
0
 public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, int lParam);
Example #41
0
 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);