WPFによるウールガラス効果の実現
12079 ワード
1と2は、Microsoft.WindowsAPICodePack.Shell.dllとusing System.Windows.Interopを参照する必要があり、DwmApi.dllバージョンのWindowsオペレーティングシステムでのみ使用できます.この2つの方法の共通の欠点は、フォームを起動すると点滅することです.
一、
二、
三、
この方法にはMicrosoft.Windows.Shell.dllが必要です 一閃の欠陥はない!
転載先:http://blog.csdn.net/kingscrown/article/details/7771094
一、
[StructLayout(LayoutKind.Sequential)]
public struct MARGINS
{
public int cxLeftWidth;
public int cxRightWidth;
public int cyTopHeight;
public int cyBottomHeight;
};
[DllImport("DwmApi.dll")]
public static extern int DwmExtendFrameIntoClientArea(
IntPtr hwnd,
ref MARGINS pMarInset);
private void ExtendAeroGlass(Window window)
{
try
{
// WPF
IntPtr mainWindowPtr = new WindowInteropHelper(window).Handle;
HwndSource mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr);
mainWindowSrc.CompositionTarget.BackgroundColor = Colors.Transparent;
// Margins
MARGINS margins = new MARGINS();
// Aero Glass
margins.cxLeftWidth = -1;
margins.cxRightWidth = -1;
margins.cyTopHeight = -1;
margins.cyBottomHeight = -1;
int hr = DwmExtendFrameIntoClientArea(mainWindowSrc.Handle, ref margins);
if (hr < 0)
{
MessageBox.Show("DwmExtendFrameIntoClientArea Failed");
}
}
catch (DllNotFoundException)
{
Application.Current.MainWindow.Background = Brushes.White;
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.Background = Brushes.Transparent;
ExtendAeroGlass(this);
}
二、
[StructLayout(LayoutKind.Sequential)]
public struct MARGINS
{
public MARGINS(Thickness t)
{
Left = (int)t.Left;
Right = (int)t.Right;
Top = (int)t.Top;
Bottom = (int)t.Bottom;
}
public int Left;
public int Right;
public int Top;
public int Bottom;
}
public class GlassHelper
{
[DllImport("dwmapi.dll", PreserveSig = false)]
static extern void DwmExtendFrameIntoClientArea(
IntPtr hWnd, ref MARGINS pMarInset);
[DllImport("dwmapi.dll", PreserveSig = false)]
static extern bool DwmIsCompositionEnabled();
public static bool ExtendGlassFrame(Window window, Thickness margin)
{
if (!DwmIsCompositionEnabled())
return false;
IntPtr hwnd = new WindowInteropHelper(window).Handle;
if (hwnd == IntPtr.Zero)
throw new InvalidOperationException(
"The Window must be shown before extending glass.");
// Set the background to transparent from both the WPF and Win32 perspectives
window.Background = Brushes.Transparent;
HwndSource.FromHwnd(hwnd).CompositionTarget.BackgroundColor = Colors.Transparent;
MARGINS margins = new MARGINS(margin);
DwmExtendFrameIntoClientArea(hwnd, ref margins);
return true;
}
}
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
GlassHelper.ExtendGlassFrame(this, new Thickness(-1));
}
三、
この方法にはMicrosoft.Windows.Shell.dllが必要です 一閃の欠陥はない!
xmlns:shell="clr-namespace:Microsoft.Windows.Shell;assembly=Microsoft.Windows.Shell"
<shell:WindowChrome.WindowChrome>
<shell:WindowChrome GlassFrameThickness="-1" ResizeBorderThickness="1"
CaptionHeight="15" CornerRadius="0" />
</shell:WindowChrome.WindowChrome>
<Window.Template>
<ControlTemplate TargetType="{x:Type Window}">
<Border BorderThickness="1">
<DockPanel>
<Grid x:Name="WindowHeader" DockPanel.Dock="Top" Height="15">
<TextBlock Text="{TemplateBinding Title}"/>
</Grid>
<ContentPresenter Margin="5,5,5,5"/>
</DockPanel>
</Border>
</ControlTemplate>
</Window.Template>
転載先:http://blog.csdn.net/kingscrown/article/details/7771094