Setting focus to an IWebBrowser2

This is a discussion on Setting focus to an IWebBrowser2 within the Windows Programming forums, part of the Platform Specific Boards category; Good day! Can anyone please tell me how to set the focus to my Web Browser? I have an IWebBrowser2 ...

  1. #1
    Registered User kimyat13's Avatar
    Join Date
    Oct 2010
    Posts
    9

    Setting focus to an IWebBrowser2

    Good day!

    Can anyone please tell me how to set the focus to my Web Browser?

    I have an IWebBrowser2 in my app hosted inside a CAxWindow.

    The problem is that when the application has loaded and the browser navigates to the start page, the user must click the mouse in the browser control window in order to get mousewheel scrolling and keyboard navigation to work.

    I'd like to have this done automatically.

    I need to make it so that when I click a button in my app and I send the pagedown and pageup key events or other keyboard events, the browser is focused so it receives them. No need to click at the start.

    SetFocus has not worked, I'm not sure I got the DoVerb in IOleObject right, but I tried that one too.
    Last edited by kimyat13; 10-19-2010 at 09:11 PM. Reason: thread solved.. no "?" needed.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I've never used exactly what you are using, however SetFocus() should work as long as you know the window handle of the control. If it's in a dialog you can use GetDlgItem() to fetch the handle for you.

    One way to make the mousewheel and keyboard follow the current cursor window is to subclass the control and track mouse events (WM_MOUSEMOVE) to bring focus to whatever control is receiving mousemove events. It's kind of a fake hottracking scheme that usually works quite well.

  3. #3
    Registered User kimyat13's Avatar
    Join Date
    Oct 2010
    Posts
    9
    all i need are keyboard inputs. but after loading the app, the browser needs to be clicked to be focused. it's embedded in a windowless control, part of the gui for the app i'm working on. i tried SetFocus by getting the handle, but it didn't work. The mouseclick is still needed. after the first mouseclick however, everything becomes fine, the browser always catches my keyboard events. i just need to get rid of the initial mouse click.

    Thanks for the reply, you gave me the idea of trying to fake sending a mouse event to the browser. (hahaha once i know how to do that.) I will keep trying today.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You could do that, but it's kinda "not recommended"... but hey it's code... there are rules?

    Looking at the interface docs it is possible to get the HWND of the browser window...

    http://msdn.microsoft.com/en-us/libr...27(VS.85).aspx
    http://msdn.microsoft.com/en-us/libr...(v=VS.85).aspx

    Did you try SetForegroundWindow() on that control?
    How about sending it a WM_ACTIVATE message?
    Or perhaps a WM_LBUTTONDOWN message?

    A fake mouse event can be created with, well, mouse_event(). It's kernel code but it does work.
    The same with keyboards keybd_event() for keystrokes.
    Last edited by CommonTater; 10-19-2010 at 09:03 PM.

  5. #5
    Registered User kimyat13's Avatar
    Join Date
    Oct 2010
    Posts
    9
    wow..

    thanks so much for the idea. yahhh it is a lame work-around, but it works anyhow. i don't know if there are rules against stupid code. but i tried eveything i could find. spent an entire weekend looking for it. no other solution worked. =(

    so here, i sent the mouse event to the CAxWindow after Navigate() and there was no longer a need to click with the mouse. keyboard works then.


    Code:
    m_HostWnd.SendMessageToDescendants(WM_LBUTTONDOWN, 0, NULL);
    /* m_HostWnd is CAxWindow, i just placed NULL at the last part, i think it's supposed to be the mouse position.. */
    thanks

    i also used keybd_event... thanks
    Code:
    switch(scrollType)
    	{
    	case StepDown:
    		keybd_event(VK_DOWN, 0xD0, 0,0);
    		Sleep(100);
    		keybd_event(VK_DOWN, 0xD0, KEYEVENTF_KEYUP,0);
    		break;
    	case StepUp:
    		keybd_event(VK_UP, 0xC8, 0,0);
    		Sleep(100);
    		keybd_event(VK_UP, 0xC8, KEYEVENTF_KEYUP,0);
    		break;
    	case PageDown:
    		keybd_event(VK_NEXT, 0xD1, 0,0);
    		Sleep(100);
    		keybd_event(VK_NEXT, 0xD1, KEYEVENTF_KEYUP,0);
    		break;
    	case PageUp:
    		keybd_event(VK_PRIOR, 0xC9, 0,0);
    		Sleep(100);
    		keybd_event(VK_PRIOR, 0xC9, KEYEVENTF_KEYUP,0);
    		break;
    	case ScrollBottom:
    		keybd_event(VK_END, 0xCF, 0,0);
    		Sleep(100);
    		keybd_event(VK_END, 0xCF, KEYEVENTF_KEYUP,0);
    		break;
    	case ScrollTop:
    		keybd_event(VK_HOME, 0xC7, 0,0);
    		Sleep(100);
    		keybd_event(VK_HOME, 0xC7, KEYEVENTF_KEYUP,0);
    		break;
            }
    Last edited by kimyat13; 10-19-2010 at 09:07 PM.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... that was fast!

    If you do WM_LBUTTONDOWN don't forget the WM_LBUTTONUP otherwise you may find yourself in drag and drop mode by moving the mouse.

    In your scroll type... You would be better sending WM_CHAR to the window.

    keybd_event is global. If your app isn't topmost... it could cause some strange behaviours in other programs.
    I have even used it to control other programs remotely.

  7. #7
    Registered User kimyat13's Avatar
    Join Date
    Oct 2010
    Posts
    9
    i wasn't able to think of that.

    thanks a million. hhahahaha so that's why my test application (to test this UI) suddenly changes mode when it's on top. )

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Kimy... if you want to have a bit of fun someday, you can try a little API level code prank...

    Create a windowless program that periodically scans for "Notepad" using the FindWindow() function... When it opens take it's window handle, put it on top with SetForegroundWindow() and slowly send keybd_event messages for the character sequence... "Don't ever open this program again" and wait for people's jaws to drop.

    Glad you got it working!

  9. #9
    Registered User kimyat13's Avatar
    Join Date
    Oct 2010
    Posts
    9
    added the LButtonUp and used this instead...
    Code:
    switch(scrollType)
    	{
    	case StepDown:
    		m_HostWnd.SendMessageToDescendants(WM_KEYDOWN, VK_DOWN); 
    		m_HostWnd.SendMessageToDescendants(WM_KEYUP, VK_DOWN); 
    		break;
    	case StepUp:
    		m_HostWnd.SendMessageToDescendants(WM_KEYDOWN, VK_UP); 
    		m_HostWnd.SendMessageToDescendants(WM_KEYUP, VK_UP); 
    		break;
    	case PageDown:
    		m_HostWnd.SendMessageToDescendants(WM_KEYDOWN, VK_NEXT); 
    		m_HostWnd.SendMessageToDescendants(WM_KEYUP, VK_NEXT); 
    		break;
    	case PageUp:
    		m_HostWnd.SendMessageToDescendants(WM_KEYDOWN, VK_PRIOR); 
    		m_HostWnd.SendMessageToDescendants(WM_KEYUP, VK_PRIOR); 
    		break;
    	case ScrollBottom:
    		m_HostWnd.SendMessageToDescendants(WM_KEYDOWN, VK_END); 
    		m_HostWnd.SendMessageToDescendants(WM_KEYUP, VK_END); 
    		break;
    	case ScrollTop:
    		m_HostWnd.SendMessageToDescendants(WM_KEYDOWN, VK_HOME); 
    		m_HostWnd.SendMessageToDescendants(WM_KEYUP, VK_HOME); 
    		break;
    	}
    thanks commontater.


    hahahahah
    i will DEFINITELY try that and scare the hell out of everyone who touches my computer.
    Last edited by kimyat13; 10-19-2010 at 10:23 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Setting Focus in Dialog
    By keira in forum Windows Programming
    Replies: 0
    Last Post: 01-21-2009, 06:40 AM
  2. Setting window focus
    By scrub05 in forum C++ Programming
    Replies: 1
    Last Post: 01-12-2005, 11:59 AM
  3. Setting a control to have startup focus
    By bennyandthejets in forum C# Programming
    Replies: 1
    Last Post: 09-19-2004, 09:14 PM
  4. Setting focus
    By tyouk in forum Windows Programming
    Replies: 10
    Last Post: 11-04-2003, 10:12 PM
  5. Setting focus in an edit box
    By nasec in forum Windows Programming
    Replies: 2
    Last Post: 04-28-2003, 12:33 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21