开发者

How to control windows application focus in .NET

开发者 https://www.devze.com 2022-12-17 22:15 出处:网络
I\'m using pinvoke \"user32.dll\" to send my application to back (behind all other apps) so it sits on desktop and vice versa. At the moment it just toggles - back/front. Is t开发者_开发知识库here a w

I'm using pinvoke "user32.dll" to send my application to back (behind all other apps) so it sits on desktop and vice versa. At the moment it just toggles - back/front. Is t开发者_开发知识库here a way to detect if my app is at the back and bring it to front or if it's on front and send it to back? THanks.


You can detect if your window is the active window by calling

[DllImport("user32.dll")] static
static extern IntPtr GetForegroundWindow();

You used to be able to call

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);

to set your window to the foreground, but Microsoft changed it's functionality in Windows ME/2000 onwards (May have been XP?).

There are various work arounds attempts for this issue, you may need to test a few to see which works for you.

http://www.tek-tips.com/faqs.cfm?fid=4262

http://markribau.org/blog/?p=7

I remember reading about a registry entry that can be set to allow SetForegroundWindow to work as desired, but it's a system wide setting.

http://www.delphipages.com/forum/showthread.php?t=198261

You can use SetWindowPos to place you window behind other windows or permanently on top if that helps

http://www.pinvoke.net/default.aspx/user32.SetWindowPos

http://msdn.microsoft.com/en-us/library/ms633545(VS.85).aspx


These links should help:

How to get the z-order in windows?

http://msdn.microsoft.com/en-us/library/ms632599%28VS.85%29.aspx#zorder

0

精彩评论

暂无评论...
验证码 换一张
取 消