1. 問題描述
如何取得正在使用的 Interner Explorer 網址
2. 方法
(1) 取得 Internet Explorer 網址
先加入參考 Microsoft HTML Object Library 與 Microsoft Internet Controls
接著請參考以下程式碼與註解
01 | this .lbURL.Items.Clear(); |
03 | SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass(); |
04 | foreach (SHDocVw.InternetExplorer ie in shellWindows) |
07 | if (Path.GetFileNameWithoutExtension(ie.FullName).ToLower().Equals( "iexplore" )) |
09 | this .lbURL.Items.Add(ie.LocationURL); |
(2) 透過 Windows API GetForegroundWindow 取得正在使用視窗[前景]的控制代碼
將宣告 GetForegroundWindow 部分加入,並且在取得 Internet Explorer 視窗時,判斷是否為正在使用前景視窗
05 | [DllImport( "user32.dll" )] |
06 | private static extern IntPtr GetForegroundWindow(); |
07 | private void timer1_Tick( object sender, EventArgs e) |
09 | this .lbURL.Items.Clear(); |
11 | SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass(); |
12 | foreach (SHDocVw.InternetExplorer ie in shellWindows) |
15 | if (Path.GetFileNameWithoutExtension(ie.FullName).ToLower().Equals( "iexplore" )) |
18 | if (ie.HWND == GetForegroundWindow().ToInt32()) |
20 | this .txtURL.Text = ie.LocationURL; |
22 | this .lbURL.Items.Add(ie.LocationURL); |
出处:http://www.dotblogs.com.tw/chou/archive/2010/01/11/12953.aspx