Search This Blog

Monday, March 30, 2009

Connect to an existing IE Browser in c#

if you like to connect to an existing IE browser and get some events , here is a code snippets to do that .
do not forget to add refrence to SHDocVw .
Yaniv



private void btnConnExisting_Click(object sender, EventArgs e)
{
SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass();

foreach (SHDocVw.InternetExplorer ie in shellWindows)
{
if (ie.LocationURL.Contains("what ever"))
{
MessageBox.Show("Location:" + ie.LocationURL);
ie.BeforeNavigate2 += new SHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHandler(this.ie_BeforeNavigate2);
}
}
}

public void ie_BeforeNavigate2(object pDisp, ref object url, ref object Flags,
ref object TargetFrameName, ref object PostData, ref object Headers, ref bool Cancel)
{
MessageBox.Show("do what you want!");
}
}

No comments: