我有一个窗口,想要给它添加一个玻璃边框(没有标题栏和不可调整大小),遇到了与您相同的问题。仅通过设置窗口的样式是无法实现的。我的解决方案是将ResizeMode设置为“CanResize”,WindowStyle设置为“None”,然后处理WM_NCHITTEST事件以将可调整大小的边框命中转换为不可调整大小的边框命中。还需要修改窗口的样式以禁用最大化和最小化(使用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>
CompositionTarget.BackgroundColor
设置为Transparent
,并将AllowsTransparency
设置为true。谢谢! - Thomas Levesque