public List<WindowInfo> GetWindowsList()
 {
     windows = new List<WindowInfo>();
     NativeMethods.EnumWindowsProc ewp = new NativeMethods.EnumWindowsProc(EvalWindows);
     NativeMethods.EnumWindows(ewp, IntPtr.Zero);
     return windows;
 }
Exemplo n.º 2
0
 public List <WindowInfo> GetWindowsList()
 {
     windows = new List <WindowInfo>();
     NativeMethods.EnumWindowsProc ewp = EvalWindows;
     NativeMethods.EnumWindows(ewp, IntPtr.Zero);
     return(windows);
 }
Exemplo n.º 3
0
        public List <SimpleWindowInfo> GetWindowsRectangleList()
        {
            windows       = new List <SimpleWindowInfo>();
            parentHandles = new HashSet <IntPtr>();

            NativeMethods.EnumWindowsProc ewp = EvalWindow;
            NativeMethods.EnumWindows(ewp, IntPtr.Zero);

            List <SimpleWindowInfo> result = new List <SimpleWindowInfo>();

            foreach (SimpleWindowInfo window in windows)
            {
                bool rectVisible = true;

                foreach (SimpleWindowInfo window2 in result)
                {
                    if (window2.Rectangle.Contains(window.Rectangle))
                    {
                        rectVisible = false;
                        break;
                    }
                }

                if (rectVisible)
                {
                    result.Add(window);
                }
            }

            return(result);
        }
        /// <summary>
        /// Refreshing the <see cref="AvailableOpenedWindows"/> collection with opened application windows.
        /// </summary>
        private static void GetOpenedWindows()
        {
            AvailableOpenedWindows.Clear();

            NativeMethods.EnumWindowsProc callback = new NativeMethods.EnumWindowsProc(Report);
            NativeMethods.EnumWindows(callback, (IntPtr)0);
        }
Exemplo n.º 5
0
        private void PopulateWindowsListWin()
        {
            NativeMethods.EnumWindowsProc procEnum = delegate(IntPtr hWnd,
                                                              IntPtr lParam)
            {
                // Wrapped in try-catch, because it's passed to native code
                try
                {
                    string strName = NativeMethods.GetWindowText(hWnd, true);
                    if (!string.IsNullOrEmpty(strName))
                    {
                        if (((NativeMethods.GetWindowStyle(hWnd) &
                              NativeMethods.WS_VISIBLE) != 0) &&
                            AutoType.IsValidAutoTypeWindow(hWnd, false) &&
                            !NativeMethods.IsTaskBar(hWnd))
                        {
                            m_cmbWindow.Items.Add(strName);
                            m_vWndImages.Add(UIUtil.GetWindowImage(hWnd, true));
                        }
                    }
                }
                catch (Exception) { Debug.Assert(false); }

                return(true);
            };

            NativeMethods.EnumWindows(procEnum, IntPtr.Zero);
            m_cmbWindow.OrderedImageList = m_vWndImages;
        }
Exemplo n.º 6
0
        public List <Rectangle> GetWindowsRectangleList()
        {
            rectangles = new List <Rectangle>();
            NativeMethods.EnumWindowsProc ewp = EvalWindow;
            NativeMethods.EnumWindows(ewp, IntPtr.Zero);

            List <Rectangle> result = new List <Rectangle>();

            foreach (Rectangle rect in rectangles)
            {
                bool rectVisible = true;

                foreach (Rectangle rect2 in result)
                {
                    if (rect2.Contains(rect))
                    {
                        rectVisible = false;
                        break;
                    }
                }

                if (rectVisible)
                {
                    result.Add(rect);
                }
            }

            return(result);
        }
