个人对于实现资源管理器功能的看法
2007-04-02 10:45
537 查看
使用exptreelib.dll 控件,实现资源管理器左侧功能。
private ExpTreeLib.ExpTree expTree1;
关于资源管理器的实现,研究了一通,最后还是用了System.Windows.Forms.:FolderBrowserDialog,
有句话说FolderBrowserDialog 够用,就直接用。
至于放弃资源管理器代码实现的原因是,在treeview 双击树接点,打开一个文件夹的时候,如果该文件夹下有很多.doc文件(2544个,共36.6 MB)的时候抛异常。这个异常,我当时没能解决掉。
。为了赶时间,最后放弃了资源管理器模式。
protected virtual void SetIcon(ImageList imageList,string FileName,bool tf)
{
SHFILEINFO fi=new SHFILEINFO();
if(tf==true)
{
int iTotal=(int)SHGetFileInfo(FileName,0,ref fi,100, 16640);//SHGFI_ICON|SHGFI_SMALLICON
try
{
if(iTotal >0)
{
Icon ic=Icon.FromHandle(fi.hIcon);
imageList.Images.Add(ic);
//return ic;
}
}
catch(Exception ex)
{ MessageBox.Show(ex.Message,"2错误提示",0,MessageBoxIcon.Error);}
}
else
{
int iTotal=(int)SHGetFileInfo(FileName,0,ref fi,100, 257);
try
{
if(iTotal >0)
{
Icon ic=Icon.FromHandle(fi.hIcon);
imageList.Images.Add(ic);
//return ic;
}
}
catch(Exception ex)
{ MessageBox.Show(ex.Message,"3错误提示",0,MessageBoxIcon.Error);}
}
// return null;
}
异常信息:
传递到icon的win32句柄无效或者类型错误。
按一个同仁的相同的异常开帖问题解决方法:
protected virtual Icon myExtractIcon(string FileName,int iIndex)
{
try
{
IntPtr hIcon=(IntPtr)ExtractIcon(this.Handle,FileName,iIndex);
if(! hIcon.Equals(null))
{
Icon icon=Icon.FromHandle(hIcon);
return icon;
}
}
catch(Exception ex)
{ MessageBox.Show(ex.Message,"1错误提示",0,MessageBoxIcon.Error);}
return null;
}
用if(hIcon != System.IntPtr.Zero) 替换 if(! hIcon.Equals(null)) 无作用。
2007年4月4日16时,忙了一天总算是把异常给处理掉了。辛苦,迷茫,以及解决问题后的兴奋,这或许就是程序员可生活吧,也是我们的追求与职业快乐吧。好了。闲话少说了,我最近越来越唠叨了。
在项目中添加如下代码:
//we need this function to release the unmanaged resource,
//the unmanaged resource will be
//copies to a managed one and it will be returned.
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public extern static bool DestroyIcon(IntPtr handle);
/// <summary>
/// 设置图标,把得到的图标放入一个ImageList
/// </summary>
/// <param name="imageList">ImageList,可以new一个</param>
/// <param name="FileName">文件名</param>
/// <param name="tf"></param>
protected virtual void SetIcon(ImageList imageList,string FileName,bool tf)
{
……
Icon ic=Icon.FromHandle(fi.hIcon);
imageList.Images.Add(ic); //添加到ImageList,可放图标和图片,只做一个转换用
DestroyIcon(fi.hIcon); // Cleanup
else
{
……
Icon ic=Icon.FromHandle(fi.hIcon);
imageList.Images.Add(ic);
DestroyIcon(fi.hIcon); // Cleanup
}
}
……
}
因使用ExtractIcon(),You must destroy the icon handle returned by ExtractIcon by calling the DestroyIcon function.
故添加了一个DestroyIcon 函数,它以前一般用于“销毁一个图标,释放这个图标占用的内存”。我觉得在一个图标被获得以后就立即这样做(销毁图标)真是个好主意(因为这个类将以各种各样不同地方式使用)。一旦需要GC就清理掉这个图标,或者被存于一个ImageList。
参考资料:
ExtractIcon
http://msdn2.microsoft.com/en-us/library/ms648068.aspx
BOOL DestroyIcon(HICON hIcon);
见http://msdn2.microsoft.com/en-us/library/ms648063.aspx
http://www.codeproject.com/上的
Obtaining (and managing) file and folder icons using SHGetFileInfo in C#
private ExpTreeLib.ExpTree expTree1;
关于资源管理器的实现,研究了一通,最后还是用了System.Windows.Forms.:FolderBrowserDialog,
有句话说FolderBrowserDialog 够用,就直接用。
至于放弃资源管理器代码实现的原因是,在treeview 双击树接点,打开一个文件夹的时候,如果该文件夹下有很多.doc文件(2544个,共36.6 MB)的时候抛异常。这个异常,我当时没能解决掉。
。为了赶时间,最后放弃了资源管理器模式。
protected virtual void SetIcon(ImageList imageList,string FileName,bool tf)
{
SHFILEINFO fi=new SHFILEINFO();
if(tf==true)
{
int iTotal=(int)SHGetFileInfo(FileName,0,ref fi,100, 16640);//SHGFI_ICON|SHGFI_SMALLICON
try
{
if(iTotal >0)
{
Icon ic=Icon.FromHandle(fi.hIcon);
imageList.Images.Add(ic);
//return ic;
}
}
catch(Exception ex)
{ MessageBox.Show(ex.Message,"2错误提示",0,MessageBoxIcon.Error);}
}
else
{
int iTotal=(int)SHGetFileInfo(FileName,0,ref fi,100, 257);
try
{
if(iTotal >0)
{
Icon ic=Icon.FromHandle(fi.hIcon);
imageList.Images.Add(ic);
//return ic;
}
}
catch(Exception ex)
{ MessageBox.Show(ex.Message,"3错误提示",0,MessageBoxIcon.Error);}
}
// return null;
}
异常信息:
传递到icon的win32句柄无效或者类型错误。
按一个同仁的相同的异常开帖问题解决方法:
protected virtual Icon myExtractIcon(string FileName,int iIndex)
{
try
{
IntPtr hIcon=(IntPtr)ExtractIcon(this.Handle,FileName,iIndex);
if(! hIcon.Equals(null))
{
Icon icon=Icon.FromHandle(hIcon);
return icon;
}
}
catch(Exception ex)
{ MessageBox.Show(ex.Message,"1错误提示",0,MessageBoxIcon.Error);}
return null;
}
用if(hIcon != System.IntPtr.Zero) 替换 if(! hIcon.Equals(null)) 无作用。
2007年4月4日16时,忙了一天总算是把异常给处理掉了。辛苦,迷茫,以及解决问题后的兴奋,这或许就是程序员可生活吧,也是我们的追求与职业快乐吧。好了。闲话少说了,我最近越来越唠叨了。
在项目中添加如下代码:
//we need this function to release the unmanaged resource,
//the unmanaged resource will be
//copies to a managed one and it will be returned.
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public extern static bool DestroyIcon(IntPtr handle);
/// <summary>
/// 设置图标,把得到的图标放入一个ImageList
/// </summary>
/// <param name="imageList">ImageList,可以new一个</param>
/// <param name="FileName">文件名</param>
/// <param name="tf"></param>
protected virtual void SetIcon(ImageList imageList,string FileName,bool tf)
{
……
Icon ic=Icon.FromHandle(fi.hIcon);
imageList.Images.Add(ic); //添加到ImageList,可放图标和图片,只做一个转换用
DestroyIcon(fi.hIcon); // Cleanup
else
{
……
Icon ic=Icon.FromHandle(fi.hIcon);
imageList.Images.Add(ic);
DestroyIcon(fi.hIcon); // Cleanup
}
}
……
}
因使用ExtractIcon(),You must destroy the icon handle returned by ExtractIcon by calling the DestroyIcon function.
故添加了一个DestroyIcon 函数,它以前一般用于“销毁一个图标,释放这个图标占用的内存”。我觉得在一个图标被获得以后就立即这样做(销毁图标)真是个好主意(因为这个类将以各种各样不同地方式使用)。一旦需要GC就清理掉这个图标,或者被存于一个ImageList。
参考资料:
ExtractIcon
http://msdn2.microsoft.com/en-us/library/ms648068.aspx
BOOL DestroyIcon(HICON hIcon);
见http://msdn2.microsoft.com/en-us/library/ms648063.aspx
http://www.codeproject.com/上的
Obtaining (and managing) file and folder icons using SHGetFileInfo in C#
相关文章推荐
- 对于strtok底层的实现(个人观念)
- 使用hive、java api两种方式实现wordcount功能、及个人感悟
- 使用hive、java api两种方式实现wordcount功能、及个人感悟
- 个人对于TDD的些微看法
- 对于ios7扫描二维码功能的实现
- 用flask开发个人博客(37)—— 使用Flask-pagedown实现博客文章预览的功能
- 对于罗永浩个人的看法
- 读《不要在功能上竞争》的个人看法
- Eclipse RCP中实现资源管理器的缩略图、平铺功能
- 对于求职的一些个人看法及总结
- 使用Gallery控件实现个人相簿功能
- 个人开发者帐号+wireless install 实现非app store程序的在线更新功能
- 用公共头文件来实现个人提炼的功能模块的集中管理
- 第三页(客户端) :远程资源管理器 c#应用源代码,SERVICE + CLIENT 模式 可实现远程文件管理,下载功能
- 继承 System.Windows.Forms.ComboBox 实现支持自动完成功能的 ComboBox!个人感觉还可以!!!
- 个人对于学习一门新的语言的看法
- 对于BOT机器人的个人看法
- 对于a标签点击之后可以发邮件和打电话的功能实现
- 做了一个个人网站,想实现音乐播放器功能,而且想用火狐浏览器使用,求方法!
- 个人对于腾讯和优酷的看法