private InfoItem GetActiveItemFromHandle(string processName, IntPtr handle)
        {
            try
            {
                if (processName == ProcessList.iexplore.ToString())
                {
                    #region IE

                    IEAccessible tabs = new IEAccessible();

                    string pageTitle = tabs.GetActiveTabCaption(handle);
                    string pageURL = tabs.GetActiveTabUrl(handle);

                    return new InfoItem
                    {
                        Title = pageTitle,
                        Uri = pageURL,
                        Type = InfoItemType.Web,
                    };

                    #endregion
                }
                else if (processName == ProcessList.firefox.ToString())
                {
                    #region FireFox

                    AutomationElement ff = AutomationElement.FromHandle(handle);

                    System.Windows.Automation.Condition condition1 = new PropertyCondition(AutomationElement.IsContentElementProperty, true);
                    System.Windows.Automation.Condition condition2 = new PropertyCondition(AutomationElement.ClassNameProperty, "MozillaContentWindowClass");
                    TreeWalker walker = new TreeWalker(new AndCondition(condition1, condition2));
                    AutomationElement elementNode = walker.GetFirstChild(ff);
                    ValuePattern valuePattern;

                    if (elementNode != null)
                    {
                        AutomationPattern[] pattern = elementNode.GetSupportedPatterns();

                        foreach (AutomationPattern autop in pattern)
                        {
                            if (autop.ProgrammaticName.Equals("ValuePatternIdentifiers.Pattern"))
                            {
                                valuePattern = elementNode.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;

                                string pageURL = valuePattern.Current.Value.ToString();
                                string pageTitle = GetActiveWindowText(handle);
                                pageTitle = pageTitle.Remove(pageTitle.IndexOf(" - Mozilla Firefox"));

                                return new InfoItem
                                {
                                    Title = pageTitle,
                                    Uri = pageURL,
                                    Type = InfoItemType.Web,
                                };
                            }
                        }
                    }

                    return null;

                    #endregion
                }
                else if (processName == "chrome")
                {
                    String pageURL = string.Empty;
                    String pageTitle = string.Empty;

                    IntPtr urlHandle = FindWindowEx(handle, IntPtr.Zero, "Chrome_AutocompleteEditView", null);
                    //IntPtr titleHandle = FindWindowEx(handle, IntPtr.Zero, "Chrome_WindowImpl_0", null);
                    IntPtr titleHandle = FindWindowEx(handle, IntPtr.Zero, "Chrome_WidgetWin_0", null);

                    const int nChars = 256;
                    StringBuilder Buff = new StringBuilder(nChars);

                    int length = SendMessage(urlHandle, WM_GETTEXTLENGTH, 0, 0);
                    if (length > 0)
                    {
                        //Get URL from chrome tab
                        SendMessage(urlHandle, WM_GETTEXT, nChars, Buff);
                        pageURL = Buff.ToString();

                        //Get the title
                        GetWindowText((int)titleHandle, Buff, nChars);
                        pageTitle = Buff.ToString();

                        return new InfoItem
                        {
                            Title = pageTitle,
                            Uri = pageURL,
                            Type = InfoItemType.Web,
                        };
                    }
                    else
                        return null;

                }
                else if (processName == ProcessList.OUTLOOK.ToString())
                {
                    #region Outlook

                    MS_Outlook.Application outlookApp;
                    MS_Outlook.MAPIFolder currFolder;
                    MS_Outlook.Inspector inspector;
                    MS_Outlook.MailItem mailItem;

                    outlookApp = (MS_Outlook.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Outlook.Application");
                    currFolder = outlookApp.ActiveExplorer().CurrentFolder;
                    inspector = outlookApp.ActiveInspector();

                    if (inspector == null)
                    {
                        mailItem = (MS_Outlook.MailItem)outlookApp.ActiveExplorer().Selection[1];
                    }
                    else
                    {
                        Regex regex = new Regex(@"\w* - Microsoft Outlook");
                        if (regex.IsMatch(GetActiveWindowText(handle)))
                        {
                            mailItem = (MS_Outlook.MailItem)outlookApp.ActiveExplorer().Selection[1];
                        }
                        else
                        {
                            mailItem = (MS_Outlook.MailItem)inspector.CurrentItem;
                        }
                    }

                    string subject = mailItem.Subject;
                    string entryID = mailItem.EntryID;

                    return new InfoItem
                    {
                        Title = subject,
                        Uri = entryID,
                        Type = InfoItemType.Email,
                    };

                    #endregion
                }
                else if (processName == ProcessList.WINWORD.ToString() ||
                    processName == ProcessList.EXCEL.ToString() ||
                    processName == ProcessList.POWERPNT.ToString() ||
                    processName == ProcessList.AcroRd32.ToString())
                {
                    #region Word, Excel, PPT, Adobe Reader

                    return null;

                    #endregion
                }
                else
                {
                    return null;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }
示例#2
0
        private InfoItem GetActiveItemFromHandle(string processName, IntPtr handle)
        {
            try
            {
                if (processName == ProcessList.iexplore.ToString())
                {
                    #region IE

                    IEAccessible tabs = new IEAccessible();

                    string pageTitle = tabs.GetActiveTabCaption(handle);
                    string pageURL   = tabs.GetActiveTabUrl(handle);

                    return(new InfoItem
                    {
                        Title = pageTitle,
                        Uri = pageURL,
                        Type = InfoItemType.Web,
                    });

                    #endregion
                }
                else if (processName == ProcessList.firefox.ToString())
                {
                    #region FireFox

                    AutomationElement ff = AutomationElement.FromHandle(handle);

                    System.Windows.Automation.Condition condition1 = new PropertyCondition(AutomationElement.IsContentElementProperty, true);
                    System.Windows.Automation.Condition condition2 = new PropertyCondition(AutomationElement.ClassNameProperty, "MozillaContentWindowClass");
                    TreeWalker        walker      = new TreeWalker(new AndCondition(condition1, condition2));
                    AutomationElement elementNode = walker.GetFirstChild(ff);
                    ValuePattern      valuePattern;

                    if (elementNode != null)
                    {
                        AutomationPattern[] pattern = elementNode.GetSupportedPatterns();

                        foreach (AutomationPattern autop in pattern)
                        {
                            if (autop.ProgrammaticName.Equals("ValuePatternIdentifiers.Pattern"))
                            {
                                valuePattern = elementNode.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;

                                string pageURL   = valuePattern.Current.Value.ToString();
                                string pageTitle = GetActiveWindowText(handle);
                                pageTitle = pageTitle.Remove(pageTitle.IndexOf(" - Mozilla Firefox"));

                                return(new InfoItem
                                {
                                    Title = pageTitle,
                                    Uri = pageURL,
                                    Type = InfoItemType.Web,
                                });
                            }
                        }
                    }

                    return(null);

                    #endregion
                }
                else if (processName == "chrome")
                {
                    String pageURL   = string.Empty;
                    String pageTitle = string.Empty;

                    IntPtr urlHandle = FindWindowEx(handle, IntPtr.Zero, "Chrome_AutocompleteEditView", null);
                    //IntPtr titleHandle = FindWindowEx(handle, IntPtr.Zero, "Chrome_WindowImpl_0", null);
                    IntPtr titleHandle = FindWindowEx(handle, IntPtr.Zero, "Chrome_WidgetWin_0", null);

                    const int     nChars = 256;
                    StringBuilder Buff   = new StringBuilder(nChars);

                    int length = SendMessage(urlHandle, WM_GETTEXTLENGTH, 0, 0);
                    if (length > 0)
                    {
                        //Get URL from chrome tab
                        SendMessage(urlHandle, WM_GETTEXT, nChars, Buff);
                        pageURL = Buff.ToString();

                        //Get the title
                        GetWindowText((int)titleHandle, Buff, nChars);
                        pageTitle = Buff.ToString();

                        return(new InfoItem
                        {
                            Title = pageTitle,
                            Uri = pageURL,
                            Type = InfoItemType.Web,
                        });
                    }
                    else
                    {
                        return(null);
                    }
                }
                else if (processName == ProcessList.OUTLOOK.ToString())
                {
                    #region Outlook

                    MS_Outlook.Application outlookApp;
                    MS_Outlook.MAPIFolder  currFolder;
                    MS_Outlook.Inspector   inspector;
                    MS_Outlook.MailItem    mailItem;

                    outlookApp = (MS_Outlook.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Outlook.Application");
                    currFolder = outlookApp.ActiveExplorer().CurrentFolder;
                    inspector  = outlookApp.ActiveInspector();

                    if (inspector == null)
                    {
                        mailItem = (MS_Outlook.MailItem)outlookApp.ActiveExplorer().Selection[1];
                    }
                    else
                    {
                        Regex regex = new Regex(@"\w* - Microsoft Outlook");
                        if (regex.IsMatch(GetActiveWindowText(handle)))
                        {
                            mailItem = (MS_Outlook.MailItem)outlookApp.ActiveExplorer().Selection[1];
                        }
                        else
                        {
                            mailItem = (MS_Outlook.MailItem)inspector.CurrentItem;
                        }
                    }

                    string subject = mailItem.Subject;
                    string entryID = mailItem.EntryID;

                    return(new InfoItem
                    {
                        Title = subject,
                        Uri = entryID,
                        Type = InfoItemType.Email,
                    });

                    #endregion
                }
                else if (processName == ProcessList.WINWORD.ToString() ||
                         processName == ProcessList.EXCEL.ToString() ||
                         processName == ProcessList.POWERPNT.ToString() ||
                         processName == ProcessList.AcroRd32.ToString())
                {
                    #region Word, Excel, PPT, Adobe Reader

                    return(null);

                    #endregion
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception)
            {
                return(null);
            }
        }