Exemplo n.º 7
0
        public override IEnumerable <IWindowControl> GetChildControls()
        {
            ISet <IntPtr> childs = new HashSet <IntPtr>();

            GCHandle handleList        = GCHandle.Alloc(childs);
            IntPtr   pointerHandleList = GCHandle.ToIntPtr(handleList);

            try
            {
                NativeMethods.EnumWindowsProc childProc = new NativeMethods.EnumWindowsProc(this.EnumChild);
                NativeMethods.EnumChildWindows(this.ControlHandler, childProc, pointerHandleList);
            }
            finally
            {
                handleList.Free();
            }

            ISet <IWindowControl> childWindows = new HashSet <IWindowControl>();

            foreach (IntPtr child in childs)
            {
                IWindowControl windowsControl = new WindowControl(child, this);
                childWindows.Add(windowsControl);
            }

            return(childWindows);
        }
Exemplo n.º 8
0
        /*
         *      public static List<Process> GetChildrenProcesses(IntPtr hParent) {
         *              List<Process> retVal = new List<Process>();
         *              IntPtr prevChild = IntPtr.Zero;
         *              IntPtr currChild = IntPtr.Zero;
         *              int count=0;
         *              while(count < 100) {
         *                      currChild = NativeMethods.FindWindowEx(hParent,prevChild,null,null);
         *                      if(currChild == IntPtr.Zero) {
         *                              break;
         *                      }
         *                      int id=NativeMethods.GetProcessId(currChild);
         *                      Process process=Process.GetProcessById(id);
         *                      //if(process.MainWindowHandle!=IntPtr.Zero) {
         *                              retVal.Add(process);
         *                              //but don't add processes with no window
         *                      //}
         *                      prevChild = currChild;
         *                      count++;
         *              }
         *              return retVal;
         *      }
         *
         *      internal static List<IntPtr> GetChildWindowsShort(IntPtr hWnd) {
         *              List<IntPtr> retVal=new List<IntPtr>();
         *              IntPtr hChildWnd;
         *              int GW_CHILD=5;
         *              int GW_HWNDNEXT=2;
         *              hChildWnd = NativeMethods.GetWindow(hWnd, GW_CHILD);
         *              while(hChildWnd!=IntPtr.Zero){
         *                      hChildWnd = NativeMethods.GetWindow(hChildWnd, GW_HWNDNEXT);
         *                      //todo: could be invalid
         *                      retVal.Add(hChildWnd);
         *              }
         *              return retVal; //not any shorter than GetChildWindows
         *      }*/

        internal static ArrayList GetChildWindows(IntPtr hWnd)
        {
            ArrayList windowHandles = new ArrayList();

            NativeMethods.EnumWindowsProc callBackPtr = GetWindowHandle;
            NativeMethods.EnumChildWindows(hWnd, callBackPtr, windowHandles);
            return(windowHandles);
        }
Exemplo n.º 9
0
        private void PopulateWindowsListWin()
        {
            Dictionary <IntPtr, bool> dWnds = new Dictionary <IntPtr, bool>();

            NativeMethods.EnumWindowsProc procEnum = delegate(IntPtr hWnd,
                                                              IntPtr lParam)
            {
                try
                {
                    if (hWnd != IntPtr.Zero)
                    {
                        dWnds[hWnd] = true;
                    }
                }
                catch (Exception) { Debug.Assert(false); }

                return(true);
            };
            NativeMethods.EnumWindows(procEnum, IntPtr.Zero);
            GC.KeepAlive(procEnum);             // Like in MainWindowFinder.FindMainWindow

            // On Windows 8 and higher, EnumWindows does not return Metro
            // app windows, thus we try to discover these windows using
            // the FindWindowEx function; we do this in addition to EnumWindows,
            // because calling FindWindowEx in a loop is less reliable (and
            // by additionally using EnumWindows we at least get all desktop
            // windows for sure)
            if (WinUtil.IsAtLeastWindows8)
            {
                int    nMax = (dWnds.Count * 2) + 2;
                IntPtr h    = NativeMethods.FindWindowEx(IntPtr.Zero, IntPtr.Zero,
                                                         null, null);
                for (int i = 0; i < nMax; ++i)
                {
                    if (h == IntPtr.Zero)
                    {
                        break;
                    }

                    dWnds[h] = true;

                    h = NativeMethods.FindWindowEx(IntPtr.Zero, h, null, null);
                }
            }

            foreach (KeyValuePair <IntPtr, bool> kvp in dWnds)
            {
                ThreadPool.QueueUserWorkItem(new WaitCallback(
                                                 EditAutoTypeItemForm.EvalWindowProc),
                                             new PwlwInfo(this, kvp.Key));
            }

            m_cmbWindow.OrderedImageList = m_vWndImages;
        }
        protected override bool DispatchMessage(object message, DispatchTarget target)
        {
            if (target == DispatchTarget.LocalProcess)
            {
                InvokeMessageHandlers(message);
                return(true);
            }

            bool successfullyDispatched = false;

            NativeMethods.EnumWindowsProc enumProc = null;

            switch (target)
            {
            case DispatchTarget.FirstOtherProcess:
                enumProc = delegate(IntPtr hWnd, IntPtr lParam) {
                    if (ShouldSendToWindow(hWnd))
                    {
                        bool wasHandled = SendMessage(hWnd, message);

                        if (wasHandled)
                        {
                            successfullyDispatched = true;
                            return(NativeMethods.StopWindowEnumeration);
                        }
                    }

                    return(NativeMethods.ContinueWindowEnumeration);
                };
                break;

            case DispatchTarget.AllOtherProcesses:
                successfullyDispatched = true;
                enumProc = delegate(IntPtr hWnd, IntPtr lParam) {
                    if (ShouldSendToWindow(hWnd))
                    {
                        SendMessage(hWnd, message);
                    }

                    return(NativeMethods.ContinueWindowEnumeration);
                };
                break;

            default:
                throw new NotSupportedException();
            }

            NativeMethods.EnumWindows(enumProc, lParam: IntPtr.Zero);
            return(successfullyDispatched);
        }
Exemplo n.º 11
0
        private bool CheckHandle(IntPtr handle, bool isWindow)
        {
            if (handle == IgnoreHandle || !NativeMethods.IsWindowVisible(handle) || (isWindow && NativeMethods.IsWindowCloaked(handle)))
            {
                return(true);
            }

            SimpleWindowInfo windowInfo = new SimpleWindowInfo(handle);

            if (isWindow)
            {
                windowInfo.IsWindow  = true;
                windowInfo.Rectangle = CaptureHelpers.GetWindowRectangle(handle);
            }
            else
            {
                windowInfo.Rectangle = NativeMethods.GetWindowRect(handle);
            }

            if (!windowInfo.Rectangle.IsValid())
            {
                return(true);
            }

            if (IncludeChildWindows && !parentHandles.Contains(handle))
            {
                parentHandles.Add(handle);

                NativeMethods.EnumWindowsProc ewp = EvalControl;
                NativeMethods.EnumChildWindows(handle, ewp, IntPtr.Zero);
            }

            if (isWindow)
            {
                Rectangle clientRect = NativeMethods.GetClientRect(handle);

                if (clientRect.IsValid() && clientRect != windowInfo.Rectangle)
                {
                    windows.Add(new SimpleWindowInfo(handle, clientRect));
                }
            }

            windows.Add(windowInfo);

            return(true);
        }
Exemplo n.º 12
0
        ///<summary>Gets visible top-level windows.  Still need more filtering if want the ones showing in task bar.</summary>
        internal static List <IntPtr> GetAllVisibleWindows()
        {
            ArrayList arrayHwnds = new ArrayList();

            NativeMethods.EnumWindowsProc callBackPtr = GetWindowHandle;
            NativeMethods.EnumWindows(callBackPtr, arrayHwnds);
            List <IntPtr> listHwnds = new List <IntPtr>();

            for (int i = 0; i < arrayHwnds.Count; i++)
            {
                if (!NativeMethods.IsWindowVisible((IntPtr)arrayHwnds[i]))                 //cuts from 500 down to 50
                {
                    continue;
                }
                listHwnds.Add((IntPtr)arrayHwnds[i]);
            }
            return(listHwnds);
        }
        private bool EvalWindow(IntPtr hWnd, IntPtr lParam)
        {
            WindowInfo window = new WindowInfo(hWnd);

            if (!IsValidWindow(window))
            {
                return(true);
            }

            if (IncludeChildWindows)
            {
                NativeMethods.EnumWindowsProc ewp = EvalWindow;
                NativeMethods.EnumChildWindows(hWnd, ewp, IntPtr.Zero);
            }

            windows.Add(window);

            return(true);
        }
