我想出这样一个思路:你先用SPY++找到IE的浏览器窗口类名,然后用FindWindowsEx获得它的窗口句柄,这样就可以获得一个CHtmlView类指针。然后你可以使用IWebBrowser2接口的get_LocationURL(BSTR *pbstrLocationURL);方法获取当前的URL,地址的变化可以通过BeforeNavigate2 事件获得。你试一下,看是否可行。
解决方案 »
- C#问题。。求高手帮忙看看
- 急!!100分咨询各位方案的可行性
- toolstripmenuitem控件中的图片绘制的原理是什么。
- 我要一个变量来动态备份DataGridView里某一列的内容,用string[]好像不能动态改变个数,用DataRow又好像没有构造函数,到底用什么好?
- 格式化问题
- SqlConnection小问题
- asp.net中gridview导出excel 大侠请进
- 最菜的菜鸟学c#,从何处着手(本人没语言基础)!!!是个难题!~~~:)
- 请问如何将两个arraylist内容相加?
- DataGrid如何做分页????
- 大家帮帮忙,为什么我用 Dns.GetHostByAddress 只能访问内网?
- 新闻系统问题求助
我以前试过,IE报错,或者自己的程式报错的几率远大于正常情况,天知道为什么。
using System.Diagnostics ;
using System.Runtime.InteropServices;
using System.Collections;
using System.Net .Sockets ;
namespace fupip
{
public class geturl
{
public geturl()
{
}
[DllImport("user32.dll")]
public static extern int GetClassName(int hwnd, byte[] lpClassName,Int32[] nMaxCount);
[DllImport("user32.dll")]
public static extern int GetWindow(int hwnd,int wCmd); [DllImport("user32.dll")]
public static extern int SendMessage(int hwnd,int wMsg,int wParam, byte[] lParam);
public const int GW_CHILD = 5;
public const int GW_HWNDNEXT = 2;
public const int WM_GETTEXT =13;
public const int WM_GETTEXTLENGTH = 14; public static string[] GetIEURL(string args)
{
string[] urls;
Process[] ps =Process.GetProcessesByName(args);
urls=new string[ps.Length];
for(int i=0;i<ps.Length;i++)
{
int hwndIE=(int)ps[i].MainWindowHandle; int hwndEdit=getkid(hwndIE);
urls[i]= gettext(hwndEdit);
}
return urls;
}
static int getkid(int hwnd)
{
byte[] str=new byte[100];
Int32[] len=new Int32[1];
int hwndkid=0;
string classname;
int handle=0;
len[0]=100;
ArrayList al=new ArrayList();
GetClassName(hwnd,str,len);
classname=System.Text.Encoding.ASCII.GetString(str);
if(classname.Substring(0,4)=="Edit")
{
handle= hwnd;
}
hwndkid=GetWindow(hwnd,GW_CHILD);
while(hwndkid!=0)
{
al.Add(hwndkid);
hwndkid=GetWindow(hwndkid,GW_HWNDNEXT);
}
foreach(int kid in al)
{
int i=getkid(kid);
if(i!=0)
handle=i;
} return handle;
}
static string gettext(int hwnd)
{
int txtlen;
byte[] txt;
string text;
txtlen = SendMessage(hwnd, WM_GETTEXTLENGTH, 0, new byte[]{0});
if(txtlen==0)
return "";
txt=new byte[txtlen+1];
txtlen = SendMessage(hwnd, WM_GETTEXT, txtlen+1, txt);
text=System.Text.Encoding.Default.GetString(txt);
return text; }
}}
但是有个问题,有时候IE的进程有2个,但是我的页面却有5个,请教,这个问什么?而且我不断的重新获取,有时候得到的URL地址却不一样,请教,这个该怎么处理?