本主题演示如何将 Windows Vista 玻璃框扩展至 Windows Presentation Foundation (WPF) 应用程序的工作区。
![]() |
---|
此示例仅在运行已启用玻璃效果的桌面窗口管理器 (DWM) 的 Windows Vista 计算机上才会起作用。Windows Vista Home Basic 版本不支持透明玻璃效果。通常利用透明玻璃效果呈现的区域在其他版本的 Windows Vista 上呈现为不透明。 |
示例
下面的示例演示一个已扩展到 Internet Explorer 7 地址栏的玻璃框。
Internet Explorer,扩展的玻璃框位于地址栏后。
若要在 WPF 应用程序上扩展玻璃框,需要访问非托管的 API。 下面的代码示例为扩展玻璃框工作区所需的两个 API 执行平台调用 (pinvoke)。 每个 API 都是在名为 NonClientRegionAPI 的类中声明的。
<StructLayout(LayoutKind.Sequential)>
Public Structure MARGINS
Public cxLeftWidth As Integer ' width of left border that retains its size
Public cxRightWidth As Integer ' width of right border that retains its size
Public cyTopHeight As Integer ' height of top border that retains its size
Public cyBottomHeight As Integer ' height of bottom border that retains its size
End Structure
<DllImport("DwmApi.dll")>
Public Shared Function DwmExtendFrameIntoClientArea(ByVal hwnd As IntPtr, ByRef pMarInset As MARGINS) As Integer
End Function
[StructLayout(LayoutKind.Sequential)]
public struct MARGINS
{
public int cxLeftWidth; // width of left border that retains its size
public int cxRightWidth; // width of right border that retains its size
public int cyTopHeight; // height of top border that retains its size
public int cyBottomHeight; // height of bottom border that retains its size
};
[DllImport("DwmApi.dll")]
public static extern int DwmExtendFrameIntoClientArea(
IntPtr hwnd,
ref MARGINS pMarInset);
DwmExtendFrameIntoClientArea_udwm_dwmextendframeintoclientarea 是用于将框扩展到工作区中的 DWM 函数。 它有两个参数,一个窗口句柄,一个 MARGINS 结构。 MARGINS 用于告知 DWM 框扩展到工作区中时延伸的程度。
若要使用 DwmExtendFrameIntoClientArea 函数,必须获取一个窗口句柄。 在 WPF 中,可以通过 HwndSource 的 Handle 属性获取窗口句柄。 在下面的示例中,框扩展至窗口的 Loaded 事件的工作区。
void OnLoaded(object sender, RoutedEventArgs e)
{
try
{
// Obtain the window handle for WPF application
IntPtr mainWindowPtr = new WindowInteropHelper(this).Handle;
HwndSource mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr);
mainWindowSrc.CompositionTarget.BackgroundColor = Color.FromArgb(0, 0, 0, 0);
// Get System Dpi
System.Drawing.Graphics desktop = System.Drawing.Graphics.FromHwnd(mainWindowPtr);
float DesktopDpiX = desktop.DpiX;
float DesktopDpiY = desktop.DpiY;
// Set Margins
NonClientRegionAPI.MARGINS margins = new NonClientRegionAPI.MARGINS();
// Extend glass frame into client area
// Note that the default desktop Dpi is 96dpi. The margins are
// adjusted for the system Dpi.
margins.cxLeftWidth = Convert.ToInt32(5 * (DesktopDpiX / 96));
margins.cxRightWidth = Convert.ToInt32(5 * (DesktopDpiX / 96));
margins.cyTopHeight = Convert.ToInt32(((int)topBar.ActualHeight + 5) * (DesktopDpiX / 96));
margins.cyBottomHeight = Convert.ToInt32(5 * (DesktopDpiX / 96));
int hr = NonClientRegionAPI.DwmExtendFrameIntoClientArea(mainWindowSrc.Handle, ref margins);
//
if (hr < 0)
{
//DwmExtendFrameIntoClientArea Failed
}
}
// If not Vista, paint background white.
catch (DllNotFoundException)
{
Application.Current.MainWindow.Background = Brushes.White;
}
}
下面的示例演示一个简单的窗口,在此窗口中框扩展至工作区。 此框扩展到上边框的后面,上边框包含两个 TextBox 对象。
<Window x:Class="SDKSample.Window1"
xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
Title="Extended Glass in WPF" Height="300" Width="400"
Loaded="OnLoaded" Background="Transparent"
>
<Grid ShowGridLines="True">
<DockPanel Name="mainDock">
<!-- The border is used to compute the rendered height with margins.
topBar contents will be displayed on the extended glass frame.-->
<Border Name="topBar" DockPanel.Dock="Top" >
<Grid Name="grid">
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="100" Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" MinWidth="100" Margin="0,0,10,5">Path</TextBox>
<TextBox Grid.Column="1" MinWidth="75" Margin="0,0,0,5">Search</TextBox>
</Grid>
</Border>
<Grid DockPanel.Dock="Top" >
<Grid.ColumnDefinitions>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" AcceptsReturn="True"/>
</Grid>
</DockPanel>
</Grid>
</Window>
下图说明扩展至 WPF 应用程序的玻璃框。
扩展到 WPF 应用程序中的玻璃框。