简体   繁体   English

将Windows应用程序置于其他窗口之上并保持关注 - 始终如此

[英]Keeping a Windows application on top of other windows and in focus - always

I am creating a kiosk application and I want to ensure it is always, no matter what, on top of other Windows applications and the Windows task bar. 我正在创建一个自助服务终端应用程序,我想确保它总是,无论如何,在其他Windows应用程序和Windows任务栏之上。

I am already blocking Windows keyboard commands (alt-tab, etc) but there are still situations that could cause an application to launch and steal the screen. 我已经阻止了Windows键盘命令(alt-tab等),但仍然存在可能导致应用程序启动并窃取屏幕的情况。

Is it possible to hook into Windows from .NET and continually test whether the application has focus and is on top, and if not then give it focus and make it on top? 是否有可能从.NET挂钩到Windows并不断测试应用程序是否具有焦点并且是否位于顶部,如果没有,那么请将其重点放在顶部?

I've actually worked on a production kiosk (it was Windows 2000, however). 我实际上在一个生产信息亭工作(但它是Windows 2000)。 The solution was to run our application as the shell. 解决方案是将我们的应用程序作为shell运行。 You accomplish this in part by replacing Explorer.exe with your application in the Shell value at: 您可以通过将Shell.exe替换为Shell中的应用程序来实现此目的:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon

See here for some more information. 有关更多信息,请参见此处

Now, we did have a secret (err... obfuscated) way to shut down our app. 现在,我们确实有一个秘密(错误...混淆)的方式来关闭我们的应用程序。 Then we would bring up Task Manager ( Ctrl - Shift - Esc ) and select File/New Task to run Explorer.exe to bring up a shell right then and there. 然后我们将调出任务管理器( Ctrl - Shift - Esc )并选择文件/新任务以运行Explorer.exe,然后在那里调出一个shell。

As an aside, when you work on a system like this, you naturally become very very proficient with the keyboard and all that it means to use keyboard shortcuts in Windows because you will likely not have a convenient way or place to put a mouse. 顺便说一句,当你使用这样的系统时,你自然会非常熟练地使用键盘以及在Windows中使用键盘快捷键的所有方法,因为你可能没有方便的地方放置鼠标。

You might be able to do this just by replacing the shell with your application. 您可以通过将shell替换为应用程序来完成此操作。
Here's a superuser question about replacing the shell with IE: https://superuser.com/questions/108265/how-do-you-replace-the-logon-shell-with-iexplore 以下是关于用IE替换shell的超级用户问题: https//superuser.com/questions/108265/how-do-you-replace-the-logon-shell-with-iexplore

If you only want to do it for the current user I think the path is 如果您只想为当前用户执行此操作,我认为路径是
HKEY_CURRENT_USER\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon

In this scenario you can run your application in full screen with always your window on top. 在这种情况下,您可以全屏运行应用程序,并始终将窗口置于顶部。 I use the following snippet in some of my opengl apps (from http://nehe.gamedev.net/ ). 我在我的一些opengl应用程序中使用以下代码段(来自http://nehe.gamedev.net/ )。 It is in win32 but I think you can use pinvoke or System.Management.ManagementClass("Win32_VideoController") 它在win32但我认为你可以使用pinvoke或System.Management.ManagementClass(“Win32_VideoController”)

    DEVMODE dmScreenSettings;                               // Device Mode
    memset(&dmScreenSettings,0,sizeof(dmScreenSettings));   // Makes Sure Memory's Cleared
    dmScreenSettings.dmSize=sizeof(dmScreenSettings);       // Size Of The Devmode Structure
    dmScreenSettings.dmPelsWidth    = width;                // Selected Screen Width
    dmScreenSettings.dmPelsHeight   = height;               // Selected Screen Height
    dmScreenSettings.dmBitsPerPel   = bits;                 // Selected Bits Per Pixel
    dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
// Try To Set Selected Mode And Get Results.  NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
if(ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL);

This will switch your app to full screen and get rid of taskbar and disallow use from doing something other than using your app. 这会将您的应用切换到全屏并摆脱任务栏并禁止使用除了使用您的应用之外的其他操作。

You can use API calls. 您可以使用API​​调用。 The snag is you have to keep checking if your app has lost focus. 问题是你必须继续检查你的应用是否失去了焦点。 Depends on exactly why you want to do it but... 取决于你想要做的确切原因,但......

    Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As IntPtr) As Boolean

is the API declaration. 是API声明。 Then you just need the window handle :) 然后你只需要窗口句柄:)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM