I have a web browser control which has navigated to a given page and is ready. Now I want to bring up the Internet explorer find dialogbox whenever the user clicks a specific button on my page. The user can already bring up the find dialog box by clicking 'ctrl+f', but I want a separate button for this action too.
I have found this: http://support.microsoft.com/kb/329014
But It have the following issues with it:
The page says that this method might not work on later versions of Internet explorer:
Warning This sample uses an undocumented command-group GUID that is subject to change in the future. Although this sample was tested to work correctly with Internet Explorer 6 a开发者_开发问答nd earlier, there is no guarantee that these techniques will continue to work successfully in future versions.
I cant compile the code. I have added the references described in the page but I get errors telling AxSHDocVw namespace could not be found.
I found a solution myself: If the web browser control name is "web", you can send the "Crtl+f" key to it with the following code:
web.Focus();
SendKeys.Send("^f");
I tested this and it brought up the find dialog box on Internet explorer 8.
I'm using Windows7 and .Net4.0 / WPF (not Win Forms). The solution with
web.Focus();
SendKeys.Send("^f");
didn't work for me. First, the send command throws System.InvalidOperationException (saying that my application does not process event and that I should use SendKeys.SendWait()). Secondly, SendKeys.SendWait("^f") just does nothing.
My solution requires to focus the document window instead of the browser window:
System.Windows.Controls.WebBrowser HtmlView; //from xaml
HTMLDocument HtmlDoc { get { return (HTMLDocument)HtmlView.Document; } }
HtmlDoc.parentWindow.focus(); //when focusing this window sending Ctr-F has an effect
Next I use keybd_event() to send the key message
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags,
UIntPtr dwExtraInfo);
byte vKey_f = 0x46;
byte vKey_ctr = 0x11;
byte f_down = 0x21;
byte f_up = 0xa1;
byte ctrl_down = 0x1d;
byte ctrl_up = 0x9d;
byte event_keyup = 0x0002;
keybd_event(vKey_ctr, ctrl_down, 0, UIntPtr.Zero); // Ctrl Press
keybd_event(vKey_f, f_down, 0, UIntPtr.Zero); // f Press
keybd_event(vKey_f, f_up, event_keyup, UIntPtr.Zero); // f Release
keybd_event(vKey_ctr, ctrl_up, event_keyup, UIntPtr.Zero); // Ctrl Release
Why "^f" does not work in my case - I don't know. But keybd_event() works fine although msdn (http://msdn.microsoft.com/en-us/library/windows/desktop/ms646304(v=vs.85).aspx) states "This function has been superseded. Use SendInput instead.". I tried SendInput() but couldn't see an effect.
Further info to keybd_event():
- Scan codes: http://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-923143f3456c/scancode.doc
- Virtual Key Codes: http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx
精彩评论