wpf中无边框窗体的问题

无边框窗体最大化显示超出屏幕外的解决方案

相关:
https://stackoverflow.com/questions/12884987/wpf-borderless-window-on-maximize-exceeds-window-height-and-width?rq=1
Solved:
通过修改ResizeMode = ResizeMode.NoResize可以有效的解决该问题,但需要注意的是在其它窗口状态下ResizeMode需要恢复原先值。具体代码如下:

    private ResizeMode _preResizeMode;

    private void BtnMax_OnClick(object sender, RoutedEventArgs e)
    {
        _preResizeMode = this.ResizeMode;
        this.ResizeMode = ResizeMode.NoResize;
        this.WindowState = WindowState.Maximized;
    }

    private void BtnNormax_OnClick(object sender, RoutedEventArgs e)
    {
        this.ResizeMode = _preResizeMode;
        this.WindowState = WindowState.Normal;
    }

    private void BtnMinimal_OnClick(object sender, RoutedEventArgs e)
    {
        this.ResizeMode = _preResizeMode;
        this.WindowState = WindowState.Minimized;
    }

无边框窗体不摭挡桌面工具栏的代码

使用P/Invoke的方式

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Interop;

namespace testxx22
{
    public partial class MainWindow
    {
        void MainWindow_SourceInitialized(object sender, EventArgs e)
        {
            IntPtr handle = (new WindowInteropHelper(this)).Handle;
            HwndSource.FromHwnd(handle).AddHook(WindowProc);
        }

        private static IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            switch (msg)
            {
                case 0x0024:
                    WmGetMinMaxInfo(hwnd, lParam);
                    handled = true;
                    break;
            }

            return (IntPtr)0;
        }

        private static void WmGetMinMaxInfo(IntPtr hwnd, IntPtr lParam)
        {
            var mmi = (MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(MINMAXINFO));

            // Adjust the maximized size and position to fit the work area of the correct monitor
            int MONITOR_DEFAULTTONEAREST = 0x00000002;
            IntPtr monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);

            if (monitor != IntPtr.Zero)
            {
                var monitorInfo = new MONITORINFO();
                GetMonitorInfo(monitor, monitorInfo);
                RECT rcWorkArea = monitorInfo.rcWork;
                RECT rcMonitorArea = monitorInfo.rcMonitor;
                mmi.ptMaxPosition.x = Math.Abs(rcWorkArea.left - rcMonitorArea.left);
                mmi.ptMaxPosition.y = Math.Abs(rcWorkArea.top - rcMonitorArea.top);
                //下两行是用来控制不摭挡工具栏    
                mmi.ptMaxSize.x = Math.Abs(rcWorkArea.right - rcWorkArea.left);
                mmi.ptMaxSize.y = Math.Abs(rcWorkArea.bottom - rcWorkArea.top);
            }

            Marshal.StructureToPtr(mmi, lParam, true);
        }

        /// <summary>
        
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值