How to prevent a 3rd party application losing the focus to my VB application.

I want to prevent a the program with the focus from losing the focus when my VB application is clicked upon, and I need to retrieve the location of the mouse click so that I can process it manually.

It is important that the application that has the focus does not receive a "lost focus" message, as this simple example should demonstrate:

Example:
If I have simple app with buttons to set the focus to Internet Explorer and send text to the address bar. One button sends "www", another sends ".experts-exchange.com".
When I click on the first button, I get "www" in the address bar.  When I click on the second button, the "www" is highlighted (when IE loses focus) and is replaced with ".experts-exchange.com".

This is just a trivial example of a wider problem.  I need to stop IE (or whatever the program is) from losing the focus, and I think that the way to do this is by preventing my app getting the focus.

I have battled with this for a while, but it is not an easy one.  Any API/subclassing experts out there, your help appreciated!

Barney
barneyhawesAsked:
Who is Participating?
 
LunchyCommented:
PAQed, with points refunded (500)

Lunchy
Friendly Neighbourhood Community Support Admin
0
 
vinnyd79Commented:
If I understand your question correctly,I don't think that is possible.
0
 
barneyhawesAuthor Commented:
I have seen it in other programs, such as the On-Screen Keyboard that comes with Windows XP, but perhaps this is not possible in VB.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
2AngelCommented:
You meen "StayOnTop" .....


Angel
0
 
ashooooCommented:
Stay on top will still steal the focus
0
 
barneyhawesAuthor Commented:
2Angel: I can get my program to stay on top - that is not the problem.

If you run the On-Screen Keyboard in XP (part of the accessability stuff), you will see that it does not "steal" the focus when you click on it.  This is what I want to reproduce.
0
 
ashooooCommented:
barneyhawes, not sure if I am right, but its worth a try...

When you click on the titlebar of the On screen keyboard, it still gets the focus. Only when you click on the buttons, it doesnt get the focus.

You might want to create a form, always on top, and create activex controls on it, with CanGetFocus property set to false. On clicking, say, buttonA, do a SendKeys("A"). If you somehow manage to keep the focus away from the form, the key will be sent to the other app.

Or try creating a Windowless ActiveX object and try playing around with it. Let me know if you reach anywhere with this and I might be able to help after that.

-Ashish
0
 
vinnyd79Commented:
I think you will need to install a system wide mouse hook using a DLL.you cannot create this type of DLL in VB.

You can try this dll:

http://www.mentalis.org/vbexamples/vbexample.php?vbexample=DSMOUSE&category=MISC

or there is one here that costs $
http://www.desaware.com/WindowsHooksL3.htm




0
 
barneyhawesAuthor Commented:
Thanks vinnyd79, that proved quite interesting - but it hasn't worked!

I used the first link (the free one) and to create a mouse hook and prevent my app getting mouse clicks.  

This worked to an extent - when I click on the application, it does not get the focus, but the app that does have the focus still loses it.

I used the GetForegroundWindow API call to determine that the foreground window handle is set to zero when I click on my app.
0
 
anaadoulCommented:
for the example you can make the vb to press end button
simply before sending the text do the following


sendkeys "{END}"


hope it works
---------------------------------
There Are 10 Kind OF People
Those Who Understands Binary AND Those Who Don't
0
 
barneyhawesAuthor Commented:
Thanks for the comment anaadoul.  It's good to see people are still reading this question!

The Internet Explorer example I gave was for an example of a more general problem.  There are other times when I cannot send the {END} key because it would have a different effect.

I have not been able to solve this problem yet, so if anyone else has any ideas please comment.  Or better still, write a sample VB app to demonstrate your method!

Barney
0
 
barneyhawesAuthor Commented:
Ok, I have solved this now (with the help of MS tech support).

Maybe I can get those points back?

Barney
0
 
ashooooCommented:
Ask the mods in the Community support areas
0
 
barneyhawesAuthor Commented:
The solution involves a CBT hook.

Details for general hooks on the microsoft site:

HOWTO: Position a MsgBox Using a Windows Hook Procedure
PSS ID Number: 180936
http://support.microsoft.com/default.aspx?scid=kb;en-us;180936
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.