using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices; //操作Win32API必须要引入的命名空间
namespace IEManipulation
{
public class IEManipulation
{
[DllImport("User32.dll")] //User32.dll是Windows操作系统的核心动态库之一
static extern int FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32.dll")]
static extern int FindWindowEx(int hwndParent, int hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll")]
static extern int GetWindowText(int hwnd, StringBuilder buf, int nMaxCount);
[DllImport("User32.dll")]
static extern int SendMessage(int hWnd, int Msg, int wParam, StringBuilder lParam);
const int WM_GETTEXT = 0x000D; //获得文本消息的16进制表示
/// <summary>
/// Get the URL of the current opened IE
/// </summary>
public static string GetURL()
{
int parent = FindWindow("IEFrame", null);
int child = FindWindowEx(parent, 0, "WorkerW", null);
child = FindWindowEx(child, 0, "ReBarWindow32", null);
child = FindWindowEx(child, 0, "ComboBoxEx32", null);
child = FindWindowEx(child, 0, "ComboBox", null);
child = FindWindowEx(child, 0, "Edit", null); //通过SPY++获得地址栏的层次结构,然后一层一层获得
StringBuilder buffer = new StringBuilder(1024);
//child表示要操作窗体的句柄号
//WM_GETTEXT表示一个消息,怎么样来驱动窗体
//1024表示要获得text的大小
//buffer表示获得text的值存放在内存缓存中
int num = SendMessage(child, WM_GETTEXT, 1024, buffer);
string URL = buffer.ToString();
return URL;
}
}
}
IE6和IE7的访问方式不太一样,IE7要改为:
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices; //操作Win32API必须要引入的命名空间
namespace IEManipulation
{
public class IEManipulation
{
[DllImport("User32.dll")] //User32.dll是Windows操作系统的核心动态库之一
static extern int FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32.dll")]
static extern int FindWindowEx(int hwndParent, int hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll")]
static extern int GetWindowText(int hwnd, StringBuilder buf, int nMaxCount);
[DllImport("User32.dll")]
static extern int SendMessage(int hWnd, int Msg, int wParam, StringBuilder lParam);
const int WM_GETTEXT = 0x000D; //获得文本消息的16进制表示
/// <summary>
/// Get the URL of the current opened IE
/// </summary>
public static string GetURL()
{
int parent = FindWindow("IEFrame", null);
int child = FindWindowEx(parent, 0, "WorkerW", null);
child = FindWindowEx(child, 0, "ReBarWindow32", null);
child = FindWindowEx(child, 0, "ComboBoxEx32", null);
child = FindWindowEx(child, 0, "ComboBox", null);
child = FindWindowEx(child, 0, "Edit", null); //通过SPY++获得地址栏的层次结构,然后一层一层获得
StringBuilder buffer = new StringBuilder(1024);
//child表示要操作窗体的句柄号
//WM_GETTEXT表示一个消息,怎么样来驱动窗体
//1024表示要获得text的大小
//buffer表示获得text的值存放在内存缓存中
int num = SendMessage(child, WM_GETTEXT, 1024, buffer);
string URL = buffer.ToString();
return URL;
}
}
}
IE6和IE7的访问方式不太一样,IE7要改为:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices; //操作Win32API必须要引入的命名空间
namespace IEManipulation
{
public class IEManipulation
{
[DllImport("User32.dll")] //User32.dll是Windows操作系统的核心动态库之一
static extern int FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32.dll")]
static extern int FindWindowEx(int hwndParent, int hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll")]
static extern int GetWindowText(int hwnd, StringBuilder buf, int nMaxCount);
[DllImport("User32.dll")]
static extern int SendMessage(int hWnd, int Msg, int wParam, StringBuilder lParam);
const int WM_GETTEXT = 0x000D; //获得文本消息的16进制表示
/// <summary>
/// Get the URL of the current opened IE
/// </summary>
public static string GetURL()
{
int parent = FindWindow("IEFrame", null);
int child = FindWindowEx(parent, 0, "WorkerW", null);
child = FindWindowEx(child, 0, "ReBarWindow32", null);
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices; //操作Win32API必须要引入的命名空间
namespace IEManipulation
{
public class IEManipulation
{
[DllImport("User32.dll")] //User32.dll是Windows操作系统的核心动态库之一
static extern int FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32.dll")]
static extern int FindWindowEx(int hwndParent, int hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll")]
static extern int GetWindowText(int hwnd, StringBuilder buf, int nMaxCount);
[DllImport("User32.dll")]
static extern int SendMessage(int hWnd, int Msg, int wParam, StringBuilder lParam);
const int WM_GETTEXT = 0x000D; //获得文本消息的16进制表示
/// <summary>
/// Get the URL of the current opened IE
/// </summary>
public static string GetURL()
{
int parent = FindWindow("IEFrame", null);
int child = FindWindowEx(parent, 0, "WorkerW", null);
child = FindWindowEx(child, 0, "ReBarWindow32", null);
child = FindWindowEx(child, 0, ("Address Band Root"), null);
child = FindWindowEx(child, 0, "ComboBoxEx32", null);
child = FindWindowEx(child, 0, "ComboBox", null);
child = FindWindowEx(child, 0, "Edit", null); //通过SPY++获得地址栏的层次结构,然后一层一层获得
StringBuilder buffer = new StringBuilder(1024);
//child表示要操作窗体的句柄号
//WM_GETTEXT表示一个消息,怎么样来驱动窗体
//1024表示要获得text的大小
//buffer表示获得text的值存放在内存缓存中
int num = SendMessage(child, WM_GETTEXT, 1024, buffer);
string URL = buffer.ToString();
return URL;
}
}
}
child = FindWindowEx(child, 0, "ComboBoxEx32", null);
child = FindWindowEx(child, 0, "ComboBox", null);
child = FindWindowEx(child, 0, "Edit", null); //通过SPY++获得地址栏的层次结构,然后一层一层获得
StringBuilder buffer = new StringBuilder(1024);
//child表示要操作窗体的句柄号
//WM_GETTEXT表示一个消息,怎么样来驱动窗体
//1024表示要获得text的大小
//buffer表示获得text的值存放在内存缓存中
int num = SendMessage(child, WM_GETTEXT, 1024, buffer);
string URL = buffer.ToString();
return URL;
}
}
}
本文介绍了一种使用C#调用Win32 API的方法,来获取当前打开的IE浏览器窗口的URL。该方法适用于IE6和IE7,并展示了针对不同版本IE浏览器的窗口类名差异。
1406

被折叠的 条评论
为什么被折叠?



