PC Review Forums Newsgroups Microsoft DotNet Microsoft Dot NET Compact Framework Check which application has focus

Reply

Check which application has focus

 
Thread Tools Rate Thread
Old 22-06-2007, 07:03 AM   #1
vsciw
Guest
 
Posts: n/a
Default Check which application has focus


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.

  Reply With Quote
Old 22-06-2007, 08:10 AM   #2
Christian Resma Helle
Guest
 
Posts: n/a
Default Re: Check which application has focus

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

  Reply With Quote
Old 22-06-2007, 10:45 AM   #3
vsciw
Guest
 
Posts: n/a
Default Re: Check which application has focus

On 22 Jun, 08:10, Christian Resma Helle <xtian...@gmail.com> wrote:
> 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.

  Reply With Quote
Old 22-06-2007, 04:01 PM   #4
Paul G. Tobey [eMVP]
Guest
 
Posts: n/a
Default Re: Check which application has focus

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.

"vsciw" <ian.watkins@vscsolutions.com> wrote in message
news:1182505545.967133.229420@o61g2000hsh.googlegroups.com...
> On 22 Jun, 08:10, Christian Resma Helle <xtian...@gmail.com> wrote:
>> 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.
>



  Reply With Quote
Old 22-06-2007, 08:32 PM   #5
Christian Resma Helle
Guest
 
Posts: n/a
Default Re: Check which application has focus

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

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


  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off