朋友说要做个可以用缩略图方式查看的ListView.初步看了下,用了半小时做了个简单的。毛病还有很多,例如速度啊。闪烁啊。不过总算是个开始吧。请大家指教!
using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Reflection;
[assembly: AssemblyTitle("无垠鞋业管理系统")]
[assembly: AssemblyDescription("无垠鞋业管理系统")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("泉州市无垠网络科技有限公司")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: AssemblyVersion("1.0.1")]
[assembly: AssemblyDelaySign(false)]
[assembly: AssemblyKeyFile("")]
[assembly: AssemblyKeyName("")]
namespace Wuyin.ShoesManager.Components
{
/// <summary>
/// ImageListView 的摘要说明。
/// </summary>
public class ImageListView : ListView
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
string path = null;
int width = 96;
bool userThumbnail = false;
[DllImport("shell32.dll")]
public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
[StructLayout(LayoutKind.Sequential)]
public struct SHFILEINFO
{
public IntPtr hIcon;
public IntPtr iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};
public const uint SHGFI_ICON = 0x100;
public const uint SHGFI_LARGEICON = 0x0; // 'Large icon
public const uint SHGFI_SMALLICON = 0x1; // 'Small icon
public enum FileFlags:uint
{
SHGFI_ICON=0x100,
SHGFI_LARGEICON=0x0,
SHGFI_SMALLICON=0x1,
}
public static System.Drawing.Icon GetIcon(string name)
{
string fName = name;
//MessageBox.Show(name);
//MessageBox.Show(f.FullName);
IntPtr hImgSmall;
SHFILEINFO shinfo = new SHFILEINFO();
hImgSmall = (SHGetFileInfo(fName,0, ref shinfo,(uint)Marshal.SizeOf(shinfo),SHGFI_ICON |SHGFI_LARGEICON));
System.Drawing.Icon myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon);
return myIcon;
}
[Description("缩略图的宽度")]
public int ThumbnailImageWidth
{
get
{
return width;
}
set
{
width=value;
}
}
[Description("使用缩略视图查看的目录路径")]
public string Path
{
get
{
return path;
}
set
{
path = value;
DrawThumbnailFromFolder(new PaintEventArgs(CreateGraphics(),ClientRectangle));
}
}
public enum view
{
ImageThumbnail,
userBaseView,
}
[Description("是否使用查看视图的缩略图方式")]
public bool UserThumbnailView
{
get
{
return userThumbnail;
}
set
{
userThumbnail=value;
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);
if(!userThumbnail)
return;
DrawThumbnailFromFolder(e);
}
Image GetThumbnailFromOtherFileOrFolder(string Name)
{
Image sImg = GetIcon(Name).ToBitmap();
float w = sImg.PhysicalDimension.Width;
float h = sImg.PhysicalDimension.Height;
float nw = 0;
float nh = 0;
if(w>h)
{
nw = width/2;
nh = h/w*nw;
}
else
{
nh = width/2;
nw = w/h*nh;
}
Bitmap bmp = new Bitmap(width,width);
Graphics g = Graphics.FromImage(bmp);
Brush brush = new SolidBrush(Color.FromArgb(236,233,216));
Pen pen = new Pen(brush,1);
g.DrawRectangle(pen,0,0,width-2,width-2);
g.DrawImage(sImg,((int)(width-nw)/2),((int)(width-nh)/2),(int)nw-2,(int)nh-2);
g.Save();
g.Dispose();
return bmp;
}
Image GetThumbnaillFromImageFile(string Name)
{
Image image = Image.FromFile(Name);
Graphics g = null;
Bitmap bmp = null;
float w = image.PhysicalDimension.Width;
float h = image.PhysicalDimension.Height;
float nw = 0;
float nh = 0;
if(w>h)
{
nw = width;
nh = h/w*nw;
}
else
{
nh = width;
nw = w/h*nh;
}
try
{
Image sImg = image.GetThumbnailImage((int)nw-2,(int)nh-2,null,new IntPtr());
bmp = new Bitmap(width,width);
g = Graphics.FromImage(bmp);
Brush brush = new SolidBrush(Color.FromArgb(236,233,216));
Pen pen = new Pen(brush,1);
g.DrawRectangle(pen,0,0,width-2,width-2);
g.DrawImage(sImg,((int)(width-nw)/2),((int)(width-nh)/2),(int)nw-2,(int)nh-2);
g.Save();
}
finally
{
image.Dispose();
g.Dispose();
}
return bmp;
}
public void DrawThumbnailFromFolder(PaintEventArgs e)
{
if(!Directory.Exists(path))
return;
Items.Clear();
if(LargeImageList!=null)
LargeImageList.Images.Clear();
LargeImageList = new ImageList(Container);
LargeImageList.ImageSize = new Size(width,width);
View=View.LargeIcon;
DirectoryInfo d = new DirectoryInfo(path);
foreach(DirectoryInfo di in d.GetDirectories())
Items.Add(new ListViewItem(new string[]{di.Name},LargeImageList.Images.Add(GetThumbnailFromOtherFileOrFolder(di.FullName),Color.Transparent)));
foreach(FileInfo f in d.GetFiles())
{
if(f.Extension.ToUpper()==".BMP" || f.Extension.ToUpper()==".JPG" || f.Extension.ToUpper()==".JPEG" || f.Extension.ToUpper()==".GIF" || f.Extension.ToUpper()==".PNG" || f.Extension.ToUpper()==".ICO")
Items.Add(new ListViewItem(new string[]{f.Name},LargeImageList.Images.Add(GetThumbnaillFromImageFile(f.FullName),Color.Transparent)));
else
Items.Add(new ListViewItem(new string[]{f.Name},LargeImageList.Images.Add(GetThumbnailFromOtherFileOrFolder(f.FullName),Color.Transparent)));
Application.DoEvents();
}
}
public ImageListView(System.ComponentModel.IContainer container)
{
///
/// Windows.Forms 类撰写设计器支持所必需的
///
container.Add(this);
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
public ImageListView()
{
///
/// Windows.Forms 类撰写设计器支持所必需的
///
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
}
}