C# get URL from firefox but don't use DDE

我的梦境 提交于 2019-12-12 18:33:55

问题


For detect URL in firefox I use DDE

 DdeClient dde = new DdeClient("Firefox", "WWW_GetWindowInfo");
 dde.Connect();
 string url1 = dde.Request("URL", int.MaxValue);
 dde.Disconnect();
 temp = url1.Replace("\"", "").Replace("\0", "");
 dde = null;

This code works perfectly!, but it is too slow for connect (dde.Connect();) is too slow 7/8 second! Is there another way to get url from firefox? (example use API windows like "SendMessage")


回答1:


This works well for me:

                AutomationElement element = AutomationElement.FromHandle(intPtr);  // intPtr is the MainWindowHandle for FireFox browser
                element = element.FindFirst(TreeScope.Subtree,
                      new AndCondition(
                          new PropertyCondition(AutomationElement.NameProperty, "Search or enter address"),
                          new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit)));
                string url = ((ValuePattern)element.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;



回答2:


            Process[] process = Process.GetProcessesByName("firefox");

            foreach (Process firefox in process)
            {
                // the chrome process must have a window
                if (firefox.MainWindowHandle == IntPtr.Zero)
                {
                    return null;
                }

                AutomationElement element = AutomationElement.FromHandle(firefox.MainWindowHandle);
                if (element == null)
                    return null;

                //search for first custom element
                AutomationElement custom1 = element.FindFirst(TreeScope.Descendants,
                 new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Custom));

                //search all custom element children
                AutomationElementCollection custom2 = custom1.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Custom));

                //for each custom child
                foreach (AutomationElement item in custom2)
                {
                    //search for first custom element
                    AutomationElement custom3 = item.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Custom));
                    //search for first document element
                    AutomationElement doc3 = custom3.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document));



                    if (!doc3.Current.IsOffscreen)
                    {
                        url = ((ValuePattern)doc3.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
                        return url;
                    }                      
                }
            }             
            return url;
   }


来源:https://stackoverflow.com/questions/15447518/c-sharp-get-url-from-firefox-but-dont-use-dde

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!