WPF案例 (四) 模拟Windows7桌面任务栏
这个程序模彷了Windows7的桌面任务栏,当在桌面上双击某个快捷方式时,将打开一个新的子界面,并且在任务栏里创建一个链接到此界面的任务栏图标,将鼠标移动到任务栏上的图标时,将Popup出界面的实时图像缩略图,单击任务栏图标时,可让界面最大化或最小化,界面如下所示,源码从这里下载
做这个任务栏界面缩略图时,使用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 };
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")
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")
};
//对任务栏的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;
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;
}
};
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 逻辑树的子叶点,