I checked a lot of related information on the Internet, but many of them are not usable. Let's summarize the previous experiences.
1. Obtain the iwebbrowser2 interface of IE and then the ihtmldocument2 interface.
Int iehwnd = find0000wa ("ieframe", null );
// Initialize all IE Windows
Ishellwindows Sw = new shellwindowsclass ();
// Round all IE Windows
For (INT I = Sw. Count-1; I> = 0; I --)
{
// Obtain the iwebbrowser2 object for each IE
Iwebbrowser2 iwb2 = Sw. Item (I) As iwebbrowser2;
// Check whether the obtained handle matches the window handle.
If (iwb2.hwnd = iehwnd)
{
// Assign a value when the search is successful
Document = (htmldocumentclass) iwb2.document;
// Perform operations on the webpage
Document. getelementbyid ("kW"). setattribute ("value", "it is a test", 1 );
Document. getelementbyid ("Su"). Click ();
}
}
2. obtain the corresponding ihtmldocument2 interface through the objectfromlresult method of accessibility. However, the handle passed in is the handle that IE shows the HTML window.
[Dllimport ("user32.dll", entrypoint = "registerwindowmessagea")]
Private Static extern int registerwindowmessage (string lpstring );
[Dllimport ("user32.dll", entrypoint = "sendmessagetimeouta")]
Private Static extern int sendmessagetimeout (intptr hwnd, int MSG, int wparam, int lparam, int fuflags, int utimeout, out int lpdwresult );
[Dllimport ("oleacc. dll")]
Private Static extern int objectfromlresult (INT lresult, ref guid riid, int wparam, ref ihtmldocument2 ppvobject );
Private const int smto_abortifhung = 0x2;
Private Static guid iid_ihtmldocument = new GUID ("626fc520-a41e-11cf-a731-00a0c9082637 ");
Private Static ihtmldocument2 getdocumentfromhandle (intptr success whandle)
{
Int lngmsg = 0;
Int lres;
If (! When whandle. Equals (intptr. Zero ))
{
Lngmsg = registerwindowmessage ("wm_html_getobject ");
If (lngmsg! = 0)
{
Sendmessagetimeout (when whandle, lngmsg, 0, 0, smto_abortifhung, 1000, out lres );
If (! (Bool) (lres = 0 ))
{
Int hR = objectfromlresult (lres, ref iid_ihtmldocument, 0, ref document );
If (bool) (document = NULL ))
{
Return document;
}
}
}
}
Return document;
}
Specific details can view http://www.xtremevbtalk.com/showthread.php? T = 295336