我有一个窗口,我只想给一个玻璃边框(没有标题栏和不可调整大小)并且遇到了和你一样的问题。您不能仅通过设置 Window 的样式来完成此操作。我的解决方案是设置 ResizeMode="CanResize" 和 WindowStyle="None" 然后处理 WM_NCHITTEST 事件以将可调整大小的边框命中转换为不可调整大小的边框命中。还需要修改 Window 的样式以禁用最大化和最小化(使用 Windows 快捷方式)和系统菜单:
private void Window_SourceInitialized(object sender, EventArgs e)
{
System.Windows.Interop.HwndSource source = (System.Windows.Interop.HwndSource)PresentationSource.FromVisual(this);
source.AddHook(new System.Windows.Interop.HwndSourceHook(HwndSourceHook));
IntPtr hWnd = new System.Windows.Interop.WindowInteropHelper(this).Handle;
IntPtr flags = GetWindowLongPtr(hWnd, -16 /*GWL_STYLE*/);
SetWindowLongPtr(hWnd, -16 /*GWL_STYLE*/, new IntPtr(flags.ToInt64() & ~(0x00010000L /*WS_MAXIMIZEBOX*/ | 0x00020000L /*WS_MINIMIZEBOX*/ | 0x00080000L /*WS_SYSMENU*/)));
}
private static IntPtr HwndSourceHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
switch (msg)
{
case 0x0084 /*WM_NCHITTEST*/:
IntPtr result = DefWindowProc(hwnd, msg, wParam, lParam);
if (result.ToInt32() >= 10 /*HTLEFT*/ && result.ToInt32() <= 17 /*HTBOTTOMRIGHT*/ )
{
handled = true;
return new IntPtr(18 /*HTBORDER*/);
}
break;
}
return IntPtr.Zero;
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr DefWindowProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex);
这为您在 Windows 7 中提供了一个适合通知区域弹出(例如时钟或音量弹出)的窗口。顺便说一句,您可以通过创建高度 44 的控件并设置其背景来重现弹出底部的阴影:
<Control.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="{x:Static SystemColors.GradientActiveCaptionColor}" Offset="0"/>
<GradientStop Color="{x:Static SystemColors.InactiveBorderColor}" Offset="0.1"/>
</LinearGradientBrush>
</Control.Background>