8,735
社区成员
发帖
与我相关
我的任务
分享
this.SourceInitialized += MainWindow_SourceInitialized;
void MainWindow_SourceInitialized(object sender, EventArgs e)
{
HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
if (source == null)
// Should never be null
throw new Exception("Cannot get HwndSource instance.");
source.AddHook(new HwndSourceHook(this.WndProc));
}
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
switch (msg)
{
case Win32.WM_GETMINMAXINFO: // WM_GETMINMAXINFO message
WmGetMinMaxInfo(hwnd, lParam);
handled = true;
break;
// case Win32.WM_NCHITTEST: // WM_NCHITTEST message
// return WmNCHitTest(lParam, ref handled);
}
return IntPtr.Zero;
}
private void WmGetMinMaxInfo(IntPtr hwnd, IntPtr lParam)
{
// MINMAXINFO structure
Win32.MINMAXINFO mmi = (Win32.MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(Win32.MINMAXINFO));
// Get handle for nearest monitor to this window
WindowInteropHelper wih = new WindowInteropHelper(this);
IntPtr hMonitor = Win32.MonitorFromWindow(wih.Handle, Win32.MONITOR_DEFAULTTONEAREST);
// Get monitor info
Win32.MONITORINFOEX monitorInfo = new Win32.MONITORINFOEX();
monitorInfo.cbSize = Marshal.SizeOf(monitorInfo);
Win32.GetMonitorInfo(new HandleRef(this, hMonitor), monitorInfo);
// Get HwndSource
HwndSource source = HwndSource.FromHwnd(wih.Handle);
if (source == null)
// Should never be null
throw new Exception("Cannot get HwndSource instance.");
if (source.CompositionTarget == null)
// Should never be null
throw new Exception("Cannot get HwndTarget instance.");
// Get transformation matrix
Matrix matrix = source.CompositionTarget.TransformFromDevice;
// Convert working area
Win32.RECT workingArea = monitorInfo.rcWork;
Point dpiIndependentSize =
matrix.Transform(new Point(
workingArea.Right - workingArea.Left,
workingArea.Bottom - workingArea.Top
));
// Convert minimum size
Point dpiIndenpendentTrackingSize = matrix.Transform(new Point(
this.MinWidth,
this.MinHeight
));
// Set the maximized size of the window
mmi.ptMaxSize.x = (int)dpiIndependentSize.X;
mmi.ptMaxSize.y = (int)dpiIndependentSize.Y;
// Set the position of the maximized window
mmi.ptMaxPosition.x = 0;
mmi.ptMaxPosition.y = 0;
// Set the minimum tracking size
mmi.ptMinTrackSize.x = (int)dpiIndenpendentTrackingSize.X;
mmi.ptMinTrackSize.y = (int)dpiIndenpendentTrackingSize.Y;
Marshal.StructureToPtr(mmi, lParam, true);
}
已 2个Form 窗口显示到 2个显示屏为例,在App 的cs代码中 重载OnStartup 方法:
frmNameSpace= System.Windows.Forms;
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MainWindow mainWindow = new MainWindow();
MainGreenRoomWindow maintainWindow = new MainGreenRoomWindow();
if (frmNameSpace.Screen.AllScreens != null && frmNameSpace.Screen.AllScreens.Count() == 2)
{
frmNameSpace.Screen sRight = frmNameSpace.Screen.AllScreens[0];
frmNameSpace.Screen sLeft = frmNameSpace.Screen.AllScreens[1];
Rectangle rectLeft = sRight.WorkingArea;
Rectangle rectRight = sLeft.WorkingArea;
mainWindow.Width = rectRight.Width;
mainWindow.Height = rectRight.Height;
maintainWindow.Top = rectLeft.Top;
maintainWindow.Left = rectLeft.Left + 3;
//maintainWindow.Width = rectLeft.Width;
//maintainWindow.Height = rectLeft.Height;
mainWindow.Show();
maintainWindow.Show();
if (mainWindow != null)
{
maintainWindow.Owner = mainWindow;
}
}
else
{
MessageBox.Show("请确保屏幕为双屏模式。");
Application.Current.Shutdown();
}
}
第一屏窗口 设置全屏:
private void SetFullScreen()
{
this.WindowState = System.Windows.WindowState.Maximized;
this.WindowStyle = System.Windows.WindowStyle.None;
this.ResizeMode = System.Windows.ResizeMode.NoResize;
this.Left = 0.0;
this.Top = 0.0;
}
第二屏窗口 设置全屏:
/// <summary>
/// 设置全屏
/// </summary>
private void SetFullScreen()
{
this.WindowStyle = System.Windows.WindowStyle.None;
this.ResizeMode = System.Windows.ResizeMode.NoResize;
this.Left = 0.0;
this.Top = 0.0;
}
以上SetFullScreen()方法 均放置在 窗口 构造函数中