我有一個窗口,我想給一個玻璃寄宿生(沒有標題欄和不可調整大小),並遇到同樣的問題。你只能通過設置窗口的樣式來完成。我的解決方案是設置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>
它看起來很有前途,謝謝。現在我必須弄清楚如何使用它,但它看起來不錯;) – 2009-08-24 20:50:20
到目前爲止,沒有運氣,我的窗戶一直黑屏...... – 2009-08-24 21:59:24
好吧,明白了......我必須設置'CompositionTarget .BackgroundColor'爲'Transparent',和'AllowsTransparency'爲true。謝謝 ! – 2009-08-24 22:16:10