3

I'm trying to create an application in VB.net that can come to the front once a certain condition occurs (in this case, a file being created). I've tried the following code, but it just results in the taskbar button blinking on Windows 7:

Me.Activate()

I understand the reason for this, and I completely understand why people don't want any random application to steal focus (I hate it too). This is an extremely limited use application (only 3 computers will actually be using it), and the users in this case would want the application to popup in front (there will be an option to turn it off too).

Any ideas if this is still possible to do on Windows 7?

favo
  • 5,426
  • 9
  • 42
  • 61
user1159415
  • 111
  • 7
  • 3
    No. There's no (documented) way of disabling the behavior. Otherwise every programmer out there who thinks their application is the best thing in the universe (read: all of them) will abuse it. – In silico Jan 19 '12 at 20:52
  • Excellent point :) Is there a setting that I can change on Windows 7? I know on XP you could turn this off. – user1159415 Jan 19 '12 at 20:53
  • As far as I know, no there isn't. Depending on the nature of your application, this might be a good use for [taskbar notifications](http://msdn.microsoft.com/en-us/library/windows/desktop/aa511497.aspx). It's way less intrusive. – In silico Jan 19 '12 at 20:55
  • I looked into that, but I really need a way to display an image with it as well. – user1159415 Jan 19 '12 at 21:44
  • 1
    Read the first comment again. It's not possible, for the very reason that everyone could think their app is more important than the next one. I'd suggest you stop trying to circumvent the rules and learn to play within them. :) – Ken White Jan 19 '12 at 21:47
  • This code seems to work: AppActivate("Window Name Here").... but from what all of you are saying, it shouldn't. – user1159415 Jan 19 '12 at 21:57
  • Well, it WAS working, it's not now. Maybe it was working because I was running it directly from Visual Studio? Hmm. – user1159415 Jan 19 '12 at 22:35
  • It works for me by following the Answer of MarkJ. In addition I call AppActivate(Process.GetCurrentProcess().Id) as well – Harun Aug 27 '13 at 05:54

1 Answers1

3

The simplest way is to change the ForegroundLockTimeout registry key on the three computers. This disables the Windows rules that prevents background applications from stealing the focus. Obviously it will allow all applications on the system to steal the focus.

You can also disable the rules through code using SystemParametersInfo with SPI_SETFOREGROUNDLOCKTIMEOUT For example see here

Community
  • 1
  • 1
MarkJ
  • 30,070
  • 5
  • 68
  • 111