• 0

How to check for form focus?


Question

Hi,

I'm creating this application where I have a notify icon.

When the application is started the form will be hidden.

The tray icon works like this:

Click ? Set form location, show form, set form focus with Me.Activate()

Click ? Hide the form

Click ? Set form location, show form, set form focus with Me.Activate()

Click ? Hide the form

Click ? Set form location, show form, set form focus with Me.Activate()

Click ? Hide the form

(...)

and on and on...

But for instance. The last click on the tray icon, made the form visible and not hidden. But suppose I have a browser window open and I'm surfing the web and now I click the tray icon again... the form will be hidden and I want it to stay visible but to come up on top and focused and for that I need to validate (somewhere in my code-- I know where) if the form is focused or not. In this case it will not be visible and not focused so I'll just bring it to the front and I'll not hide it as it would happen in a normal situation. If it was visible AND focused, I would hide it.

Conclusion, how do I check if the form is focused or not?

Edited by Nazgulled
Link to comment
https://www.neowin.net/forum/topic/215229-how-to-check-for-form-focus/
Share on other sites

14 answers to this question

Recommended Posts

  • 0

The form won't be focused. As soon as you click on the tray icon, it loses focus. If a windows sits behind another, it will be visible and not have focus. So you really won't know based on those two properties whether to hide it or not.

First, I'd check the WindowState of your form. If it's not minimized, then do the next steps.

What you'd have to do is to use the GetTopWindow and GetNextWindow API functions to go through the top level windows, determine if they are applications, activatable, visible, and not tool windows. If it meets the criteria, compare its handle to your form's handle, if they aren't the same then you know your form is behind another form. If they are the same, then your form is top most in the z-order as long as there are no other windows preceding it the GetNextWindow search. You're going to need GetWindowInfo API, too.

Kind of a pain. I'd lay it out for you, but I'm way too busy. Maybe someone else could take the idea and help you out.

  • 0
  gerry.74 said:
If you got VB 2005, try this:

Set the option "TopMost" from False to True, and it will stay on top all the time, I use it for error messenges.. :D

Good Luck!! :D

That's in previous versions too... however i found that, this option was a pain when you have dialog boxes in your application..., especially modal ones.

  • 0

Wityh screenshots is hard, it would have to be avideo marking every click I make... but what I is so simple and I don't know why it is so hard to understand...

You click on the tray icon, if the form is hidden, show the form, if the form is visible and it's the active window in explorer, hide it, BUT if it is visible and it's not the active window (ie: I have some other app running wich is the active one), show the form...

I don't know what else I can do to explain this better...

  • 0
  gerry.74 said:
If you got VB 2005, try this:

Set the option "TopMost" from False to True, and it will stay on top all the time, I use it for error messenges.. :D

Good Luck!! :D

actually... that works on 2003 too... at least is working for me... anyway, that's what I'll do, even though that was not exactly what I wanted... but nevermind

  • 0

Well, here's the class file for determining if your form is a top level app. It works pretty good. Some stuff that's in there is just test code, so if you want to play around with it, go ahead. You could plop this code in a C# library if you don't feel like trying to understand it, and converting it to VB.

Rename the file, of course, to use. Some of this stuff may not work, so don't get all anal on me. I'm too busy to do everything perfect.

http://www.opticverve.us/code/findwindowlib.cs.txt

@Nazgulled - in your case, you'd want to have in the click handler for your notify icon code similar to this. If someone wants to translate this to VB, please do. I'm way behind on a real project. :(

Just a note, the reason I use WindowState to show and hide the form is that you get that cool animation to and from the taskbar. I don't think Hide()/Show() does that. Also, from a usability perspective, you want the user to know that the app isn't closing, just running in the background. It's a hint for them.

	 switch( this.WindowState )
 	 {
    case FormWindowState.Minimized:
   	 this.WindowState = FormWindowState.Normal;
   	 this.Activate();
   	 this.ShowInTaskbar = true;
   	 break;
    default:
   	 if( this.Visible )
   	 {
      if( FindWindowLib.IsTopWindow( this.Handle ) == false )
      {
     	 this.Activate();
      }
      else
      {
     	 this.WindowState = FormWindowState.Minimized;
     	 this.ShowInTaskbar = false;
      }	
   	 }
   	 else
   	 {
      this.Activate();
   	 }
   	 break;
 	 }

  • 0

for now, I'll let my app the way it is, and then, if I feel like it, I'll see what I can do about that...

about your code, maybe it will be useful for someone :) I could try and translate into vb but after reading it, and I understand a bit of C (I know that is C#, but the syntax is almost the same with a little of VB on it...) but I don't if I could exactly translate into VB...

though, my app works a bit different, I never use the WindoState because I have a different animation ;) the way my app works and all, that's the way I want it... you'll see when I finish, cuz I'll make it public, I just need to finish one small thing that I don't know how :s

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.