WPF示例: 简单模拟Windows资源管理器

现附上主要代码:

using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Input;
using System.Windows.Controls;
using System.IO;
namespace xiaohai.ButtonExplorer
{

class MyButton : ContentControl
{
static RoutedEvent ClickEvent;
ImageBrush ibru;
//按钮背景画刷*****************************************************
BitmapImage bimg; //画刷使用的图像*************************************************
FileSystemInfo fileinfo;
static BitmapImage bimg1, bimg2, bimg3, bimg4,bimg5,bimg6,bimg7;

static MyButton() //注册Click事件***************************************************
{
ClickEvent
= EventManager.RegisterRoutedEvent("Click", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyButton));

bimg1
= new BitmapImage(new Uri("Flowers.ico", UriKind.Relative)); //文件夹所使用的图像******************
bimg2 = new BitmapImage(new Uri("chinaz16.ico", UriKind.Relative)); //其它使用的图像***********************
bimg3 = new BitmapImage(new Uri("MPEG.ico", UriKind.Relative)); //视频使用的图像***********************
bimg4 = new BitmapImage(new Uri("iTunes.ico", UriKind.Relative)); //音乐使用的图像***********************
bimg5 = new BitmapImage(new Uri("Ps.ico", UriKind.Relative)); //程序使用的图像***********************
bimg6 = new BitmapImage(new Uri("zip.ico", UriKind.Relative)); //压缩文件使用的图像***********************
bimg7 = new BitmapImage(new Uri("jpg.ico", UriKind.Relative)); //图片使用的图像***********************

}
public MyButton() : this(new DirectoryInfo(@"e:\"))
//无参初始化***************************************************************
{
FontSize
= 48;


}
public MyButton(FileSystemInfo info) //单个参数初始化*****************************************************
{
FontSize
= 48; FontFamily = new FontFamily("王羲之行书体"); Foreground = Brushes.Green;
ibru
= new ImageBrush();
this.fileinfo = info; //控制按钮的背景画刷*************************************
if (info is DirectoryInfo)
bimg
= bimg1;
else if (info is FileInfo)
{
if (info.FullName.EndsWith(".mp3"))
bimg
= bimg4;
else if ((info.FullName.EndsWith(".rmvb")) | (info.FullName.EndsWith(".flv")) | (info.FullName.EndsWith(".f4v")))
bimg
= bimg3;
else if (info.FullName.EndsWith(".exe"))
bimg
= bimg5;
else if (info.FullName.EndsWith(".rar"))
bimg
= bimg6;
else if (info.FullName.EndsWith(".jpg"))
bimg
= bimg7;
else
bimg
= bimg2;
}


ibru.ImageSource
= bimg;
this.fileinfo = info; //控制按钮上显示的内容******************************************************
if (info.Name.Length > 3)
Content
= info.Name.Substring(0, 3);
else
Content
= info.Name;

ToolTip t
= new ToolTip(); //为按钮添加提示信息*****************************

ToolTip
= t;
t.FontSize
= 32;
t.Foreground
= Brushes.Red;
if (info is FileInfo & info.FullName.EndsWith(".jpg")) //如果是图像文件,则显示初略图****************
{
Image img
= new Image();
img.Width
= img.Height = 100;
img.Stretch
= Stretch.UniformToFill;
img.Source
= new BitmapImage(new Uri(info.FullName, UriKind.Absolute));
t.Content
= img;
}
else
{
t.Content
= info.Name;
}

if (info is DirectoryInfo)
FontWeight
= FontWeights.Bold;

Margin
= new Thickness(30);

}

public MyButton(FileSystemInfo info, string str) :this(info ) //两个参数初始化***********************
{
Content
= str;
}

void Button_Click(object sender, RoutedEventArgs e)
{
if (fileinfo is FileInfo)
System.Diagnostics.Process.Start(fileinfo.FullName);
//运行文件***************************
else if (fileinfo is DirectoryInfo)
{
DirectoryInfo dir
= fileinfo as DirectoryInfo;
Panel p
= Parent as Panel;
Application.Current.MainWindow.Title
= dir .FullName ;
p.Children.Clear();

if (dir.Parent != null)
p.Children.Add(
new MyButton(dir.Parent, "..")); //设置返回上一级按钮*****************


foreach( FileSystemInfo info in dir.GetFileSystemInfos())
{
p.Children.Add(
new MyButton(info)); //加入下一级按钮********************************
}
p.Children.Add(
new MyButton(new DirectoryInfo(@"c:\")));
p.Children.Add(
new MyButton(new DirectoryInfo(@"e:\")));

}

}
//按钮的Click路由事件****************************************
public event RoutedEventHandler Click
{
add
{
AddHandler(ClickEvent ,value );
}
remove
{
RemoveHandler(ClickEvent, value);
}
}
//按钮的Click路由事件****************************************
void OnClick()
{

RoutedEventArgs e
= new RoutedEventArgs();
e.RoutedEvent
= ClickEvent;
e.Source
= this;
RaiseEvent(e);

}

//重载绘制方法************************************************************
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
drawingContext.DrawRoundedRectangle(ibru,
new Pen(SystemColors.ControlBrush, 1), new Rect(new Point(0, 0), new Size(128, 128)), 6, 6);
FormattedText f
=new FormattedText("**" ,System .Globalization .CultureInfo .CurrentCulture ,
FlowDirection .LeftToRight ,
new Typeface (new FontFamily ("Times New Roman"),FontStyles .Normal ,FontWeights .Black ,
FontStretches .Normal ),FontSize ,Foreground );


}
//重载左边按钮按下事件**********************************************
protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnPreviewMouseLeftButtonDown(e);
bimg
= new BitmapImage(new Uri("chinaz13.ico", UriKind.Relative));
ibru.ImageSource
= bimg;
}
//重载UP****************************************************************************
protected override void OnPreviewMouseLeftButtonUp(MouseButtonEventArgs e)
{
base.OnPreviewMouseLeftButtonUp(e);
if (fileinfo is DirectoryInfo)
bimg
= bimg1;
else if (fileinfo is FileInfo)
{
if (fileinfo.FullName.EndsWith(".mp3"))
bimg
= bimg4;
else if ((fileinfo.FullName.EndsWith(".rmvb")) | (fileinfo.FullName.EndsWith(".flv")) | (fileinfo.FullName.EndsWith(".f4v")))
bimg
= bimg3;
else if (fileinfo.FullName.EndsWith(".exe"))
bimg
= bimg5;
else if (fileinfo.FullName.EndsWith(".rar"))
bimg
= bimg6;
else if (fileinfo.FullName.EndsWith(".jpg"))
bimg
= bimg7;
else
bimg
= bimg2;
}
ibru.ImageSource
= bimg;

Button_Click(
null, null);

}


}
}

  效果图:*****************************************************************************

 

     

WPF示例: 简单模拟Windows资源管理器_第1张图片

 

WPF示例: 简单模拟Windows资源管理器_第2张图片

 

 

WPF示例: 简单模拟Windows资源管理器_第3张图片

你可能感兴趣的:(windows)