我已经使用 WPF Shell 集成库 ( http://archive.msdn.microsoft.com/WPFShell ) 实现了自定义窗口镶边。
Chrome 一直有效,直到我将 ReSizeMode 设置为 NoResize。然后,如果将鼠标悬停在关闭按钮上,我注意到底层系统关闭按钮显示在自定义镀铬关闭按钮下。
预期的行为是底层的关闭按钮永远不会出现。如果我在桌面上移动窗口或选择另一个窗口并返回到这个窗口,我注意到系统关闭按钮再次被隐藏。因此,可能本质上是(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;
}
任何想法如何解决这一问题?
非常感谢,阿琼