DestoryIcon函式介紹
Destroys an icon and frees any memory the icon occupied.
銷毀一個圖示並且釋放該圖示所占用的記憶體;
BOOL WINAPI DestroyIcon( __in HICON hIcon);
在C#.Net中的使用方法為 :
[DllImport("user32.dll", EntryPoint = "DestroyIcon")]
private static extern bool DestroyIcon(IntPtr handle);
hIcon [in] 圖示的句柄
Type: HICON
A handle to the icon to be destroyed. The icon must not be in use.
將要被銷毀的圖示的句柄,並且該圖示不能正在被使用
Type: BOOL布爾值
If the function succeeds, the return value is nonzero.如果銷毀圖示成功則返回值為true;
If the function fails, the return value is zero. To get extended error information, call GetLastError.
如果銷毀圖示失敗返回值為false;
It is only necessary to call DestroyIcon for icons and cursors created with the following functions: CreateIconFromResourceEx (if called without the LR_SHARED flag), CreateIconIndirect, and CopyIcon. Do not use this function to destroy a shared icon. A shared icon is valid as long as the module from which it was loaded remains in memory. The following functions obtain a shared icon.
LoadIconLoadImage (if you use the LR_SHARED flag) CopyImage (if you use the LR_COPYRETURNORG flag and the hImage parameter is a shared icon) CreateIconFromResourceCreateIconFromResourceEx (if you use the LR_SHARED flag)
//調用SHGetFileInfo獲取檔案信息
[DllImport("Shell32.dll")]
private static extern int SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags);
//包含檔案信息的結構
public struct SHFILEINFO
{
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
public char szDisplayName;
public char szTypeName;
}
[DllImport("user32.dll", EntryPoint = "DestroyIcon")]
private static extern bool DestroyIcon(IntPtr handle);
///
/// 獲取檔案的圖示
///
///
///
///
public void SetIcon(ImageList imagelist, string filename, bool tf)
{
SHFILEINFO sfi = new SHFILEINFO();
int ntotal = 0;
if (tf)
{
//SHGFI_ICON|SHGFI_SMALLICON
ntotal = (int)SHGetFileInfo(filename, 0, ref sfi, 100, 16640);
try
{
if (ntotal > 0)
{
Icon ico = Icon.FromHandle(sfi.hIcon);
imagelist.Images.Add(ico);
DestroyIcon(sfi.hIcon);
}
}
catch (Exception e)
{ MessageBox.Show(e.ToString(), "錯誤提示:", 0, MessageBoxIcon.Error); }
}
else
{
ntotal = (int)SHGetFileInfo(filename, 0, ref sfi, 100, 257);
try
{
if (ntotal > 0)
{
Icon ico = Icon.FromHandle(sfi.hIcon);
imagelist.Images.Add(ico);
DestroyIcon(sfi.hIcon);
}
}
catch (Exception e)
{ MessageBox.Show(e .ToString (),"錯誤提示:",0,MessageBoxIcon.Error ); }
}
}