求教多显示器下WPF自定义窗体的最大化问题!!!

cronos00 2015-11-26 09:57:06
问题是这样的,设置了WindowStyle="None" AllowsTransparency="True"之后,自己做了个标题栏,实现了缩放与最大化问题,但是最大化时会遮住任务栏。
然后就上网查了下, 这个问题还是很好解决的。
但是。。在多显示器情况下不行了
如果副显示器的分辨率比主显示器分辨率高的话,最大化时窗体仍然会盖住副显示器的任务栏。
下面上最大化的解决代码,求大神指教。。
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);
}
...全文
999 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Veary 2015-12-02
  • 打赏
  • 举报
回复
善用shell
cronos00 2015-11-26
  • 打赏
  • 举报
回复
继续补充,分别设置两个显示器为主显示器,在断点下可以看到最后mmi里面的数据是一模一样的。但是显示却有问题,,不知道为什么了
cronos00 2015-11-26
  • 打赏
  • 举报
回复
补充下,副显示器的任务栏被盖住,所以宽度上就会超出屏幕,显示不全了。不然盖住任务栏也无所谓了。
exception92 2015-11-26
  • 打赏
  • 举报
回复
这是我处理 全屏的 代码,可以实现分屏效果,你可以看着改改试试。

已 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()方法  均放置在 窗口 构造函数中
cronos00 2015-11-26
  • 打赏
  • 举报
回复
引用 3 楼 duanzi_peng 的回复:
在show 之前 获取 第二屏幕的 WorkingArea , 在窗体中 根据WorkingArea的top,left 来设置 位置,在根据WorkingArea的Width,Height 来设置大小。通过设置Height-=10 来缩小高度。
谢谢回复,在副屏幕上最大化时,这些数据都是正常的,高度也是减去了任务栏的高度,但显示出来就是不对,,很令人郁闷。 感觉有可能还是分辨率的问题,但不知道该怎么调试了
exception92 2015-11-26
  • 打赏
  • 举报
回复
在show 之前 获取 第二屏幕的 WorkingArea , 在窗体中 根据WorkingArea的top,left 来设置 位置,在根据WorkingArea的Width,Height 来设置大小。通过设置Height-=10 来缩小高度。

8,735

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