When you use the webbrowser control to browse a Web page, you can jump to the page by pressing enter after entering the information,
There are two ways to let the program do this.
Method 1: Simulate submitting carriage return information
First, let the input box get the focus
Ihtmlelementcollection * pcoll = NULL;
HR = prightdoc-> get_all (& pcoll); // retrieves a set of webpage Elements
Variant index;
V_vt (& Index) = vt_i4;
V_i4 (& Index) = 0;
Variant varid = stringtovariant (strdocid); // the ID of the webpage element in the input box-analyzed by packet capture tools such as httpwatch
HR = pcoll-> item (varid, index, & Pdisp );
Ihtmlelement2 * pelem2;
HR = Pdisp-> QueryInterface (iid_ihtmlelement2, (void **) & pelem2 );
If (hR = s_ OK)
{
Pelem2-> focus (); // obtain the focus
Pelem2-> release ();
}
Send the carriage return message hwnd h_wnd =: findwindow (null, "program window title") to the webbrowser control; hwnd hshell =: find1_wex (h_wnd, null, "shell embedding ", null); hwnd hview =: find1_wex (hshell, null, "shell docobject View", null); hwnd hieserver =: find1_wex (hview, null, "Internet assumer_server ", null);: postmessage (hieserver, wm_keydown, vk_return, 0);: postmessage (hieserver, wm_keyup, vk_return, 0, reload pretranslatemessage ().
Method 2: Let webbrowser execute JavaScript Functions
First, use httpwatch and other web page listening tools to obtain the webpage code, as shown below:
Onkeypress = "If (window. event. keycode = 13) {Wade. event. stopevent (); Return jsfunctin ();}"
In this way, the JavaScript function name executed by pressing the Enter key is jsfunctin ()
Secondly, webbrowser calls execScript () and executes the JS function. The Code is as follows:
Ihtmlwindow2 * pwindow;
HR = prightdoc-> get_parentwindow (& pwindow );
Variant ret;
Ret. Vt = vt_empty;
BSTR bstrcode = sysallocstring (L "jsfunctin ()");
BSTR bstrlanguage = sysallocstring (L "JavaScript ");
HR = pwindow-> execScript (bstrcode, bstrlanguage, & RET );