Created
June 10, 2011 10:04
-
-
Save azyobuzin/1018577 to your computer and use it in GitHub Desktop.
Aero的に透明化するビヘイビア
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TransparentBackgroundBehavior : Behavior<Window> | |
{ | |
protected override void OnAttached() | |
{ | |
base.OnAttached(); | |
this.AssociatedObject.Loaded += this.AssociatedObject_Loaded; | |
} | |
protected override void OnDetaching() | |
{ | |
this.AssociatedObject.Loaded -= this.AssociatedObject_Loaded; | |
base.OnDetaching(); | |
} | |
public MARGINS Margins | |
{ | |
get { return (MARGINS)GetValue(MarginsProperty); } | |
set { SetValue(MarginsProperty, value); } | |
} | |
// Using a DependencyProperty as the backing store for Margins. This enables animation, styling, binding, etc... | |
public static readonly DependencyProperty MarginsProperty = | |
DependencyProperty.Register("Margins", typeof(MARGINS), typeof(TransparentBackgroundBehavior), new UIPropertyMetadata(new MARGINS() | |
{ | |
cxLeftWidth = -1, | |
cxRightWidth = 0, | |
cyTopHeight = 0, | |
cyBottomHeight = 0 | |
})); | |
private void AssociatedObject_Loaded(object sender, RoutedEventArgs e) | |
{ | |
var w = sender as Window; | |
w.Background = Brushes.Transparent; | |
if (Environment.OSVersion.Version.Major >= 6 && DwmIsCompositionEnabled()) | |
{ | |
var source = HwndSource.FromVisual(w) as HwndSource; | |
source.CompositionTarget.BackgroundColor = Color.FromArgb(0, 0, 0, 0); | |
DwmExtendFrameIntoClientArea(source.Handle, this.Margins); | |
} | |
else | |
{ | |
w.Background = SystemColors.GradientActiveCaptionBrush; | |
} | |
} | |
[DllImport("dwmapi.dll", PreserveSig = false)] | |
private static extern void DwmExtendFrameIntoClientArea( | |
IntPtr hWnd, MARGINS pMargins); | |
[DllImport("dwmapi.dll", PreserveSig = false)] | |
public static extern bool DwmIsCompositionEnabled(); | |
} | |
[StructLayout(LayoutKind.Sequential)] | |
class MARGINS | |
{ | |
public int cxLeftWidth; | |
public int cxRightWidth; | |
public int cyTopHeight; | |
public int cyBottomHeight; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment