Check which application has focus

V

vsciw

Hi.

we have a WM5 device, with several applications running. is it
possible for one of our applications running on this device (vb.net
cf2) to identify which program has "focus" on the device (which app
the user is seeing on the screen)??

is there any environment or registry settings i can look at?

Ian.
 
C

Christian Resma Helle

Hi Ian,

You can P/Invoke a method called GetForegroundWindow() from
coredll.dll and compare the handle you get with the handle of your
applications main form.

To call GetForegroundWindow() you would have to use DllImport

<DllImport("coredll.dll")> _
Private Shared Function GetForegroundWindow() As IntPtr
End Function

In your main form compare Me.Handle with the IntPtr you get from
GetForegroundWIndow()


Regards,
Christian Resma Helle
http://christian-helle.blogspot.com
 
V

vsciw

Hi Ian,

You can P/Invoke a method called GetForegroundWindow() from
coredll.dll and compare the handle you get with the handle of your
applications main form.

To call GetForegroundWindow() you would have to use DllImport

<DllImport("coredll.dll")> _
Private Shared Function GetForegroundWindow() As IntPtr
End Function

In your main form compare Me.Handle with the IntPtr you get from
GetForegroundWIndow()

Regards,
Christian Resma Hellehttp://christian-helle.blogspot.com

Thanks. I'll give this a go. Can this info tell me the name of the
application in the foreground? I only want to swap back to "my"
application on certain occasions - i need to allow some apps into the
foreground.

Ian.
 
P

Paul G. Tobey [eMVP]

Define "name of the application"! If you want the title of the window, yes,
you can call GetWindowText() on it and you'll get the title. If you mean
the name of the EXE that launched it, no (not that a user couldn't just as
easily change the name of any EXE, anyway). If you own the applications in
question, you'd be better off coming up with some way for them to
communicate with one another when it's time for you to come to the front.

Paul T.
 
C

Christian Resma Helle

Following up on Paul's advice, here's a sample of p/invoking GetWindowText()

<DllImport("coredll.dll")>
Private Shared Function GetWindowText( _
ByVal hWnd As IntPtr, _
ByVal lpString As StringBuilder, _
ByVal nMaxCount As Integer) As Integer
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top