windows DirectX 应用程序中 Alt-Tab 支持的最佳实践?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/972299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:36:57  来源:igfitidea点击:

Best practices for Alt-Tab support in a DirectX app?

windowsdirectxalt-tabtask-switching

提问by chaos

When writing DirectX applications, obviously it's desirable to support the user suspending the application via Alt-Tabin a way that's fast and error-free. What is the best set of practices for ensuring this? Things that need to be addressed include:

在编写 DirectX 应用程序时,显然需要支持用户通过Alt-Tab以一种快速且无错误的方式挂起应用程序。确保这一点的最佳实践是什么?需要解决的问题包括:

  1. The best methods of detecting when your application has been alt-tabbed out of and when it has been returned to.
  2. What DirectX resources are lost when the user alt-tabs, and the best ways to cope with this.
  3. Major things to do and things to avoid in application architecture for purposes of alt-tab support.
  4. Any significant differences between major DirectX versions as they apply to the above.
  1. 检测应用程序何时被 alt 选项卡退出以及何时返回的最佳方法。
  2. 当用户按下 alt-tab 时会丢失哪些 DirectX 资源,以及解决此问题的最佳方法。
  3. 出于支持 alt-tab 的目的,在应用程序架构中要做的主要事情和要避免的事情。
  4. 主要 DirectX 版本之间的任何显着差异,因为它们适用于上述内容。

Interesting tricks and gotchas are also good to hear about.

有趣的技巧和陷阱也很值得一听。

回答by jjxtra

I will assume you are using C++ for the purposes of my answers, but if you can afford to use C#, XNA (http://creators.xna.com/) is an excellent game platform that handles all of these issues for you.

我将假设您在回答时使用 C++,但如果您负担得起使用 C#,XNA ( http://creators.xna.com/) 是一个出色的游戏平台,可以为您处理所有这些问题。

1]

1]

This article is helpful for windows events in the window procedure to detect when a window loses or gains focus, you could handle this on your main window: http://www.functionx.com/win32/Lesson05.htm. Also, check out the WM_ACTIVATEAPP message here: http://msdn.microsoft.com/en-us/library/ms632614(VS.85).aspx

本文有助于窗口过程中的窗口事件检测窗口何时失去或获得焦点,您可以在主窗口上处理此问题:http: //www.functionx.com/win32/Lesson05.htm。另外,请在此处查看 WM_ACTIVATEAPP 消息:http: //msdn.microsoft.com/en-us/library/ms632614(VS.85) .aspx

2]

2]

3]

3]

I would make sure to never disable Alt-Tab. You probably want minimal CPU load while the application is not active because the user probably Alt-Tabbed because they want to do something else, so you could completely pause the application, or reduce the frames rendered per second. If the application is minimzed, you of course don't need to render anything either. After thinking about a network game, my best solution is that you should still reduce the frames rendered per second as well as the amount of network packets handled, possibly even throwing away many of the packets that come in until the game is re-activated.

我会确保永远不会禁用 Alt-Tab。您可能希望在应用程序未处于活动状态时将 CPU 负载降至最低,因为用户可能会使用 Alt-Tabbed,因为他们想做其他事情,因此您可以完全暂停应用程序,或减少每秒渲染的帧数。如果应用程序被最小化,你当然也不需要渲染任何东西。在考虑过网络游戏之后,我的最佳解决方案是您仍然应该减少每秒渲染的帧数以及处理的网络数据包的数量,甚至可能会丢弃许多进入的数据包,直到重新激活游戏。

4]

4]

Honestly I would just stick to DirectX 9.0c (or DirectX 10 if you want to limit your target operating system to Vista and newer) if at all possible :)

老实说,如果可能的话,我只会坚持使用 DirectX 9.0c(如果您想将目标操作系统限制为 Vista 和更新版本,则使用 DirectX 10):)

Finally, the DirectX sdk has numerous tutorials and samples: http://www.microsoft.com/downloads/details.aspx?FamilyID=24a541d6-0486-4453-8641-1eee9e21b282&displaylang=en

最后,DirectX sdk 有很多教程和示例:http: //www.microsoft.com/downloads/details.aspx?FamilyID=24a541d6-0486-4453-8641-1eee9e21b282&displaylang=en

回答by chris166

We solved it by not using a fullscreen DirectX device at all - instead we used a full-screen window with the top-most flag to make it hide the task bar. If you Alt-Tab out of that, you can remove the flag and minimize the window. The texture resources are kept alive by the window.

我们完全不使用全屏 DirectX 设备解决了这个问题——而是使用了一个带有最顶部标志的全屏窗口来隐藏任务栏。如果您不使用 Alt-Tab,您可以删除该标志并最小化窗口。纹理资源由窗口保持活动状态。

However, this approach doesn't handle the device lost event happening due to 'lock screen', Ctrl+Alt+Delete, remote desktop connections, user switching or similar. But those don't need to be handled extremely fast or efficiently (at least that was the case in our application)

但是,这种方法不会处理由于“锁定屏幕”、Ctrl+Alt+Delete、远程桌面连接、用户切换或类似操作而发生的设备丢失事件。但是那些不需要非常快速或高效地处理(至少在我们的应用程序中是这样)

回答by Captain Lepton

All serious D3D apps should be able to handle lost devices as this is something that can happen for a variety of reasons.

所有严肃的 D3D 应用程序都应该能够处理丢失的设备,因为这可能由于多种原因而发生。

In DX10 under Vista there is a new "Timeout Detection and Recovery" feature that makes it common in my experience for graphics devices to be reset which would cause a lost device for your app. This seems to be improving as drivers mature but you need to handle it anyway.

在 Vista 下的 DX10 中,有一个新的“超时检测和恢复”功能,根据我的经验,重置图形设备会导致应用程序丢失设备。随着驱动程序的成熟,这似乎正在改善,但无论如何您都需要处理它。

回答by PowerApp101

In DX8 and 9 (and 10?) if you create your resources (vertex and index buffers and textures mainly) using D3DPOOL_MANAGED they will persist across lost devices and will not need reloading. This is because they are stored in system memory and the DX runtime copies to video memory automatically. However there is a performance cost due to the copying and this is not recommended for rapidly changing vertex data. Of course you would profile first to determine if there is a speed issue :-)

在 DX8 和 9(和 10?)中,如果您使用 D3DPOOL_MANAGED 创建您的资源(主要是顶点和索引缓冲区和纹理),它们将在丢失的设备上持续存在并且不需要重新加载。这是因为它们存储在系统内存中,而 DX 运行时会自动复制到视频内存中。然而,由于复制而存在性能成本,因此不建议将其用于快速更改的顶点数据。当然,您会首先进行分析以确定是否存在速度问题:-)