您的位置:首页 > 编程语言 > C#

C#一种变通的程序与窗口使用同一图标资源的方法

2010-03-14 14:16 483 查看
public MainForm()
{
InitializeComponent();
this.Icon = Win32.GetIcon();
}
public class Win32
{
[DllImport("shell32.dll", EntryPoint = "ExtractIcon")]
public static extern int ExtractIcon(IntPtr hInst, string lpFileName, int nIndex);
[DllImport("shell32.dll", EntryPoint = "SHGetFileInfo")]
public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttribute, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint Flags);
[DllImport("User32.dll", EntryPoint = "DestroyIcon")]
public static extern int DestroyIcon(IntPtr hIcon);
[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 static Icon GetIcon()
{
Icon RetrunIco;
Win32.SHFILEINFO shfi = new Win32.SHFILEINFO();
try
{
FileInfo fi = new FileInfo(Application.ExecutablePath);
//获得图标
Win32.SHGetFileInfo(Application.ExecutablePath,
(uint)0x80,
ref shfi,
(uint)System.Runtime.InteropServices.Marshal.SizeOf(shfi),
(uint)(0x100 | 0x400)); //取得Icon和TypeName
RetrunIco = (Icon)Icon.FromHandle(shfi.hIcon).Clone();
Win32.DestroyIcon(shfi.hIcon);
return RetrunIco;
//销毁图标
}
catch
{
return null;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