Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Identifying the active Windows window

Status
Not open for further replies.

ChrisRChamberlain

Programmer
Mar 23, 2000
3,392
GB
Hi all

The following example code, modified from George Tasker's IsRunning.prg, iterates through the various windows and processes and returns the windows title.

What I am trying to identify is the active Windows window from within VFP, the Windows window title also being required.

I assume there are other properties that could be returned within such an example loop that could determine which Windows window is currently active.

The WinAPI call GetActiveWindow() will only return the active window handle to the active window associated with the thread that calls the function, and therefore won't work in this situation.

CLEA

DECLARE INTEGER GetDesktopWindow IN Win32API
DECLARE INTEGER GetWindow IN Win32API ;
[tab]INTEGER hwnd ,;
[tab]INTEGER dflag
DECLARE INTEGER GetWindowText IN Win32API ;
[tab]INTEGER hwnd ,;
[tab]STRING @lptstr ,;
[tab]INTEGER cbmax

LOCAL lnhwnd ,;
[tab]lctitle_bar ,;
[tab]lntext_len

lnhwnd = GetDesktopWindow()
lnhwnd = GetWindow(lnhwnd,5)
lctitle_bar = []

DO WHILE !EMPTY(lnhwnd)
[tab]lctitle_bar = SPACE(200) + CHR(0)
[tab]lntext_len = GetWindowText(lnhwnd, @lctitle_bar, 200)
[tab]lctitle_bar = UPPER(LEFT(lctitle_bar, lntext_len))
[tab]IF !EMPTY(lctitle_bar)
[tab][tab]? lctitle_bar
[tab]ENDI
[tab]lnhwnd = GetWindow(lnhwnd,2)
ENDDO


TIA
FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
Chris

Have you looked into WONTOP(), WEXIT AND WVISIBLE()?
Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Mike

'Have you looked into WONTOP(), WEXIST() AND WVISIBLE()?'

It's the Windows window that requires indentification - unfortunately those functions only work on VFP windows.
FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
Chris

Maybe this?
Code:
WindowName()
Procedure WindowName
#DEFINE SW_NORMAL    1
#DEFINE SW_MAXIMIZE  3
#DEFINE SW_MINIMIZE  6

DECLARE Long FindWindow in Win32API String, String
DECLARE Long BringWindowToTop in Win32API Long
DECLARE Long ShowWindow in Win32API Long, Long

Local lnHWND,lcTitle
lcTitle = _screen.Caption
_screen.Caption = Sys(3)
lnHWND = FindWindow(null,m.lcTitle)
_Screen.Caption = m.lcTitle
ENDPROC
Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Mike

The procedure doesn't appear to do anything unless I'm missing it somewhere.

Are you suggesting there is a SYS() function that will return the active Windows window?
FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
Mike

If you press CTRL+ALT+DEL, the window at the top of the task list is always the active Windows window - that's the one I am trying to retrieve.

The text to the right of the icon is what is needed, but I'm probably approaching it from the wrong direction by looping through all windows/processes and trying somehow to identify it from the resultant list.

Reducing the windows/processes to those contained in the task list would be a good start, best of all being to return the task list as is and use the value from the topmost entry!
FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
Chris,
First, what's on the top of my Task Manager List depends on what it was last sorted on - Image Name, PID, CPU, etc. Also, on my two processor system, which would be "active"? Two different?

Actually, if VFP is checking it must, by definition, be the active application.

If you go to the UT ( click on the Download picture on the left, and choose the Visual FoxPro area. Next enter 'Enumerate' (without the quotes) in the Title area and press Enter. You should get back an entry for:
"Enumerate Processes in Windows" - it even has a demo that looks a lot like the task manager's process list in Windows NT/2000/XP.

Rick
 
Rick

Under Win2K, the active Window is at the top of the list, provided you press CTRL+ALT+DEL - I suspect you are going to the task manager without pressing those keys?

'Actually, if VFP is checking it must, by definition, be the active application.'

What should happen in this app is that the active Windows window would be identified in a timer event - VFP would not be the active window at that time, but would then use the active Windows window title for other processing purposes.

Thanks for the info on Enumerate from That enumerates the processes names as opposed to the window titles but still offers no means of identifying the active window.
FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
Chris,
I'm under XP Pro, and it appears that with each new start of the task manager it lists the tasks in reverse PID order - i.e. the highest one at the top. Unfortunately this isn't the current window or even the last task fired up.

Rick
 
The following code, modified from code courtesy of Anatoliy Mogylevets of provides an answer :-

LOCAL hWindow,lcText,lnLength,lcTitle

DECLARE INTEGER GetForegroundWindow IN user32
DECLARE INTEGER GetWindowText IN Win32API ;
[tab]INTEGER hWnd ,;
[tab]STRING @cText ,;
[tab]INTEGER nType

hWindow = GetForegroundWindow()
lcText = SPACE(250)
lnLength = GetWindowText(hWindow,@lcText,LEN(lcText))
lcTitle = LEFT(lcText,lnLength)
? lcTitle
FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
Okay, to take this further...

what's the best way to end a process programmatically?

This will happen after the user has tried to open the program, but they get an error telling them it's already open and I want to end the process programmatically - don't need them going into the task manager trying to do it themselves -

thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top