Exemplo n.º 14
0
        private bool CheckHandle(IntPtr handle, bool isWindow)
        {
            if (handle == IgnoreHandle || !NativeMethods.IsWindowVisible(handle))
            {
                return(true);
            }

            Rectangle rect;

            if (isWindow)
            {
                rect = CaptureHelpers.GetWindowRectangle(handle);
            }
            else
            {
                rect = NativeMethods.GetWindowRect(handle);
            }

            if (!rect.IsValid())
            {
                return(true);
            }

            if (IncludeChildWindows)
            {
                NativeMethods.EnumWindowsProc ewp = EvalControl;
                NativeMethods.EnumChildWindows(handle, ewp, IntPtr.Zero);
            }

            if (isWindow)
            {
                Rectangle clientRect = NativeMethods.GetClientRect(handle);

                if (clientRect.IsValid())
                {
                    rectangles.Add(clientRect);
                }
            }

            rectangles.Add(rect);

            return(true);
        }
Exemplo n.º 15
0
        internal static IEnumerable <IntPtr> FindWindows(NativeMethods.EnumWindowsProc filter)
        {
            var windows = new List <IntPtr>();

            NativeMethods.EnumWindows(
                delegate(IntPtr wnd, IntPtr param)
            {
                if (filter(wnd, param))
                {
                    // only add the windows that pass the filter
                    windows.Add(wnd);
                }

                // but return true here so that we iterate all windows
                return(true);
            },
                IntPtr.Zero);

            return(windows);
        }
Exemplo n.º 16
0
        public Rectangle CalculateWindowRectangle()
        {
            if (handle.ToInt32() > 0)
            {
                rectangle = CaptureHelpers.GetWindowRectangle(handle);

                NativeMethods.GetWindowThreadProcessId(handle, out processId);

                string processName = Process.GetProcessById((int)processId).ProcessName;

                if (!Engine.ConfigWorkflow.ActiveWindowTryCaptureChildren || processName == "explorer")
                {
                    windows.Enqueue(new KeyValuePair <IntPtr, Rectangle>(handle, rectangle));
                }
                else
                {
                    NativeMethods.EnumWindowsProc ewpWindows = new NativeMethods.EnumWindowsProc(EvalWindows);
                    NativeMethods.EnumWindows(ewpWindows, IntPtr.Zero);
                }

                foreach (KeyValuePair <IntPtr, Rectangle> window in windows)
                {
                    rectangle = rectangle.Merge(window.Value);
                    NativeMethods.EnumWindowsProc ewpControls = new NativeMethods.EnumWindowsProc(EvalControls);
                    NativeMethods.EnumChildWindows(window.Key, ewpControls, IntPtr.Zero);
                }

                foreach (KeyValuePair <IntPtr, Rectangle> control in controls)
                {
                    rectangle = rectangle.Merge(control.Value);
                }

                rectangle.Intersect(bounds);

                //TestRectangle(rectangle);

                return(rectangle);
            }

            return(Rectangle.Empty);
        }
        public Rectangle CalculateWindowRectangle()
        {
            if (handle.ToInt32() > 0)
            {
                rectangle = CaptureHelpers.GetWindowRectangle(handle);

                NativeMethods.GetWindowThreadProcessId(handle, out processId);

                string processName = Process.GetProcessById((int)processId).ProcessName;

                if (!Engine.ConfigWorkflow.ActiveWindowTryCaptureChildren || processName == "explorer")
                {
                    windows.Enqueue(new KeyValuePair<IntPtr, Rectangle>(handle, rectangle));
                }
                else
                {
                    NativeMethods.EnumWindowsProc ewpWindows = new NativeMethods.EnumWindowsProc(EvalWindows);
                    NativeMethods.EnumWindows(ewpWindows, IntPtr.Zero);
                }

                foreach (KeyValuePair<IntPtr, Rectangle> window in windows)
                {
                    rectangle = rectangle.Merge(window.Value);
                    NativeMethods.EnumWindowsProc ewpControls = new NativeMethods.EnumWindowsProc(EvalControls);
                    NativeMethods.EnumChildWindows(window.Key, ewpControls, IntPtr.Zero);
                }

                foreach (KeyValuePair<IntPtr, Rectangle> control in controls)
                {
                    rectangle = rectangle.Merge(control.Value);
                }

                rectangle.Intersect(bounds);

                //TestRectangle(rectangle);

                return rectangle;
            }

            return Rectangle.Empty;
        }
