简体   繁体   English

如何在.NET中控制Windows应用程序焦点

[英]How to control windows application focus in .NET

I'm using pinvoke "user32.dll" to send my application to back (behind all other apps) so it sits on desktop and vice versa. 我正在使用pinvoke“ user32.dll”将我的应用程序发送回(在所有其他应用程序之后),因此它位于桌面上,反之亦然。 At the moment it just toggles - back/front. 目前,它只是切换-后/前。 Is there 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?). 将窗口设置为前台,但是Microsoft更改了Windows ME / 2000及更高版本的功能(可能是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://www.tek-tips.com/faqs.cfm?fid=4262

http://markribau.org/blog/?p=7 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. 我记得曾经读过一篇有关注册表项的信息,该注册表项可以设置为允许SetForegroundWindow根据需要工作,但这是系统范围的设置。

http://www.delphipages.com/forum/showthread.php?t=198261 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 您可以使用SetWindowPos将窗口放置在其他窗口的后面或永久放置在顶部(如果有帮助)

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Windows Forms应用程序中将焦点设置为控件? - How to set focus to a control in a Windows Forms application? 如何在C#windows窗体应用程序中使用低于当前控件的tabindex将焦点发送到控件? - How to send focus to control with tabindex lower than current control in C# windows form application? 如何将focus()赋予错误提供程序在C#Windows应用程序中命中的第一个控件? - How to give focus() to the first control that Error Provider hits in C# Windows Application? 如何在.net windows窗体应用程序中将访问控制与我的ORM集成? - How to integrate access control with my ORM in a .net windows form application? 如何强制关注Windows窗体中的控件 - How to force a focus on a control in windows forms 如何在.NET中验证后将焦点设置为控件 - How to set focus to a control after validation in .NET 禁用Windows控制焦点 - Disable Windows Control Focus 如何在C#Windows应用程序中将滚动焦点设置为指定的控件? - How to set the scroll focus to specified control in c# windows app? 如何设置哪个控件专注于应用程序启动 - How to set which control has focus on Application start 如何在C#.net(2010)Windows窗体应用程序中获取TreeView控件中的所有复选框值? - how to get the All the Checkbox values in TreeView control in C#.net(2010) Windows Forms Application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM