c#中如何获得文件的图标,我想做一个拖拽文件得到图标的程序,,大侠们给点资料
http://blog.csdn.net/xuzhiqiang1010/archive/2009/11/23/4859264.aspx找到一个资料,谢谢楼下的那位...
http://blog.csdn.net/xuzhiqiang1010/archive/2009/11/23/4859264.aspx
找到一个资料,谢谢楼下的那位 展开
找到一个资料,谢谢楼下的那位 展开
展开全部
自定义一个方法:
public static System.Drawing.Icon GetFileIcon(string name, IconSize size,bool linkOverlay)
{
Shell32.SHFILEINFO shfi = new Shell32.SHFILEINFO();
uint flags = Shell32.SHGFI_ICON | Shell32.SHGFI_USEFILEATTRIBUTES;
if (true == linkOverlay) flags += Shell32.SHGFI_LINKOVERLAY;
/* Check the size specified for return. */
if (IconSize.Small == size)
{
flags += Shell32.SHGFI_SMALLICON ;
}
else
{
flags += Shell32.SHGFI_LARGEICON ;
}
Shell32.SHGetFileInfo( name,Shell32.FILE_ATTRIBUTE_NORMAL,ref shfi,(uint) System.Runtime.InteropServices.Marshal.SizeOf(shfi),flags );
// Copy (clone) the returned icon to a new object, thus allowing us to clean-up properly
System.Drawing.Icon icon = (System.Drawing.Icon)System.Drawing.Icon.FromHandle(shfi.hIcon).Clone();
User32.DestroyIcon( shfi.hIcon ); // Cleanup
return icon;
}
为按钮添加一个点击事件:
private void cmdFileSmall_Click(object sender, System.EventArgs e)
{
OpenFileDialog dlgOpenFile = new OpenFileDialog();
if(dlgOpenFile.ShowDialog() == DialogResult.OK)
{
this.Icon=GetFileIcon(dlgOpenFile.FileName, IconSize.Small, false);
}
}
public static System.Drawing.Icon GetFileIcon(string name, IconSize size,bool linkOverlay)
{
Shell32.SHFILEINFO shfi = new Shell32.SHFILEINFO();
uint flags = Shell32.SHGFI_ICON | Shell32.SHGFI_USEFILEATTRIBUTES;
if (true == linkOverlay) flags += Shell32.SHGFI_LINKOVERLAY;
/* Check the size specified for return. */
if (IconSize.Small == size)
{
flags += Shell32.SHGFI_SMALLICON ;
}
else
{
flags += Shell32.SHGFI_LARGEICON ;
}
Shell32.SHGetFileInfo( name,Shell32.FILE_ATTRIBUTE_NORMAL,ref shfi,(uint) System.Runtime.InteropServices.Marshal.SizeOf(shfi),flags );
// Copy (clone) the returned icon to a new object, thus allowing us to clean-up properly
System.Drawing.Icon icon = (System.Drawing.Icon)System.Drawing.Icon.FromHandle(shfi.hIcon).Clone();
User32.DestroyIcon( shfi.hIcon ); // Cleanup
return icon;
}
为按钮添加一个点击事件:
private void cmdFileSmall_Click(object sender, System.EventArgs e)
{
OpenFileDialog dlgOpenFile = new OpenFileDialog();
if(dlgOpenFile.ShowDialog() == DialogResult.OK)
{
this.Icon=GetFileIcon(dlgOpenFile.FileName, IconSize.Small, false);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询