C#获得IHTMLDocument2并且创建链接的单击事件

private void button4_Click(object sender, EventArgs e)
{
IntPtr ParenthWndldsl = new IntPtr(0);
IntPtr EdithWnd = new IntPtr(0);
Thread.Sleep(200);
ParenthWndldsl = FindWindow(null, "电话服务平台1.01 - [欢迎使用]");

if (!ParenthWndldsl.Equals(IntPtr.Zero))
{
//得到User Name这个子窗体,并设置其内容
EdithWnd = FindWindowEx(ParenthWndldsl, IntPtr.Zero, "MDIClient", "");
IntPtr e1 = FindWindowEx(EdithWnd, IntPtr.Zero, "TForm_welcome", "欢迎使用");
IntPtr e2 = FindWindowEx(e1, IntPtr.Zero, "Shell Embedding", "");
IntPtr e3 = FindWindowEx(e2, IntPtr.Zero, "Shell DocObject View", "");
IntPtr e4 = FindWindowEx(e3, IntPtr.Zero, "Internet Explorer_Server", "");


mshtml.IHTMLDocument2 id = getIEWindowDocument(int.Parse(e4.ToString()));//上边代码是获得句柄的
IHTMLElementCollection links = id.links;
foreach (mshtml.IHTMLElement k in links)
{
// mshtml.HTMLAnchorElementClass ks = k.GetType();
// string aa = ((mshtml.HTMLAnchorElementClass)(k)).href;
string aaa = k.outerHTML;//这个是指定单击
if (aaa=="<AREA title=电话受理 href="+"\"$dhsl\""+" shape=rect coords=188,137,251,210>")
{
k.click();
}
//mshtml.HTMLAnchorElementClass ks = (mshtml.HTMLAnchorElementClass)(k);

}


}

}

class Win32API
{
[DllImport("User32.dll")] //User32.dll是Windows操作系统的核心动态库之一
public static extern int FindWindow(string lpClassName, string lpWindowName);


[DllImport("user32", EntryPoint = "RegisterWindowMessage")]
public static extern int RegisterWindowMessage(
string lpString
);

[DllImport("user32", EntryPoint = "SendMessage")]
public static extern int SendMessage(
int hwnd,
int wMsg,
int wParam,
ref int lParam
);

[DllImport("OLEACC.DLL", EntryPoint = "ObjectFromLresult")]
public static extern int ObjectFromLresult(
int lResult,
ref System.Guid riid,
int wParam,
[System.Runtime.InteropServices.MarshalAs(UnmanagedType.Interface), System.Runtime.InteropServices.In, System.Runtime.InteropServices.Out]ref System.Object ppvObject
//注意这个函数ObjectFromLresult的声明.
);
}



public mshtml.IHTMLDocument2 getIEWindowDocument(int jubing)
{
System.Object domObject = new System.Object();
int tempInt = 0;
System.Guid guidIEDocument2 = new Guid(); //应该是IHTMLDocument2的接口id
int WM_Html_GETOBJECT = Win32API.RegisterWindowMessage("WM_Html_GETOBJECT");


//开始查找指定的ie窗体
int hwnd = jubing;

int W = Win32API.SendMessage(hwnd, WM_Html_GETOBJECT, 0, ref tempInt);
int lreturn = Win32API.ObjectFromLresult(W, ref guidIEDocument2, 0, ref domObject);
mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)domObject;

//MessageBox.Show(doc.toString());
MessageBox.Show("到这里2");
MessageBox.Show(doc.url);
MessageBox.Show(doc.title);

return doc;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要获取IWebBrowser2中的元素id,可以使用以下步骤: 1. 通过IWebBrowser2::get_Document方法获取IHTMLDocument2接口。 2. 使用IHTMLDocument2::get_all方法获取IHTMLElementCollection接口,并使用它来搜索指定的元素。 3. 在IHTMLElementCollection中使用IHTMLElementCollection::item方法获取指定项的IHTMLElement接口。 4. 在IHTMLElement中使用IHTMLElement::get_id方法获取元素的id属性。 下面是一个示例代码: ``` IWebBrowser2* pWebBrowser = // 获取WebBrowser控件指针 IHTMLDocument2* pHTMLDoc = NULL; pWebBrowser->get_Document((IDispatch**)&pHTMLDoc); // 获取所有元素 IHTMLElementCollection* pAllElements = NULL; pHTMLDoc->get_all(&pAllElements); // 搜索指定元素 VARIANT vIndex; vIndex.vt = VT_I4; vIndex.lVal = 0; BSTR bstrTagName = L"input"; BSTR bstrType = L"text"; BSTR bstrName = L"name"; IHTMLElement* pInputElement = NULL; while (pAllElements->item(bstrTagName, vIndex, (IDispatch**)&pInputElement) == S_OK) { // 检查元素的type和name属性 CComBSTR bstrTypeValue; CComBSTR bstrNameValue; pInputElement->get_type(&bstrTypeValue); pInputElement->get_name(&bstrNameValue); if (bstrTypeValue == bstrType && bstrNameValue == bstrName) { // 获取元素的id属性 CComBSTR bstrIdValue; pInputElement->get_id(&bstrIdValue); // 在这里使用元素的id属性 break; } // 下一个元素 vIndex.lVal++; pInputElement->Release(); } pAllElements->Release(); ``` 注意,在使用完IHTMLElement接口后必须调用Release方法来释放它。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值