您的位置:首页 > 其它

个人对于实现资源管理器功能的看法

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#
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: