winform获取当前Url地址

时间:2022-11-26 18:59:07
winform获取当前Url地址winform获取当前Url地址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         }