天天看點

WPF案例 (四) 模拟Windows7桌面工作列

WPF案例 (四) 模拟Windows7桌面工作列

   做這個工作列界面縮略圖時,使用VisualBrush将子界面的實時圖像填充到Rectangle

1  Rectangle emptyRectangle = new Rectangle

2   {

3       Width = 250D,

4       Height = 130D,

5       Fill = new VisualBrush { Visual = child },

6       Margin = new Thickness(2)

7   };

   又分别使用了兩個Border來模彷Windows7工作列縮略圖的圓角

WPF案例 (四) 模拟Windows7桌面工作列
WPF案例 (四) 模拟Windows7桌面工作列

使用圓角

 //給工作列的Popup界面縮略圖應用圓角

   Border visualBorder = new Border

    {

       BorderBrush = Brushes.Transparent,

       BorderThickness = new Thickness(1),

       CornerRadius = new CornerRadius(10),

       Margin = new Thickness(10),

       Height = 150D,

       Width = 270D,

       Child = emptyRectangle,

       HorizontalAlignment = HorizontalAlignment.Center,

       VerticalAlignment = VerticalAlignment.Center,

       Background = this.FindResource("transparentColor") 

                     as LinearGradientBrush

     };

 //對工作列的Popup界面縮略圖的背景應用圓角

   Border host = new Border

       CornerRadius = new CornerRadius(8),

       Child = visualBorder,

       Background = this.FindResource("thumbnailBackground") 

                          as ImageBrush

    };

    定義一個ToolTip用來宿主界面的縮略圖

WPF案例 (四) 模拟Windows7桌面工作列
WPF案例 (四) 模拟Windows7桌面工作列

ToolTip

 //添加一ToolTip,用來宿主子界面的宿略圖

  ToolTip visualToolTip = new ToolTip

  {

      Content = host,

      Background = Brushes.Transparent,

      BorderBrush = Brushes.Transparent,

      Placement = PlacementMode.Top,

      HorizontalContentAlignment = HorizontalAlignment.Center,

      VerticalContentAlignment = VerticalAlignment.Center,

      HasDropShadow = false,

      VerticalOffset=3

  };

 //設計ToolTip的Popup延遲時間為20ms

 ToolTipService.SetInitialShowDelay(thumbnail, 20);

 thumbnail.ToolTip = visualToolTip;

   定義事件,子界面關閉時,從工作列中移除此界面在工作列上的圖示,當在工作列圖示上單擊滑鼠時,實作界面的最大化或者最小化

WPF案例 (四) 模拟Windows7桌面工作列
WPF案例 (四) 模拟Windows7桌面工作列

定義事件

//當打開的子界面關閉時需從工作列移除此界面在工作列上的圖示

  child.Closed += delegate

   {

     this.statusBar.Children.Remove(hostBorder);

   };

//在工作列上單擊任務圖示時,最大化或者最小化子界面

  hostBorder.MouseLeftButtonDown += delegate(object sender, MouseButtonEventArgs e)

      if (e.ClickCount == 1)

         {

           if (child.WindowState == WindowState.Minimized)

             {

                //正常顯示子界面

                   child.WindowState = WindowState.Normal;

                 child.Topmost = true;

             }

            else

                //最小化子界面

                  child.WindowState = WindowState.Minimized;

         }

   這個程式實作起來很簡單,但需要注意的是往工作列裡添加子界面工作列圖示時,應該以從上往下的順序添加,這樣可以避免重複Rendering 邏輯樹的子葉點,