這個程序模彷了Windows7的桌面任務欄,當在桌面上雙擊某個快捷方式時,將打開一個新的子界面,並且在任務欄裡創建一個鏈接到此 界面的任務欄圖標,將鼠標移動到任務欄上的圖標時,將Popup出界面的實時圖像縮略圖,單擊任務欄圖標時,可讓界面最大化或最小化, 界面如下所示,源碼從這裡(http://files.cnblogs.com/5460600/StatusBar.rar)下載
做這個任務欄界面縮略圖時,使用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任務欄縮略圖的圓角
使用圓角
//給任務欄的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
{
BorderBrush = Brushes.Transparent,
BorderThickness = new Thickness(1),
CornerRadius = new CornerRadius(8),
Child = visualBorder,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
Background = this.FindResource("thumbnailBackground")
as ImageBrush
};
定義一個ToolTip用來宿主界面的縮略圖
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;
定義事件,子界面關閉時,從任務欄中移除此界面在任務欄上的圖標,當在任務欄圖標上單擊鼠標時,實現界面的最大化或者最小化
定義事件
//當打開的子界面關閉時需從任務欄移除此界面在任務欄上的圖標
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 邏輯樹的子葉點.