简体   繁体   English

是否可以通过 PInvoke 指定 WPF 窗口的 MaximizedBounds?

[英]Is it possible to specify the MaximizedBounds of a WPF window via PInvoke?

I'm having trouble with my borderless WPF window.我的无边框 WPF 窗口有问题。 Specifically, it's maximizing over top the windows taskbar, and also maximizes with a -7 margin, making the window maximize beyond the screen by 7 pixels in each direction.具体来说,它在窗口任务栏上方最大化,并且还以 -7 边距最大化,使窗口在每个方向上超出屏幕 7 个像素。

This would be easily solved in winforms by setting the MaximizedBounds property of the window, but WPF does not have this property.这可以通过设置窗口的 MaximizedBounds 属性在 winforms 中轻松解决,但 WPF 没有此属性。

I tried solving this intercepting the maximize message via WndProc and setting the size/position manually, but this had the issue of overwriting the RestoreRegion to the maximized size/position, removing the ability to restore the window.我尝试通过 WndProc 拦截最大化消息并手动设置大小/位置来解决这个问题,但这存在将 RestoreRegion 覆盖为最大化大小/位置的问题,从而消除了恢复窗口的能力。

So, I thought that I might be able to set the MaximizedBounds of the window via PInvoke.所以,我想我也许可以通过 PInvoke 设置窗口的 MaximizedBounds。

This page implies that it can be done with Win32 SetWindowPlacement... It takes a structure called a WINDOWPLACEMENT: 这个页面暗示它可以用 Win32 SetWindowPlacement 来完成......它采用一个称为 WINDOWPLACEMENT 的结构:

    [Serializable]
    [StructLayout(LayoutKind.Sequential)]
    public struct WINDOWPLACEMENT
    {
        public int length;
        public int flags;
        public int showCmd;
        public POINT minPosition;
        public POINT maxPosition;
        public RECT normalPosition;
    } 

I think that I just need to set the Rectanle and a flag to specify that it's for the MaximizedBound property, but I can't find any example of this online, and none of the flags I've found look like it would accomplish this.我认为我只需要设置 Rectanle 和一个标志来指定它是用于 MaximizedBound 属性的,但是我在网上找不到任何这样的例子,而且我发现的所有标志看起来都不会完成这个。 Am I barking up the wrong tree?我在吠叫错误的树吗? If so, is there any other way to specify the MaximizedBound (or similar) that I've overlooked?如果是这样,有没有其他方法可以指定我忽略的 MaximizedBound (或类似的)?

I'd try something like this before going down the P/Invoke route - there is no WM_SETMINMAXINFO message that I'm aware of, so you'd probably need to override your WinProc to handle the WM_GETMINMAXINFO and return bogus data....but anyways, try this first, see if it works for you:在走 P/Invoke 路线之前,我会尝试这样的事情 - 我知道没有WM_SETMINMAXINFO消息,所以你可能需要覆盖你的 WinProc 来处理WM_GETMINMAXINFO并返回虚假数据......但无论如何,先试试这个,看看它是否适合你:

public class ConstrainedWindow : Window
{
    public ConstrainedWindow()
    {
        this.MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
        this.MaxWidth = SystemParameters.MaximizedPrimaryScreenWidth;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM