Skip to content

Instantly share code, notes, and snippets.

@deenseth
Created December 9, 2011 23:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deenseth/1453886 to your computer and use it in GitHub Desktop.
Save deenseth/1453886 to your computer and use it in GitHub Desktop.
Determine Active Windows and URL of the item from IE 8, Firefox 3.6, Chrome 11, Outlook
handle is the return of the function GetForegroundWindow()
private InfoItem GetActiveItemFromHandle(string processName, IntPtr handle)
{
try
{
if (processName == ProcessList.iexplore.ToString())
{
#region IE 8
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 3.6. The logic below doesn't work with Firefox 4 or above.
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
}
// Work for chrome 11 but not above.
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment