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:

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 "".
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 "".

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!

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

If I understand your question correctly,I don't think that is possible.
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.
You meen "StayOnTop" .....

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Stay on top will still steal the focus
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.
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.

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

You can try this dll:

or there is one here that costs $

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.
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
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!

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

Maybe I can get those points back?

Ask the mods in the Community support areas
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;en-us;180936
PAQed, with points refunded (500)

Friendly Neighbourhood Community Support Admin

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.