您的位置:首页 > 编程语言 > C#

C# 分别在windows form和wpf中实现windows 7 玻璃(areo)效果

2011-09-27 13:49 579 查看
在windows7中大量使用了玻璃效果(也就是说Areo效果)感觉非常漂亮。平时自己做一些小东西也很想要这个效果,于是琢磨了一下。

1、在windows form程序中实现玻璃效果

这里要用到微软提供的Windows 7 API Code Pack

注:使用此API可以实现很多windows 7的特效(如:jumplist,areo,任务栏图标显示任务进度,等等。。。)

首先在程序中引入上面的API,然后只需要把我们的Form窗体继承自Microsoft.WindowsAPICodePack.Shell.GlassForm就可以了。

public partial class AeroForm : GlassForm


效果:



2、在wpf程序中实现玻璃效果

这里我们同样用系统提供的API来实现

/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private void button1_Click(object sender, RoutedEventArgs e)
{
this.Background = Brushes.Transparent;
ExtendAeroGlass(this);
}

[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;
}
}
}


效果和上面的图片一致。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: