C# WPF Window is bigger than screen when maximized

PLEASE DON’T FLAG AS DUPLICATE! As I found the related Stackoverflow questions, but they didn’t work.

I am creating a window with WindowStyle="None" AllowTransparency="True". So I am trying to make a fully custom window. I tried to make the windows features working with it, like layout bar, animated state change.

I found this answer:

public void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{
    if (this.WindowState == WindowState.Maximized)
    {
        this.BorderThickness = new System.Windows.Thickness(8);
    }
    else
    {
        this.BorderThickness = new System.Windows.Thickness(0);
    }
}

While it is not bad, I would like to solve the root problem if I can. It is because I assume that it will be fixed, and don’t want to change everything back in each of my apps. Also, I don’t know if it’s a Win 11 related problem or other versions have too.

Also I found this answer:

IntPtr myStyle = new IntPtr(WS.WS_CAPTION | WS.WS_CLIPCHILDREN | WS.WS_MINIMIZEBOX | WS.WS_SYSMENU | WS.WS_SIZEBOX | WS.WS_MAXIMIZEBOX); // ADD  "| WS.WS_MAXIMIZEBOX " to enable snap bar
SetWindowLongPtr(new HandleRef(null, handle), GWL_STYLE, myStyle);

This is all and good, but when I add the WS.WS_MAXIMIZEBOX to the myStyle then if I Mmaximize the window, it will be 8 pixels bigger than the screen (minus the taskbar). Now I assume that I searched a lot and I could not find a working answer. On most places I found this answer to work (not for me):


private void WindowLoaded(object sender, RoutedEventArgs e)
{
    handle = new WindowInteropHelper(window).Handle;
    HwndSource.FromHwnd(handle).AddHook(new HwndSourceHook(WindowProc));

    IntPtr myStyle = new IntPtr(WS.WS_CAPTION | WS.WS_CLIPCHILDREN | WS.WS_MINIMIZEBOX | WS.WS_SYSMENU | WS.WS_SIZEBOX | WS.WS_MAXIMIZEBOX); // ADD  "| WS.WS_MAXIMIZEBOX " to enable snap bar
    SetWindowLongPtr(new HandleRef(null, handle), GWL_STYLE, myStyle);
}

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

public static void WmGetMinMaxInfo(IntPtr hwnd, IntPtr lParam)
{
    MINMAXINFO mmi = (MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(MINMAXINFO));
    int MONITOR_DEFAULTTONEAREST = 0x00000002;
    IntPtr monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
    if (monitor != IntPtr.Zero)
    {
        MONITORINFO monitorInfo = new MONITORINFO();
        GetMonitorInfo(monitor, monitorInfo);
        RECT rcWorkArea = monitorInfo.rcWork;
        RECT rcMonitorArea = monitorInfo.rcMonitor;
        mmi.ptMaxPosition.x = Math.Abs(rcWorkArea.left - rcMonitorArea.left) // + 1;
        mmi.ptMaxPosition.y = Math.Abs(rcWorkArea.top - rcMonitorArea.top) // + 1;
        mmi.ptMaxSize.x = Math.Abs(rcWorkArea.right - rcWorkArea.left) // - 1;
        mmi.ptMaxSize.y = Math.Abs(rcWorkArea.bottom - rcWorkArea.top) // - 1;
    }
    Marshal.StructureToPtr(mmi, lParam, true);
}

So when I remove the WS.WS_MAXIMIZEBOX style then it works.
But I need it for the snap bar.

So when I add it, it ONLY works if I either
mmi.ptMaxSize.y = Math.Abs(rcWorkArea.bottom - rcWorkArea.top) - {number here > 0};
mmi.ptMaxSize.x = Math.Abs(rcWorkArea.bottom - rcWorkArea.top) - {number here > 0};

OR
mmi.ptMaxPosition.y = Math.Abs(rcWorkArea.top - rcMonitorArea.top) + {number here > 0};
mmi.ptMaxPosition.x = Math.Abs(rcWorkArea.top - rcMonitorArea.top) + {number here > 0};

It works if I do any of above. But if I can, I would like not to create a pixel gap on any of the sides.
So I am here seeking for help. How can I make it work without the need of a gap.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật