Solved

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

Posted on 2003-10-28
16
218 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

831 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