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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detecting Application GotFocus or Activate 2

Status
Not open for further replies.

megmogg

Programmer
Jul 14, 2005
76
GB

Hi all

Using VFP8

In my application, which uses _screen, I would like to detect when it gets activated from another windows application, i.e. when a user is using Outlook and then clicks back (or Alt-tab) onto my application.

I tried binding the Activate event to the _screen as below but it didnt work.

Code:
oScreenFocus=NEWOBJECT("ScreenFocus")
BINDEVENT(_screen, "Activate", oScreenFocus, "onfocus")

DEFINE CLASS ScreenFocus AS Custom
   PROCEDURE onfocus
      MESSAGEBOX("Got Focus")
	ENDPROC
ENDDEFINE

Anyone any ideas?

Thanks

 
This is a stab at it using windows functions:

*COPY TO PRG and RUN
clear
PUBLIC GetForegroundWindow, GetWindowText, lCurHwnd, loTimer
Declare Integer GetForegroundWindow IN user32

DECLARE INTEGER ShowWindow IN user32;
INTEGER hwnd,;
INTEGER nCmdShow

DECLARE INTEGER GetWindowThreadProcessId IN user32;
INTEGER hWnd,;
INTEGER @ lpdwProcId


lCurHwnd = GetForegroundWindow()


loTimer = CREATEOBJECT('myTimer')



DEFINE CLASS myTimer AS TIMER
Interval = 100

PROCEDURE Timer
lnThisProcess = GetWindowThreadProcessId(lCurHwnd, 0)
lnOtherProcess = GetWindowThreadProcessId(GetForegroundWindow(), 0)
IF lnThisProcess != lnOtherProcess
?"OtherProcessActive"
ELSE
?"ThisProcessActive"
ENDIF

** This will show the handle specified
*ShowWindow(lCurHwnd,1)
ENDPROC

ENDDEFINE



Regards,

Rob
 

Thanks for the reply and see what you're doing.

Wouldn't the timer drain resources?

Its just a shame there is no focus or activate event for the application itself.

The activate for the _screen is when the screen gets focus, i.e. when the last form is closed. But not when switching from another application.



 

Well, as it happens, we are moving to VFP9 in the next couple of weeks.

Do you have an example by any chance?

Thanks

 
I tried your code and here is something to note. I copied it into a .prg and ran it. When I click on the screen the behind the prg the _screen.activate event fired and the message appeared. However now you just have to figure out how to have the _screen be on top which from what I can see it is not. If you had a form instead of _screen you could possibly handle it that way you want. Create a form and set _screen.visible = .F. at runtime then when the form gets activated your bind will work.

Regards,

Rob
 

Thanks for the replies

robsuttonjr - when it is compiled, it only fires when a form within the _screen is closed and the _screen itself gets the focus. What I want to achieve is when a user is using another application and alt-tab's back to mine, I want the active form to requery. The events within VFP are only firing on VFP actions/events not after the alt-tab.

MarciaAkins - thanks, will take a look.


Thanks both for your help. [thumbsup2]






 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top