winform获取当前Url地址

本文介绍了一种通过C#代码获取IE6及IE8浏览器当前页面URL的方法。利用P/Invoke调用Windows API函数,实现了对IE浏览器窗口句柄的查找,并进一步获取其地址栏中的URL信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ExpandedBlockStart.gifView 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         } 

转载于:https://www.cnblogs.com/sera/archive/2011/10/07/2200449.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值