Solved

Which app has the focus??

Posted on 1998-06-04
4
171 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
ID: 1462539
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
ID: 1462540
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
ID: 1462541
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
ID: 1462542
Glad I could help.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

830 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