2016-04-24 14:12:33 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Runtime.InteropServices;
|
2016-04-24 18:57:01 +02:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Windows.Automation;
|
2016-04-24 14:12:33 +02:00
|
|
|
|
|
|
|
|
|
namespace KeeWebHelper
|
|
|
|
|
{
|
|
|
|
|
class WindowHelper
|
|
|
|
|
{
|
|
|
|
|
[DllImport("user32.dll")]
|
|
|
|
|
static extern IntPtr GetForegroundWindow();
|
|
|
|
|
|
|
|
|
|
[DllImport("user32.dll")]
|
|
|
|
|
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
|
|
|
|
|
|
2016-04-24 18:57:01 +02:00
|
|
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
|
|
|
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId);
|
|
|
|
|
|
2016-04-24 14:12:33 +02:00
|
|
|
|
public static WindowInfo GetActiveWindowInfo()
|
|
|
|
|
{
|
|
|
|
|
const int nChars = 2048;
|
|
|
|
|
StringBuilder Buff = new StringBuilder(nChars);
|
2016-04-24 18:57:01 +02:00
|
|
|
|
IntPtr hwnd = GetForegroundWindow();
|
2016-04-24 14:12:33 +02:00
|
|
|
|
|
|
|
|
|
var result = new WindowInfo();
|
2016-04-24 18:57:01 +02:00
|
|
|
|
if (GetWindowText(hwnd, Buff, nChars) > 0)
|
2016-04-24 14:12:33 +02:00
|
|
|
|
{
|
|
|
|
|
result.Title = Buff.ToString();
|
|
|
|
|
}
|
2016-04-24 18:57:01 +02:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
result.Url = GetUrl(hwnd);
|
|
|
|
|
}
|
|
|
|
|
catch { }
|
|
|
|
|
|
2016-04-24 14:12:33 +02:00
|
|
|
|
return result;
|
|
|
|
|
}
|
2016-04-24 18:57:01 +02:00
|
|
|
|
|
|
|
|
|
static string GetUrl(IntPtr hwnd)
|
|
|
|
|
{
|
|
|
|
|
uint pid = 0;
|
|
|
|
|
GetWindowThreadProcessId(hwnd, out pid);
|
|
|
|
|
if (pid <= 0)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
var process = Process.GetProcessById((int)pid);
|
|
|
|
|
return GetUrlAcc(process);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static string GetUrlAcc(Process process)
|
|
|
|
|
{
|
|
|
|
|
var el = AutomationElement.FromHandle(process.MainWindowHandle);
|
|
|
|
|
if (el == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (process.ProcessName.IndexOf("chrome", StringComparison.OrdinalIgnoreCase) >= 0)
|
|
|
|
|
{
|
|
|
|
|
var elChr = el.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Google Chrome"));
|
|
|
|
|
if (elChr != null)
|
|
|
|
|
{
|
|
|
|
|
el = elChr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var controlCondition = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit);
|
|
|
|
|
var valueCondition = new PropertyCondition(AutomationElement.IsValuePatternAvailableProperty, true);
|
|
|
|
|
var condition = new AndCondition(controlCondition, valueCondition);
|
|
|
|
|
AutomationElement urlEl = el.FindFirst(TreeScope.Descendants, condition);
|
|
|
|
|
if (urlEl == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var val = (ValuePattern)urlEl.GetCurrentPattern(ValuePattern.Pattern);
|
|
|
|
|
return val.Current.Value;
|
|
|
|
|
}
|
2016-04-24 14:12:33 +02:00
|
|
|
|
}
|
|
|
|
|
}
|