WebEnh

.net7 mvc jquery bootstrap json 学习中 第一次学PHP,正在研究中。自学进行时... ... 我的博客 https://enhweb.github.io/ 不错的皮肤:darkgreentrip,iMetro_HD
随笔 - 1079, 文章 - 1, 评论 - 75, 阅读 - 174万
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

wpf 双屏显示问题

Posted on   WebEnh  阅读(302)  评论(0编辑  收藏  举报
复制代码

// 在WPF中处理双屏显示问题,通常需要确保应用程序能够识别两个显示器,并在每个显示器上正确渲染内容。以下是一个简化的示例,展示如何在WPF应用程序中设置窗口,使其跨越两个显示器:


 


using
System; using System.Windows; using System.Windows.Forms; public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.SourceInitialized += MainWindow_SourceInitialized; } private void MainWindow_SourceInitialized(object sender, EventArgs e) { IntPtr windowHandle = new WindowInteropHelper(this).Handle; Screen[] screens = Screen.AllScreens; if (screens.Length > 1) { Rectangle primaryScreenBounds = screens[0].Bounds; Rectangle secondaryScreenBounds = screens[1].Bounds; // 设置窗口的起始位置和大小,以覆盖两个屏幕 this.Left = primaryScreenBounds.Left; this.Top = primaryScreenBounds.Top; this.Width = primaryScreenBounds.Width + secondaryScreenBounds.Width; this.Height = Math.Max(primaryScreenBounds.Height, secondaryScreenBounds.Height); // 将窗口的其余部分移动到第二个屏幕 User32.SetWindowPos( windowHandle, (IntPtr)(-1), // HWND_TOPMOST secondaryScreenBounds.Left, secondaryScreenBounds.Top, secondaryScreenBounds.Width, secondaryScreenBounds.Height, 0x0001 | 0x0002 // SWP_NOMOVE | SWP_NOSIZE ); } } } // 扩展类User32包含对Win32 API的调用 public static class User32 { [System.Runtime.InteropServices.DllImport("user32.dll")] public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags); }
复制代码

在这个示例中,我们首先检索所有屏幕的边界,然后调整主窗口的位置和大小,使其覆盖两个显示器。接下来,我们使用User32.SetWindowPos方法将窗口的其余部分移动到第二个屏幕。

请注意,这个示例使用了Windows API SetWindowPos来调整窗口的位置和大小,并且需要引用System.Windows.Forms命名空间下的Screen类来获取显示器信息。

确保您已经在项目中引用了System.Windows.Forms程序集,并且在XAML中定义了相应的窗口资源。这个示例假设您已经有了一个WPF窗口,并且正确设置了XAML和事件处理器。

 
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示

喜欢请打赏

扫描二维码打赏

了解更多