在辅助显示器上全屏显示
如何对 dotNet Windows(或 WPF)应用程序进行编程,以使其在辅助显示器上全屏显示?
How can you program a dotNet Windows (or WPF) Application in order to let it going fullscreen on the secondary monitor?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
用于最大化辅助监视器(如果有)的窗口的扩展方法。
不假设辅助监视器是 System.Windows.Forms.Screen.AllScreens[2];
Extension method to Maximize a window to the secondary monitor (if there is one).
Doesn't assume that the secondary monitor is System.Windows.Forms.Screen.AllScreens[2];
对于 WPF 应用程序,请查看这篇文章 。 最终这取决于 WindowState 何时设置为最大化。 如果您在 XAML 或窗口构造函数中设置它(即在加载窗口之前),它将始终最大化到主显示上。 另一方面,如果在加载窗口时将 WindowState 设置为“最大化” - 它将在之前最大化的屏幕上最大化。
For WPF apps look at this post. Ultimately it depends on when the WindowState is set to Maximized. If you set it in XAML or in window constructor (i.e. before the window is loaded) it will always be maximized onto primary display. If, on the other hand, you set WindowState to Maximized when the window is loaded - it will maximise on the screen on which it was maximized before.
我注意到一个答案主张在 Loaded 事件中设置位置,但是当窗口首先显示正常然后最大化时,这会导致闪烁。 如果您在构造函数中订阅 SourceInitialized 事件并在其中设置位置,它将处理在辅助监视器上最大化而不会闪烁 - 我假设此处为 WPF
将坐标替换为辅助监视器上的任何坐标
I notice an answer which advocates setting the position in the Loaded event, but this causes flicker when the window is first shown normal then maximized. If you subscribe to the SourceInitialized event in your constructor and set the position in there it will handle maximizing onto secondary monitors without flicker - I'm assuming WPF here
Substitute coords for any on your secondary monitor
请参阅这篇代码项目文章。
其中的代码可以工作,但默认为您的主要代码监视器。 要更改此设置,您需要将对 GetSystemMetrics 的调用替换为对 获取监视器信息。 使用 GetMonitorInfo,您可以获得适当的 RECT 并传递给 SetWindowPos。
GetMonitorInfo 允许您获取任何监视器的 RECT。
有一篇 MSDN 文章,介绍多显示器设置中的定位应用< /a> 这可能有助于更好地解释事情。
See this codeproject article.
The code in there will work, but default to your primary monitor. To change this, you'll need to replace the calls to GetSystemMetrics will calls to GetMonitorInfo. Using GetMonitorInfo, you can get the appropriate RECT to pass to SetWindowPos.
GetMonitorInfo allows you to get the RECT for any monitor.
There is an MSDN Article on Position Apps in Multi-Monitor Setups that might help explain things a bit better.
@jay-evans 的回答对我有用。 但是,我还需要添加 WpfscreenHelper Nuget 包来获取屏幕信息,同时不依赖于旧的 System.Windows.Forms 命名空间,如 @grantnz 的答案。
另请注意,仅设置尺寸(左侧、顶部、宽度+高度)会在每一侧留下一个小边框。 只有设置了WindowState.Maximized后,才达到真正的全屏效果。
由于我的显示器具有不同的 DPI 配置,因此我还必须使用 WpfWorkingArea 而不是 WorkArea,因为坐标返回不正确。
为了完整起见,在此发布,这是使用带有 .NET 7.0 的 WPF,并且已确认可以使用不同的 DPI 配置:
@jay-evans answer did the trick for me. However, I also needed to add the WpfscreenHelper Nuget package to get the screen information while not depending on the old System.Windows.Forms namespace as in @grantnz's answer.
Also note that setting the dimensions only (left, top, width + height) left me with a small border on each side. Only after setting WindowState.Maximized is it that the real full-screen effect was achieved.
Since I have monitors with varying DPI configurations, I also had to use WpfWorkingArea instead of WorkingArea, since the coordinates were coming back incorrectly.
Posting here for completeness, this is using WPF with .NET 7.0 and is confirmed to work with varying DPI configurations:
从您的问题中不清楚您是否正在寻找一种将窗口移动到辅助监视器然后进入全屏的方法,或者您只是希望在窗口所在的任何监视器(可能是主要或辅助监视器)上支持全屏模式)。
如果是后者,对于WPF窗口,虽然与全屏模式不太一样,但您可以在最大化时删除边框,并在未最大化时恢复边框。 无需检查哪个显示器等。标题栏的显示由边框状态控制。
感谢 Pavel 在当前问题中基于表单的回答,以及 Nir 在 这个问题。
It's not clear from your question if you are looking for a way to move the window to the secondary monitor and then go fullscreen, or if you are just looking to support fullscreen mode on whatever monitor the window is on (which may be primary or secondary).
If the later, for a WPF window, though not quite the same as fullscreen mode, you can remove the borders when it is maximized and restore the border when not maximized. No need to check for which monitor, etc. The display of the caption/title bar is controlled by the border state.
Credit goes to Pavel for his Forms-based answer in the current question and to Nir for his answer in this question.
在 WPF 中:将 WindowState 属性设置为 Normal(不是 Maximixed)并创建事件 Loaded。 如果发生这种情况,请编写以下代码:
In WPF: Set the WindowState property in Normal (not Maximixed) and create the event Loaded. In the event write this code: