Solved

Which app has the focus??

Posted on 1998-06-04
4
157 Views
Last Modified: 2010-05-03
Hi,

Can anyone send me the API example of how to find the current application that has the focus, I tried the other way around, ie: does my VB app still have the focus using the event lostfocus but it does not quite work the way I want.

Some help will be greatly appreciated.

Cheers
0
Comment
Question by:mmahdi
  • 3
4 Comments
 
LVL 1

Accepted Solution

by:
Sekans earned 80 total points
Comment Utility
mmahdi,
Try this:

Dim hWndCURR As Long
Dim WNDTEXT As String
hWndCURR = GetForegroundWindow()
TXTLEN = GetWindowTextLength(hWndCURR)
WNDTEXT = Space$(TXTLEN + 1)
TXTLEN = GetWindowText(hWndCURR, WNDTEXT, TXTLEN + 1)
MsgBox WNDTEXT

WNDTEXT is the window text of the current application

Regards,
Sekans
0
 
LVL 1

Expert Comment

by:Sekans
Comment Utility
mmahdi,
I apologize, the code I submitted will return the foreground window, perhaps a more appropriate code would be:
    hWndCURR = GetFocus()
this would go in place of:
    hWndCURR = GetForegroundWindow()

Regards,
Sekans
0
 
LVL 1

Author Comment

by:mmahdi
Comment Utility
Sekans,

Thanks for the code, the first answer somehow work, however I had to edit the code:

from: WNDTEXT = Space$(TXTLEN + 1)
to: WNDTEXT = Space$(TXTLEN)

the first line returned a string length of whatever the caption of the window is + 1 extra character. I am using the procedure in a timer control. If my app has not got the focus then activate some process.

Thanks
MMAHDI

0
 
LVL 1

Expert Comment

by:Sekans
Comment Utility
Glad I could help.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…

743 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

16 Experts available now in Live!

Get 1:1 Help Now