Solved

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

Posted on 2003-10-28
16
202 Views
Last Modified: 2010-05-01
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
0
Comment
Question by:barneyhawes
  • 6
  • 3
  • 2
  • +3
16 Comments
 
LVL 28

Expert Comment

by:vinnyd79
ID: 9634905
If I understand your question correctly,I don't think that is possible.
0
 

Author Comment

by:barneyhawes
ID: 9634942
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
 
LVL 2

Expert Comment

by:2Angel
ID: 9634967
You meen "StayOnTop" .....


Angel
0
 
LVL 3

Expert Comment

by:ashoooo
ID: 9634987
Stay on top will still steal the focus
0
 

Author Comment

by:barneyhawes
ID: 9635047
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
 
LVL 3

Expert Comment

by:ashoooo
ID: 9635125
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
 
LVL 28

Expert Comment

by:vinnyd79
ID: 9635180
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:barneyhawes
ID: 9635489
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
 
LVL 1

Expert Comment

by:anaadoul
ID: 9805006
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
 

Author Comment

by:barneyhawes
ID: 9816590
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
 

Author Comment

by:barneyhawes
ID: 9949924
Ok, I have solved this now (with the help of MS tech support).

Maybe I can get those points back?

Barney
0
 
LVL 3

Expert Comment

by:ashoooo
ID: 9952055
Ask the mods in the Community support areas
0
 

Author Comment

by:barneyhawes
ID: 10175326
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
 
LVL 2

Accepted Solution

by:
Lunchy earned 0 total points
ID: 10197126
PAQed, with points refunded (500)

Lunchy
Friendly Neighbourhood Community Support Admin
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now