Search This Blog

Tuesday, November 10, 2009

Extend Glass Frame Into a WPF Application

参考链接:

http://msdn.microsoft.com/en-us/library/ms748975.aspx

环境

  Windows Vista + Visual Studio 2008

步骤

1. 创建一个C# WPF项目ExtendVistaGlassFrame。

2. 修改XAML文件如下 (注意x:Class的值换成自己的类名):

<Window x:Class="ExtendVistaGlassFrame.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.RowDefinitions>
                    <RowDefinition Height="53*" />
                    <RowDefinition Height="180.04*" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <TextBox AcceptsReturn="True" Grid.RowSpan="2" />
            </Grid>
        </DockPanel>
    </Grid>
</Window>

3. 刷新XAML的设计视图。 点击窗口生成onLoad方法。全文如下。编辑之外需要在引用中添加System.Drawing.dll。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Interop;

namespace ExtendVistaGlassFrame
{
    /// <summary>
    /// Window1.xaml の相互作用ロジック
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

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

3. 添加一个空类, 类名为NonClientRegionAPI.cs。文件内容如下。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace ExtendVistaGlassFrame
{
    class 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);
    }
}

4. 执行。搞定。

No comments:

Post a Comment