需要调用api完成
声明:
VB.NET版本
Private Declare Auto Function SHGetFileInfo _
Lib "shell32" (ByVal pszPath As String, ByVal dwFileAttributes As Integer, ByRef psfi As SHFILEINFO, _
ByVal cbFileInfo As Integer, ByVal uFlags As Integer) As Integer
C#中建立了一个类(原方法来自网友)
GetAppIconApi
1
using System;
2
using System.Reflection;
3
using System.Runtime.InteropServices;
4
namespace GetAppIcon
5
{
6
public class GetAppIconApi
7
{
8
public struct SHFileInfo
9
{
10
public IntPtr hIcon;
11
public int iIcon;
12
public uint dwAttribs;
13
[MarshalAs(UnmanagedType.LPStr, SizeConst=260)]
14
public string pszDisplayName;
15
[MarshalAs(UnmanagedType.LPStr, SizeConst=80)]
16
public string pszTypeName;
17
};
18
19
public enum SHGFI{SmallIcon = 0x00000001,LargeIcon = 0x00000000,ICON=0x000000100,DISPLAYNAME=0x000000200,TYPENAME=0x000000400,SysIconIndex=0x00004000,UseFileAttributes = 0x00000010}
20
21
[DllImport("shell32")]
22
public static extern IntPtr SHGetFileInfo(string pszPath,uint dwAttribs,out SHFileInfo lpfi,int cb,SHGFI flags);
23
24
}
25
26
27
28
}
29
调用API函数,并生成ico文件
using System;2
using System.Reflection;3
using System.Runtime.InteropServices;4
namespace GetAppIcon5
{6
public class GetAppIconApi7
{8
public struct SHFileInfo9
{10
public IntPtr hIcon;11
public int iIcon;12
public uint dwAttribs;13
[MarshalAs(UnmanagedType.LPStr, SizeConst=260)]14
public string pszDisplayName;15
[MarshalAs(UnmanagedType.LPStr, SizeConst=80)]16
public string pszTypeName;17
};18

19
public enum SHGFI{SmallIcon = 0x00000001,LargeIcon = 0x00000000,ICON=0x000000100,DISPLAYNAME=0x000000200,TYPENAME=0x000000400,SysIconIndex=0x00004000,UseFileAttributes = 0x00000010}20

21
[DllImport("shell32")]22
public static extern IntPtr SHGetFileInfo(string pszPath,uint dwAttribs,out SHFileInfo lpfi,int cb,SHGFI flags);23

24
}25
26

27
28
}29

1
private void button1_Click(object sender, System.EventArgs e)
2
{
3
//通过下面得到ICON
4
string filename=this.textBox1.Text.Trim();
5
string strPath="";
6
string strNewFile="";
7
GetAppIcon.GetAppIconApi.SHFileInfo fileiconinfo=new GetAppIcon.GetAppIconApi.SHFileInfo();
8
GetAppIcon.GetAppIconApi.SHGetFileInfo(filename,0,out fileiconinfo,Marshal.SizeOf(fileiconinfo),GetAppIcon.GetAppIconApi.SHGFI.ICON|GetAppIcon.GetAppIconApi.SHGFI.SmallIcon);
9
try
10
{
11
Icon thefileicon=Icon.FromHandle(fileiconinfo.hIcon);
12
this.Icon=thefileicon;
13
strPath=Application.StartupPath+"\\";
14
strNewFile="ff"+".ico";
15
strNewFile=strPath+strNewFile;
16
System.Drawing.Bitmap oBp;
17
oBp=this.Icon.ToBitmap();
18
oBp.Save(strNewFile,System.Drawing.Imaging.ImageFormat.Icon);
19
//先将图标转换成bitmap文件再将bitMap文件转换成ico文件
20
21
}
22
catch(Exception ex)
23
{
24
MessageBox.Show("该文件图标无效,请重新选择图标");
25
}
26
27
}
加入生成的ICO文件不能直接作为窗体的ICO,会报错"参数Picture必须是可用作ICO的图片,我研究过,问题应该是
private void button1_Click(object sender, System.EventArgs e)2
{3
//通过下面得到ICON4
string filename=this.textBox1.Text.Trim();5
string strPath="";6
string strNewFile="";7
GetAppIcon.GetAppIconApi.SHFileInfo fileiconinfo=new GetAppIcon.GetAppIconApi.SHFileInfo();8
GetAppIcon.GetAppIconApi.SHGetFileInfo(filename,0,out fileiconinfo,Marshal.SizeOf(fileiconinfo),GetAppIcon.GetAppIconApi.SHGFI.ICON|GetAppIcon.GetAppIconApi.SHGFI.SmallIcon);9
try10
{11
Icon thefileicon=Icon.FromHandle(fileiconinfo.hIcon);12
this.Icon=thefileicon;13
strPath=Application.StartupPath+"\\";14
strNewFile="ff"+".ico";15
strNewFile=strPath+strNewFile;16
System.Drawing.Bitmap oBp;17
oBp=this.Icon.ToBitmap();18
oBp.Save(strNewFile,System.Drawing.Imaging.ImageFormat.Icon);19
//先将图标转换成bitmap文件再将bitMap文件转换成ico文件20

21
}22
catch(Exception ex)23
{24
MessageBox.Show("该文件图标无效,请重新选择图标");25
}26
27
}oBp=this.Icon.ToBitmap();
oBp.Save(strNewFile,System.Drawing.Imaging.ImageFormat.Icon);
在保存时应该根据ico文件的格式进行设置,后来一直没有成功
如果想将其他窗体的图标换成刚才生成的图标,可以如下方法
1
2
Image oImage;
3
System.Drawing.Bitmap bp;
4
bp=new Bitmap(@"c:\ff.ico");
5
try
6
{
7
this.Icon=Icon.FromHandle(bp.GetHicon());
8
}
9
catch(Exception ex)
10
{
11
MessageBox.Show(ex.Message);
12
}
这是窗体中的ico图标改变
2
Image oImage;3
System.Drawing.Bitmap bp;4
bp=new Bitmap(@"c:\ff.ico");5
try6
{ 7
this.Icon=Icon.FromHandle(bp.GetHicon());8
}9
catch(Exception ex)10
{11
MessageBox.Show(ex.Message);12
}


浙公网安备 33010602011771号