Solved

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

Posted on 2003-10-28
16
211 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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
 

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

773 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