.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 
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();
        }

    }


}

 aaa.html

 

<html id="aa">
<title>aaa</title>
<body>
<input id="t1" name="txtTest" />
</body>
</html>

原创粉丝点击