winform擷取當前Url地址

來源:互聯網
上載者:User
View Code

 1         [DllImport("user32.dll")]
 2         private static extern int FindWindow(string lpClassName, string lpWindowName);
 3 
 4         [DllImport("user32.dll", SetLastError = true)]
 5         private static extern int FindWindowEx(int parentHandle, int childAfter, string className, string windowTitle);
 6 
 7         [DllImport("user32.dll", EntryPoint = "SendMessage")]
 8         private static extern int SendMessage(int hWnd, uint Msg, int wParam, StringBuilder sb);
 9 
10         [DllImport("user32.dll")]
11         private static extern int GetForegroundWindow();
12 
13         private const int WM_GETTEXT = 0x000D;
14 
15         private void button1_Click(object sender, EventArgs e)
16         {
17             int currentPar = FindIEForm();
18          MessageBox.Show( GetIE6URL(currentPar));
19              MessageBox.Show( GetIE8URL(currentPar));
20         }
21         /// <summary>
22         /// 獲得IE表單的控制代碼
23         /// </summary>
24         /// <returns></returns>
25         public static int FindIEForm()  //尋找IE表單;找到就返回控制代碼;
26         {
27             if (FindWindow("IEFrame", null) != (int)0)
28             {
29                 return FindWindow("IEFrame", null);
30             }
31             return (int)0;
32         }
33 
34         /// <summary>
35         /// 獲得IE6.0的url
36         /// </summary>
37         /// <param name="phwnd"></param>
38         /// <returns></returns>
39         public static string GetIE6URL(int phwnd)    //得到IE地址欄裡的資訊;
40         {
41             int child = FindWindowEx(phwnd, 0, "WorkerW", null);
42             child = FindWindowEx(child, 0, "ReBarWindow32", null);
43             child = FindWindowEx(child, 0, "ComboBoxEx32", null);
44             child = FindWindowEx(child, 0, "ComboBox", null);
45             child = FindWindowEx(child, 0, "Edit", null);
46             StringBuilder buffer = new StringBuilder(1024);
47             int num = SendMessage(child, WM_GETTEXT, 1024, buffer);   //用一可變字串變數儲存地址欄的資訊;
48             string URL = buffer.ToString().Trim('/').ToLower();
49             return URL;
50         }
51 
52         /// <summary>
53         /// 獲得IE8.0的url
54         /// </summary>
55         /// <param name="phwnd"></param>
56         /// <returns></returns>
57         public static string GetIE8URL(int phwnd)    //得到IE地址欄裡的資訊;
58         {
59             int mainHand = FindWindow("IEFrame", null);
60             int child = FindWindowEx(mainHand, 0, "WorkerW", null);
61             child = FindWindowEx(child, 0, "ReBarWindow32", null);
62             child = FindWindowEx(child, 0, "Address Band Root", null);
63             child = FindWindowEx(child, 0, "Edit", null);
64             StringBuilder buffer = new StringBuilder(1024);
65             int num = SendMessage(child, WM_GETTEXT, 1024, buffer);   //用一可變字串變數儲存地址欄的資訊;
66             string URL = buffer.ToString().Trim('/').ToLower();
67             return URL;
68         } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.