简体   繁体   English

在 WPF 应用程序中支持 Windows 11 Snap Layout

[英]Support Windows 11 Snap Layout in WPF app

I want to enable SnapLayout for WPF.我想为 WPF 启用 SnapLayout。 Because I use Customized Window, according to the documentation , I have to do it myself因为我用的是Customized Window,根据文档,我得自己做

For Win32 apps, make sure you are responding appropriately to WM_NCHITTEST (with a return value of HTMAXBUTTON for the maximize/restore button).对于 Win32 应用程序,请确保您对 WM_NCHITTEST 做出适当的响应(最大化/恢复按钮的返回值为 HTMAXBUTTON)。

I used the following code我使用了以下代码

private const int HTMAXBUTTON = 9;

private IntPtr HwndSourceHook(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam, ref bool handled)
        {
            switch (msg)
            {
                case InteropValues.WM_NCHITTEST:
                    try
                    {
                        int x = lparam.ToInt32() & 0xffff;
                        int y = lparam.ToInt32() >> 16;
                        var rect = new Rect(_ButtonMax.PointToScreen(new Point()), new Size(_ButtonMax.Width, _ButtonMax.Height));
                        if (rect.Contains(new Point(x, y)))
                        {
                            handled = true;
                        }
                        return new IntPtr(HTMAXBUTTON);
                    }
                    catch (OverflowException)
                    {
                        handled = true;
                    }
                    break;
            }

            return IntPtr.Zero;
        }

SnapLayout is displayed well But the maximize button does not work and if I click on it, a button will be created next to it. SnapLayout 显示良好但最大化按钮不起作用,如果我点击它,旁边会创建一个按钮。 How can I solve this problem?我怎么解决这个问题?

在此处输入图片说明

Mark the WM_NCLBUTTONDOWN message as handled.WM_NCLBUTTONDOWN消息标记为已处理。 This prevents the classic button from showing.这可以防止经典按钮显示。

case InteropValues.WM_NCLBUTTONDOWN:
     handled = true;
     break;

Handle various WM_NC * mouse messages and using SendMessage to send related messages to the Window to mimic "client area" mouse messages with translated POINT values so that WPF can receive them.处理各种WM_NC * 鼠标消息并使用SendMessage将相关消息发送到 Window 以模拟具有翻译的 POINT 值的“客户区”鼠标消息,以便 WPF 可以接收它们。

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

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