Exemplo n.º 18
0
        public static IReadOnlyList <SystemWindow> GetAllWindows()
        {
            var result = new List <SystemWindow>();

            var callback = new NativeMethods.EnumWindowsProc((hWnd, lParam) =>
            {
                if (hWnd != IntPtr.Zero)
                {
                    var window = new SystemWindow(hWnd);
                    result.Add(window);
                }

                return(true);
            });

            if (!NativeMethods.EnumWindows(callback, IntPtr.Zero))
            {
                Debug.WriteLine("Failed to enumerate windows.");
            }

            return(result);
        }
 public bool EnumChildWindows(IntPtr hWndParent, Func<IntPtr, bool> lpEnumFunc)
 {
     var enumWindowsProcWrapper = new NativeMethods.EnumWindowsProc((IntPtr wnd, ref IntPtr _) => lpEnumFunc(wnd));
     var lParam = IntPtr.Zero;
     return NativeMethods.EnumChildWindows(hWndParent, enumWindowsProcWrapper, ref lParam);
 }
        private bool EvalWindow(IntPtr hWnd, IntPtr lParam)
        {
            WindowInfo window = new WindowInfo(hWnd);

            if (!IsValidWindow(window))
            {
                return true;
            }

            if (IncludeChildWindows)
            {
                NativeMethods.EnumWindowsProc ewp = new NativeMethods.EnumWindowsProc(EvalWindow);
                NativeMethods.EnumChildWindows(hWnd, ewp, IntPtr.Zero);
            }

            windows.Add(window);

            return true;
        }
Exemplo n.º 21
0
        private void OnFormLoad(object sender, EventArgs e)
        {
            Debug.Assert(m_vStringDict != null); if (m_vStringDict == null)
            {
                throw new InvalidOperationException();
            }
            Debug.Assert(m_atConfig != null); if (m_atConfig == null)
            {
                throw new InvalidOperationException();
            }

            GlobalWindowManager.AddWindow(this);

            m_ctxKeySeq.Attach(m_rbKeySeq);
            m_ctxKeyCodes.Attach(m_rtbPlaceholders);

            if (!m_bEditSequenceOnly)
            {
                m_bannerImage.Image = BannerFactory.CreateBanner(m_bannerImage.Width,
                                                                 m_bannerImage.Height, BannerStyle.Default,
                                                                 Properties.Resources.B48x48_KCMSystem, KPRes.ConfigureAutoTypeItem,
                                                                 KPRes.ConfigureAutoTypeItemDesc);
            }
            else             // Edit keystrokes only
            {
                m_bannerImage.Image = BannerFactory.CreateBanner(m_bannerImage.Width,
                                                                 m_bannerImage.Height, BannerStyle.Default,
                                                                 Properties.Resources.B48x48_KCMSystem, KPRes.ConfigureKeystrokeSeq,
                                                                 KPRes.ConfigureKeystrokeSeqDesc);
            }

            this.Icon = Properties.Resources.KeePass;

            // m_clrOriginalForeground = m_lblOpenHint.ForeColor;
            m_clrOriginalBackground = m_cmbWindow.BackColor;
            // m_strOriginalWindowHint = m_lblTargetWindowInfo.Text;

            StringBuilder sbPH = new StringBuilder();

            sbPH.Append("<b>");
            sbPH.Append(KPRes.StandardFields);
            sbPH.Append(":</b><br />");

            sbPH.Append("{" + PwDefs.TitleField + "} ");
            sbPH.Append("{" + PwDefs.UserNameField + "} ");
            sbPH.Append("{" + PwDefs.PasswordField + "} ");
            sbPH.Append("{" + PwDefs.UrlField + "} ");
            sbPH.Append("{" + PwDefs.NotesField + "}");

            bool bCustomInitialized = false;

            foreach (KeyValuePair <string, ProtectedString> kvp in m_vStringDict)
            {
                if (!PwDefs.IsStandardField(kvp.Key))
                {
                    if (bCustomInitialized == false)
                    {
                        sbPH.Append("<br /><br /><b>");
                        sbPH.Append(KPRes.CustomFields);
                        sbPH.Append(":</b><br />");
                        bCustomInitialized = true;
                    }

                    sbPH.Append("{" + PwDefs.AutoTypeStringPrefix + kvp.Key + "} ");
                }
            }

            sbPH.Append("<br /><br /><b>" + KPRes.KeyboardKeyModifiers + ":</b><br />");
            sbPH.Append(KPRes.KeyboardKeyShift + @": +, ");
            sbPH.Append(KPRes.KeyboardKeyControl + @": ^, ");
            sbPH.Append(KPRes.KeyboardKeyAlt + @": %");

            sbPH.Append("<br /><br /><b>" + KPRes.SpecialKeys + ":</b><br />");
            foreach (string strNav in SpecialKeyCodes)
            {
                if (strNav == VkcBreak)
                {
                    sbPH.Append("<br /><br />");
                }
                else
                {
                    sbPH.Append("{" + strNav + "} ");
                }
            }

            sbPH.Append("<br /><br /><b>" + KPRes.OtherPlaceholders + ":</b><br />");
            foreach (string strPH in SpecialPlaceholders)
            {
                if (strPH == VkcBreak)
                {
                    sbPH.Append("<br /><br />");
                }
                else
                {
                    sbPH.Append("{" + strPH + "} ");
                }
            }

            m_rtbPlaceholders.Rtf = StrUtil.SimpleHtmlToRtf(sbPH.ToString());
            LinkifyRtf(m_rtbPlaceholders);

            if (m_strOriginalName != null)
            {
                m_cmbWindow.Text = m_strOriginalName;

                if (!m_bEditSequenceOnly)
                {
                    m_rbKeySeq.Text = m_atConfig.GetSafe(m_strOriginalName);
                }
                else
                {
                    m_rbKeySeq.Text = m_atConfig.DefaultSequence;
                }
            }

            m_bBlockUpdates = true;
            if (m_rbKeySeq.Text.Length > 0)
            {
                m_rbSeqCustom.Checked = true;
            }
            else
            {
                m_rbSeqDefault.Checked = true;
            }
            m_bBlockUpdates = false;

            try
            {
                NativeMethods.EnumWindowsProc procEnum = delegate(IntPtr hWnd,
                                                                  IntPtr lParam)
                {
                    string strName = NativeMethods.GetWindowText(hWnd);
                    if ((strName != null) && (strName.Length > 0))
                    {
                        if ((NativeMethods.GetWindowStyle(hWnd) &
                             NativeMethods.WS_VISIBLE) != 0)
                        {
                            m_cmbWindow.Items.Add(strName);
                        }
                    }

                    return(true);
                };

                NativeMethods.EnumWindows(procEnum, IntPtr.Zero);
            }
            catch (Exception) { Debug.Assert(false); }

            EnableControlsEx();
            m_cmbWindow.Focus();
        }
Exemplo n.º 22
0
 internal static extern bool EnumThreadWindows(uint dwThreadId, NativeMethods.EnumWindowsProc lpfn, IntPtr lParam);
Exemplo n.º 23
0
 public static extern bool EnumWindows(NativeMethods.EnumWindowsProc callback, IntPtr extraData);
Exemplo n.º 24
0
 public static extern bool EnumChildWindows(IntPtr hwndParent, NativeMethods.EnumWindowsProc lpEnumFunc, IntPtr lParam);