How do you trick a window into believing it is focused?

I am trying to send mouse clicks to a WebBrowser control inside a form using PostMessage() and I am having a pretty significant problem. What I'm trying to achieve is to simulate mouse clicks on this web browser while my form is minimized. Normally PostMessage() will work just fine, but it seems to work only while my form has focus. This leads me to believe that there are some checks to check if a particular website that I load in my WebBrowser control is located before it processes mouse events.

This is how I send clicks with my program:

 private void SendClick(Point location) { resetHandle = true; StringBuilder className = new StringBuilder(100); while (className.ToString() != "Internet Explorer_Server") { handle = GetWindow(handle, 5); // 5 == child GetClassName(handle, className, className.Capacity); //MessageBox.Show(className.ToString()); } IntPtr lParam = (IntPtr)((location.Y << 16) | location.X); IntPtr wParam = IntPtr.Zero; const uint downCode = 0x201; const uint upCode = 0x202; const uint moveCode = 0x200; PostMessage(handle, moveCode, wParam, lParam); //move mouse PostMessage(handle, downCode, wParam, lParam); // mousedown PostMessage(handle, upCode, wParam, lParam); // mouseup } 

This is what resetHandle does:

 private void timer3_Tick(object sender, EventArgs e) { if (resetHandle == true) { handle = webBrowser1.Handle; resetHandle = false; } } 

I'm not sure if there is a better way to send mouse events to the background window, and I'm open to any ideas. What I'm really asking is, if at all possible, for the window to show that it is in focus when it is actually minimized?

Any help at all would be greatly appreciated!

+4
source share
1 answer

Instead of keeping the window minimum, save it in its normal state (restored), but set its X or Y coordinate so that it is turned off.

If you want to give the user the illusion of minimization and recovery, use HwndSource.AddHook to view SC_MINIMIZE . In your HwndSourceHook handler HwndSourceHook move the window on or off the screen according to the pseudo-minimized state and set the handled parameter to true.

+1
source

All Articles