西西軟件園多重安全檢測下載網(wǎng)站、值得信賴的軟件下載站!
軟件
軟件
文章
搜索

首頁編程開發(fā)其它知識 → WPF 中編寫具有玻璃效果主題的程序技巧(Aero Glass)

WPF 中編寫具有玻璃效果主題的程序技巧(Aero Glass)

相關(guān)軟件相關(guān)文章發(fā)表評論 來源:西西整理時間:2011/4/24 7:50:02字體大。A-A+

作者:西西點擊:427次評論:0次標(biāo)簽: Aero

3D快速啟動Aero QLaunch1.2.22英文官方安裝版
  • 類型:系統(tǒng)其它大小:1.2M語言:英文 評分:5.0
  • 標(biāo)簽:
立即下載

Windows 7 操作系統(tǒng)默認(rèn)具有一款玻璃效果主題(Aero Glass)。如果選擇了該款主題,所有的應(yīng)用程序標(biāo)題欄都會處于玻璃透明效果(如下圖)。這個功能是由Desktop Window Manager(DWM)服務(wù)支持的。

  默認(rèn)情況下,我們編寫的應(yīng)用程序在Windows 7 中也只有標(biāo)題欄和窗口框架會具備玻璃效果,其他區(qū)域仍是不透明狀態(tài)(如下圖)。如果想將程序整體都改為上圖IE 窗口的效果,可以使用DWM API 將玻璃區(qū)域進(jìn)行擴(kuò)展。

首先,從dwmapi.dll 中調(diào)取DwmExtendFrameIntoClientArea 方法。

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

  創(chuàng)建方法ExtendAeroGlass 方法,可將WPF Window窗口的Aero Glass 區(qū)域擴(kuò)展。

private void ExtendAeroGlass(Window window)
{
try
{
// 為WPF程序獲取窗口句柄
IntPtr mainWindowPtr = new WindowInteropHelper(window).Handle;
HwndSource mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr);
mainWindowSrc.CompositionTarget.BackgroundColor = Colors.Transparent;

// 設(shè)置Margins
MARGINS margins = new MARGINS();

// 擴(kuò)展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;
}
}

  簡單制作一個WPF 界面。

<Window x:Class="WpfAeroGlass.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="layout">
<Button x:Name="btn" Content="Button" Margin="191,66,202,211" />
<CheckBox x:Name="checkBox" Content="Extend AeroGlass"
Click="CheckBox_Checked" Height="24" Width="121" />
</Grid>
</Window>

  補(bǔ)充CheckBox 點擊事件,在其中啟用ExtendAeroGlass 方法。

private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
if (checkBox.IsChecked.Value)
{
this.Background = Brushes.Transparent;
ExtendAeroGlass(this);
}
else
{
this.Background = Brushes.White;
}
}

演示效果

  運行程序后,默認(rèn)界面狀態(tài)。

  點擊"Extend AeroGlass" 選框,界面中<Grid> 也將呈現(xiàn)玻璃效果。

  Windows API

  通過Windows API Code Pack 可以對Aero Glass 效果進(jìn)行開啟或關(guān)閉。在程序中加入Microsoft.WindowsAPICodePack.Shell 命名空間,調(diào)整AeroGlassCompositioinEnabled 完成開/關(guān)Aero Glass的效果。

GlassWindow.AeroGlassCompositionEnabled = checkBox.IsChecked.Value;

    相關(guān)評論

    閱讀本文后您有什么感想? 已有人給出評價!

    • 8 喜歡喜歡
    • 3 頂
    • 1 難過難過
    • 5 囧
    • 3 圍觀圍觀
    • 2 無聊無聊

    熱門評論

    最新評論

    發(fā)表評論 查看所有評論(0)

    昵稱:
    表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
    字?jǐn)?shù): 0/500 (您的評論需要經(jīng)過審核才能顯示)