Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
In questo argomento viene illustrato come estendere la cornice di vetro di Windows Vista nell'area client di un'applicazione Windows Presentation Foundation (WPF).
Annotazioni
Questo esempio funziona solo in un computer Windows Vista che esegue Desktop Window Manager (DWM) con vetro abilitato. Windows Vista Home Basic Edition non supporta l'effetto vetro trasparente. Le aree che solitamente vengono rese con l'effetto vetro trasparente su altre edizioni di Windows Vista sono rese opache.
Cornice in vetro estesa nella barra degli indirizzi
L'immagine seguente illustra la cornice di vetro estesa nella barra degli indirizzi di Internet Explorer 7:
Per estendere la cornice di vetro in un'applicazione WPF, è necessario accedere all'API non gestita. Nell'esempio di codice seguente viene utilizzata una funzione Platform Invoke (pinvoke) per richiamare le due API necessarie per ampliare il frame nell'area client. Ognuna di queste API viene dichiarata in una classe denominata NonClientRegionAPI.
[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);
<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
DwmExtendFrameIntoClientArea è la funzione DWM che estende il frame nell'area client. Accetta due parametri: un handle di finestra e una struttura MARGINI di. MARGINI viene utilizzato per indicare al DWM quanto la cornice debba essere estesa nell'area client.
Extended Glass Frame nell'evento Loaded
Per usare la funzione
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;
}
}
Cornice in vetro estesa nell'area cliente
L'esempio seguente mostra una finestra semplice in cui il frame viene esteso nell'area client. La cornice viene estesa dietro il bordo superiore che contiene i due oggetti TextBox.
<Window x:Class="SDKSample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://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>
L'immagine seguente illustra la cornice di vetro estesa in un'applicazione WPF:
Vedere anche
- Panoramica di Desktop Window Manager
- Panoramica dell'effetto sfocatura di Desktop Window Manager
- DwmExtendFrameIntoClientArea
.NET Desktop feedback