我正在开发 WPF 应用程序,我想选择显示窗口的监视器。我的.NET版本是8.0,SO是Windows,版本是7.0。
我正在尝试应用 Mostlytech 步骤,但它对我来说不起作用,窗口始终显示在同一监视器上,与所选的
AllScreens
参数无关。:
我的C#:
public MainWindow () {
this.WindowStartupLocation = WindowStartupLocation.Manual;
Debug.Assert(System.Windows.Forms.SystemInformation.MonitorCount > 1);
System.Drawing.Rectangle workingArea = System.Windows.Forms.Screen.AllScreens[0].WorkingArea;
this.Left = workingArea.Left;
this.Top = workingArea.Top;
this.Width = workingArea.Width;
this.Height = workingArea.Height;
this.WindowState = WindowState.Maximized;
this.WindowStyle = WindowStyle.None;
this.Topmost = true;
this.Show();
InitializeComponent();
SetLists();
// Rest of the Program.
}
我的 XAML 标头:
<Window x:Class="_4913_TV.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:_4913_TV"
mc:Ignorable="d"
WindowState="Maximized"
WindowStyle="None"
Title="MainWindow"
Cursor="None"
Width="4096"
Height="2160"
KeyDown="KeyedKey">
System.Drawing.Rectangle workingArea = System.Windows.Forms.Screen.AllScreens[0].WorkingArea;
它似乎正确包含来自我的显示器的信息。
非常感谢您的支持。
当显示器的 DPI 不是默认值 (100%) 时,您链接的代码片段不会使窗口正确最大化。
相反,我想建议一种不同的方法。下面的代码将操纵最大化窗口的位置和大小,以适应由命令行参数给出的索引号指定的监视器的工作区域。
using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
namespace WpfApp2;
public partial class MainWindow : Window
{
public int Index { get; private set; }
public MainWindow()
{
InitializeComponent();
// Get index number from command-line argument.
Index = int.TryParse(Environment.GetCommandLineArgs().LastOrDefault(), out int i) && (i > 0) ? i : 0;
nint handle = new WindowInteropHelper(this).EnsureHandle();
HwndSource source = HwndSource.FromHwnd(handle);
source.AddHook(new HwndSourceHook(WndProc));
}
private const int WM_GETMINMAXINFO = 0x0024;
[StructLayout(LayoutKind.Sequential)]
private struct POINT
{
public int x;
public int y;
}
[StructLayout(LayoutKind.Sequential)]
private struct MINMAXINFO
{
public POINT ptReserved;
public POINT ptMaxSize;
public POINT ptMaxPosition;
public POINT ptMinTrackSize;
public POINT ptMaxTrackSize;
}
private nint WndProc(nint hwnd, int msg, nint wParam, nint lParam, ref bool handled)
{
switch (msg)
{
case WM_GETMINMAXINFO:
var screens = System.Windows.Forms.Screen.AllScreens;
if (Index < screens.Length)
{
var workingArea = screens[Index].WorkingArea;
var info = Marshal.PtrToStructure<MINMAXINFO>(lParam);
info.ptMaxPosition.x = workingArea.X;
info.ptMaxPosition.y = workingArea.Y;
info.ptMaxSize.x = workingArea.Width;
info.ptMaxSize.y = workingArea.Height;
Marshal.StructureToPtr(info, lParam, true);
}
break;
}
return nint.Zero;
}
}
<Window x:Class="WpfApp2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Width="400" Height="300"
WindowStartupLocation="Manual"
WindowState="Maximized"
Topmost="True">
<Grid>
</Grid>
</Window>
它要求应用程序清单中包含以下部分。
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- Windows 10 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
</application>
</compatibility>
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2, PerMonitor</dpiAwareness>
</windowsSettings>
</application>