1

我已经使用 WPF Shell 集成库 ( http://archive.msdn.microsoft.com/WPFShell ) 实现了自定义窗口镶边。

Chrome 一直有效,直到我将 ReSizeMode 设置为 NoResize。然后,如果将鼠标悬停在关闭按钮上,我注意到底层系统关闭按钮显示在自定义镀铬关闭按钮下。自定义 Chrome 按钮部分隐藏系统关闭按钮

预期的行为是底层的关闭按钮永远不会出现。如果我在桌面上移动窗口或选择另一个窗口并返回到这个窗口,我注意到系统关闭按钮再次被隐藏。因此,可能本质上是(1)应用程序首次启动和(2)ResizeMode = NoResize 时。

我的第一个怀疑是我们如何处理自定义 Chrome 中的 WM.NCHITTEST。如果我修改这个函数以返回 HClient,那么这个问题就解决了。但是,我失去了拖放以及右键单击标题栏的能力。

这是来自 WindowsChromeWorker 类的 WM.NCHITTEST 消息处理程序的代码。

    private IntPtr _HandleNCHitTest( WM uMsg, IntPtr wParam, IntPtr lParam, out bool handled ) {
        IntPtr lRet = IntPtr.Zero;
        handled = false;

        // Give DWM a chance at this first.
        if ( Utility.IsOSVistaOrNewer && _chromeInfo.GlassFrameThickness != default( Thickness ) && _isGlassEnabled ) {
            // If we're on Vista, give the DWM a chance to handle the message first.
            handled = NativeMethods.DwmDefWindowProc( _hwnd, uMsg, wParam, lParam, out lRet );
        }


        // Handle letting the system know if we consider the mouse to be in our effective non-client area.
        // If DWM already handled this by way of DwmDefWindowProc, then respect their call.
        if ( IntPtr.Zero == lRet ) {
            var mousePosScreen = new Point( Utility.GET_X_LPARAM( lParam ), Utility.GET_Y_LPARAM( lParam ) );
            Rect windowPosition = _GetWindowRect();

            HT ht = _HitTestNca(
                DpiHelper.DeviceRectToLogical( windowPosition ),
                DpiHelper.DevicePixelsToLogical( mousePosScreen ) );

            // Don't blindly respect HTCAPTION.
            // We want UIElements in the caption area to be actionable so run through a hittest first.
            if ( ht != HT.CLIENT) {
                Point mousePosWindow = mousePosScreen;
                mousePosWindow.Offset( -windowPosition.X, -windowPosition.Y );
                mousePosWindow = DpiHelper.DevicePixelsToLogical( mousePosWindow );
                IInputElement inputElement = _window.InputHitTest( mousePosWindow );
                if ( inputElement != null && WindowChrome.GetIsHitTestVisibleInChrome( inputElement ) ) {
                    ht = HT.CLIENT;
                }
            }
            //handled = false;
            handled = true;
            lRet = new IntPtr((int)ht);
        }
        return lRet;
    }

    private static readonly HT[,] _HitTestBorders = new[,]
    {
        { HT.TOPLEFT,    HT.TOP,     HT.TOPRIGHT    },
        { HT.LEFT,       HT.CLIENT,  HT.RIGHT       },
        { HT.BOTTOMLEFT, HT.BOTTOM,  HT.BOTTOMRIGHT },
    };

    private HT _HitTestNca( Rect windowPosition, Point mousePosition ) {
        // Determine if hit test is for resizing, default middle (1,1).
        int uRow = 1;
        int uCol = 1;
        bool onResizeBorder = false;

        //if (_window.ResizeMode == ResizeMode.NoResize)
          //  _chromeInfo.ResizeBorderThickness = new Thickness(0);

        // Determine if the point is at the top or bottom of the window.
        if ( mousePosition.Y >= windowPosition.Top && mousePosition.Y < windowPosition.Top + _chromeInfo.ResizeBorderThickness.Top + _chromeInfo.CaptionHeight ) {
            onResizeBorder = ( mousePosition.Y < ( windowPosition.Top + _chromeInfo.ResizeBorderThickness.Top ) );
            uRow = 0; // top (caption or resize border)
        } else if ( mousePosition.Y < windowPosition.Bottom && mousePosition.Y >= windowPosition.Bottom - (int)_chromeInfo.ResizeBorderThickness.Bottom ) {
            uRow = 2; // bottom
        }

        // Determine if the point is at the left or right of the window.
        if ( mousePosition.X >= windowPosition.Left && mousePosition.X < windowPosition.Left + (int)_chromeInfo.ResizeBorderThickness.Left ) {
            uCol = 0; // left side
        } else if ( mousePosition.X < windowPosition.Right && mousePosition.X >= windowPosition.Right - _chromeInfo.ResizeBorderThickness.Right ) {
            uCol = 2; // right side
        }

        // If the cursor is in one of the top edges by the caption bar, but below the top resize border,
        // then resize left-right rather than diagonally.
        if ( uRow == 0 && uCol != 1 && !onResizeBorder ) {
            uRow = 1;
        }

        HT ht = _HitTestBorders[uRow, uCol];

        if ( ht == HT.TOP && !onResizeBorder) {
            ht = HT.CAPTION;
        }

        return ht;
    }

任何想法如何解决这一问题?

非常感谢,阿琼

4

1 回答 1

3

所有仪式..我确实解决了这个问题。

首先,我最初的怀疑是错误的。WM.NCHITTEST 消息的处理正确。这真的是一个窗口样式的问题。

最好的解决方案是隐藏系统关闭按钮,让 Chrome 关闭按钮代替它工作。但是您在网上找到的解决方案,即在窗口样式中切换 SYSMENU 位标志http://winsharp93.wordpress.com/2009/07/21/wpf-hide-the-window-buttons-minimize-restore-and- close-and-the-icon-of-a-window/在我的情况下不起作用。

关闭按钮是隐藏的,但尽管 ResizeMode 设置为 NoResize,但我注意到 Resize Cursor 和 Resize Menu { Max\Min\Restore} 都已启用。

经过几个小时的反复试验,我想出了这段代码:

    //This property descriptor is used to hook-onto the resizemode change notification

      private void Window_Loaded( object sender, System.Windows.RoutedEventArgs e)
      {
      // When the default handling of ResizeMode = NoResize causes problems - this is why custom handling is required.
    System.ComponentModel.DependencyPropertyDescriptor _resizeModePropertyDescriptor;

     _resizeModePropertyDescriptor = System.ComponentModel.DependencyPropertyDescriptor.FromProperty(Window.ResizeModeProperty,
            typeof(Window));
        _resizeModePropertyDescriptor.AddValueChanged(this._window, new EventHandler(this._Window_ResizeModePropertyChanged));
      }

    /// <summary>
    /// This property change handler only reacts when the ReSizeMode is set to NoResize.
    /// In the default window style when Resize = NoResize, causes the underlying system close button to show up under the Chrome Close Button.
    /// This is a fix to handle that problem. [ Refer to defect #5134 for further details.
    /// </summary>
    /// <param name="sender">ChromeWorker object</param>
    /// <param name="e">Event Args - Not really used</param>
    private void _Window_ResizeModePropertyChanged(object sender, EventArgs e)
    {
        if (_window.ResizeMode == ResizeMode.NoResize)
        {
            //Got these styles merely by trial and error.
            _ModifyStyle(
                WS.SYSMENU | WS.DLGFRAME | WS.BORDER | WS.CLIPCHILDREN | WS.CLIPSIBLINGS,  //Styles to Remove
                WS.POPUPWINDOW);                                                            //Style to Add
        }
    }

    /// <summary>Add and remove a native WindowStyle from the HWND.</summary>
    /// <param name="removeStyle">The styles to be removed.  These can be bitwise combined.</param>
    /// <param name="addStyle">The styles to be added.  These can be bitwise combined.</param>
    /// <returns>Whether the styles of the HWND were modified as a result of this call.</returns>
    private bool _ModifyStyle( WS removeStyle, WS addStyle ) {
        Assert.IsNotDefault( _hwnd );
        var dwStyle = (WS)NativeMethods.GetWindowLongPtr( _hwnd, GWL.STYLE ).ToInt32();
        var dwNewStyle = ( dwStyle & ~removeStyle ) | addStyle;
        if ( dwStyle == dwNewStyle ) {
            return false;
        }

        NativeMethods.SetWindowLongPtr( _hwnd, GWL.STYLE, new IntPtr( (int)dwNewStyle ) );
        return true;
    }

您也可以使用 SourceInitialized 事件进行接线,但我还没有真正测试过。而已!这记录了对 Window Chrome 的紧张探索一周!

我不得不承认我有点好奇——在 WPF 上工作这么久并没有让我意识到那里有一个完整的 win32 世界,它更强大(也令人沮丧!)

于 2012-03-25T17:21:55.193 回答