.net (c#)winform中操作ie窗口元素
来源:互联网 发布:java开发属于哪个部门 编辑:程序博客网 时间:2024/05/17 22:17
用ie打开aaa.html
运行 这个winform程序. 在winform的textbox里随便输入一串字符, 点击button, ie窗口中input id="t1" 的值被改为 winform中textbox的值.
增加引用dll:
mshtml
c:/windows/system32/SHDocVw.dll
运行 这个winform程序. 在winform的textbox里随便输入一串字符, 点击button, ie窗口中input id="t1" 的值被改为 winform中textbox的值.
增加引用dll:
mshtml
c:/windows/system32/SHDocVw.dll
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using mshtml;
namespace testWinIE
...{
public partial class Form1 : Form
...{
/**//*[DllImport("user32.dll")]
public static extern IntPtr FindWindow
(string sClassName,
string sWindowTitle);
[DllImport("user32.dll")]
public static extern IntPtr FindWindowEx(
IntPtr hParent,
IntPtr hNext,
string sClassName,
string sWindowTitle);
[DllImport("user32.dll")]
public static extern int GetWindowTextLength(IntPtr hTxt); // handle to window or control with text
[DllImport("user32.dll")]
public static extern int GetWindowText(IntPtr hWnd, // handle to window or control with text
string lpString, // address of buffer for text
int nMaxCount // maximum number of characters to copy
);*/
/**//*开始我试图使用调用api的方式来使用c# winform程序与ie窗口进行通信, 是成功了,可以控制ie窗口和标准windows控件,
* 但是有个问题,控制不了文档区的元素,浪费了我好多时间。
* 我们做windows程序知道,windows下面都是窗口,一个文本框 classname为 Edit,一个button,等等等等.
* 而在ie的文档区,它却不是,害我用了很多方法浪费时间,包括把16进制编号都用上了,就是查不到。
* ie中的(准确地说,应该是web page中的htmlelement,比如input)它们不是标准windows控件
* 最后查资料到ms,得到了微软件的实现方法,走了弯路,历时近5个小时,解决了这个问题.
* fcuandy 2007-12-18
*/
public Form1()
...{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
...{
}
public void test()
...{
//IntPtr ieWin = FindWindow("IEFrame", "aaa - Microsoft Internet Explorer");
//IntPtr txtPtr = IntPtr.Zero;
//if (!ieWin.Equals(IntPtr.Zero))
//{
// MessageBox.Show("window has been found");
//txtPtr = FindWindowEx(ieWin, IntPtr.Zero, "IEFrame", null);
//txtPtr = FindWindowEx(ieWin,IntPtr.Zero, "Shell DocObject View", "");
//txtPtr = FindWindowEx(txtPtr,IntPtr.Zero , "Internet Explorer_Server", "");
//if (!txtPtr.Equals(IntPtr.Zero))
//{
/**//*
获得所有ie进程。因为windows下资源窗口使用ie内核,所以不光是我们通常指的web浏览器,它还包括你打开我的电脑这样的窗口
*/
SHDocVw.ShellWindows sws = new SHDocVw.ShellWindows();
//SHDocVw.InternetExplorer iw = SHDocVw. new SHDocVw.InternetExplorer();
/**//*遍历ie进程*/
foreach (SHDocVw.InternetExplorer iw in sws)
...{
//MessageBox.Show(iw.LocationName);
/**//*
如果使用的窗口中正打开的是我的aaa.html
* 我这里就没有对窗口类型进行检测了,因为我的aaa.html它一定是ie打开的。
*/
if (iw.LocationName == "aaa.html")
...{
//MessageBox.Show(iw.LocationName);
//HtmlDocument doc = iw.get (HtmlDocument)iw.Document;
//MessageBox.Show(doc.DomDocument.ToString());
mshtml.HTMLDocument doc = (mshtml.HTMLDocument)iw.Document;
//MessageBox.Show(((HTMLInputTextElement)doc.getElementById("t1")).value);
//ihtmldocument2接口.可以查看msdn6. 做法有点类似于js对页面htmlelement操作了。
((HTMLInputTextElement)doc.getElementById("t1")).value = textBox1.Text;
}
}
//int txtLen = GetWindowTextLength(txtPtr);
//MessageBox.Show(winTxt);
}
private void button1_Click(object sender, EventArgs e)
...{
test();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using mshtml;
namespace testWinIE
...{
public partial class Form1 : Form
...{
/**//*[DllImport("user32.dll")]
public static extern IntPtr FindWindow
(string sClassName,
string sWindowTitle);
[DllImport("user32.dll")]
public static extern IntPtr FindWindowEx(
IntPtr hParent,
IntPtr hNext,
string sClassName,
string sWindowTitle);
[DllImport("user32.dll")]
public static extern int GetWindowTextLength(IntPtr hTxt); // handle to window or control with text
[DllImport("user32.dll")]
public static extern int GetWindowText(IntPtr hWnd, // handle to window or control with text
string lpString, // address of buffer for text
int nMaxCount // maximum number of characters to copy
);*/
/**//*开始我试图使用调用api的方式来使用c# winform程序与ie窗口进行通信, 是成功了,可以控制ie窗口和标准windows控件,
* 但是有个问题,控制不了文档区的元素,浪费了我好多时间。
* 我们做windows程序知道,windows下面都是窗口,一个文本框 classname为 Edit,一个button,等等等等.
* 而在ie的文档区,它却不是,害我用了很多方法浪费时间,包括把16进制编号都用上了,就是查不到。
* ie中的(准确地说,应该是web page中的htmlelement,比如input)它们不是标准windows控件
* 最后查资料到ms,得到了微软件的实现方法,走了弯路,历时近5个小时,解决了这个问题.
* fcuandy 2007-12-18
*/
public Form1()
...{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
...{
}
public void test()
...{
//IntPtr ieWin = FindWindow("IEFrame", "aaa - Microsoft Internet Explorer");
//IntPtr txtPtr = IntPtr.Zero;
//if (!ieWin.Equals(IntPtr.Zero))
//{
// MessageBox.Show("window has been found");
//txtPtr = FindWindowEx(ieWin, IntPtr.Zero, "IEFrame", null);
//txtPtr = FindWindowEx(ieWin,IntPtr.Zero, "Shell DocObject View", "");
//txtPtr = FindWindowEx(txtPtr,IntPtr.Zero , "Internet Explorer_Server", "");
//if (!txtPtr.Equals(IntPtr.Zero))
//{
/**//*
获得所有ie进程。因为windows下资源窗口使用ie内核,所以不光是我们通常指的web浏览器,它还包括你打开我的电脑这样的窗口
*/
SHDocVw.ShellWindows sws = new SHDocVw.ShellWindows();
//SHDocVw.InternetExplorer iw = SHDocVw. new SHDocVw.InternetExplorer();
/**//*遍历ie进程*/
foreach (SHDocVw.InternetExplorer iw in sws)
...{
//MessageBox.Show(iw.LocationName);
/**//*
如果使用的窗口中正打开的是我的aaa.html
* 我这里就没有对窗口类型进行检测了,因为我的aaa.html它一定是ie打开的。
*/
if (iw.LocationName == "aaa.html")
...{
//MessageBox.Show(iw.LocationName);
//HtmlDocument doc = iw.get (HtmlDocument)iw.Document;
//MessageBox.Show(doc.DomDocument.ToString());
mshtml.HTMLDocument doc = (mshtml.HTMLDocument)iw.Document;
//MessageBox.Show(((HTMLInputTextElement)doc.getElementById("t1")).value);
//ihtmldocument2接口.可以查看msdn6. 做法有点类似于js对页面htmlelement操作了。
((HTMLInputTextElement)doc.getElementById("t1")).value = textBox1.Text;
}
}
//int txtLen = GetWindowTextLength(txtPtr);
//MessageBox.Show(winTxt);
}
private void button1_Click(object sender, EventArgs e)
...{
test();
}
}
}
aaa.html
<html id="aa">
<title>aaa</title>
<body>
<input id="t1" name="txtTest" />
</body>
</html>
- .net (c#)winform中操作ie窗口元素
- winform C#.NET注册表操作类
- C#.NET的Winform程序中使用ADO.NET操作SQL Server数据库
- 在IE中使用VS.net WinForm控件
- 在IE中使用VS.net WinForm控件
- 在IE中使用VS.net WinForm控件
- [转]在IE中使用VS.net WinForm控件
- 在IE中使用VS.net WinForm控件
- 在IE中使用VS.net WinForm控件
- [C#.NET][WinForm] Windows 窗口讯息接收 - WndProc 的使用
- winform中使用IE
- C#WinForm中打开IE并打开指定的网站
- .net中用winform调用ie
- [VB.NET]winform中,怎样取消子窗口的Icon??
- asp.net(c#)操作html元素
- .NET固定WinForm窗口大小
- IE中使用WinForm控件
- IE中使用WinForm控件
- 记得那份好
- Asp.net 2.0自定义控件开发相关的几种嵌入资源解决方案
- Asp.net 2.0 自定义控件开发[实现GridView多行表头固定表体滚动效果][示例代码下载]
- 珍惜自已的爱, 世界上没有後悔药 [转载收藏]
- C#农历~源代码(一)
- .net (c#)winform中操作ie窗口元素
- 她为了8块5毛钱……[转载]
- 书讯 -- Microsoft Office SharePoing Server 2007 新一代企业 Web 解决方案
- Asp.net 2.0 自定义控件开发专题[详细探讨页面状态(视图状态和控件状态)机制及其使用场景](示例代码下载)
- 珍惜自已的爱, 世界上没有後悔药 [转载收藏]
- 人力资源的统统都来学习一下
- [MVP] 热烈祝贺!您已经获得连任 Microsoft MVP 奖励
- Asp.net 2.0 自定义控件开发[创建自定义HeaderRow的GridView控件][示例代码下载]
- Asp.net 2.0控件开发相关调试(JavaScript调试和自定义控件设计时调试)