如何用代码取DLL文件里的图标

vistin 2003-07-11 12:52:29
如何用代码取DLL或EXE文件里的图标,并加到IMAGELIST里。
DLL不止一个图标,要通过*.DLL,1的方式得到指定索引的图标。
...全文
55 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
chinchy 2003-07-12
  • 打赏
  • 举报
回复
http://www.kennyandkarin.com/Kenny/CodeCorner/Tools/IconBrowser/
vistin 2003-07-11
  • 打赏
  • 举报
回复
littlehb(网际浪子) 能把例子发给我吗?
cotyledon@sina.com.
谢谢!
vistin 2003-07-11
  • 打赏
  • 举报
回复
图标所在文件是通过注册表得到的,FRAMEWORK 精简版有怎么用呢?
wudixiaocaoren 2003-07-11
  • 打赏
  • 举报
回复
http://www.uncj.net/bbs/dispbbs.asp?boardID=2&ID=8278
孟子E章 2003-07-11
  • 打赏
  • 举报
回复
http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=75&printer=t

Embedding and Using Resources in C#
孟子E章 2003-07-11
  • 打赏
  • 举报
回复
http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=75&printer=t

Embedding and Using Resources in C#
hater 2003-07-11
  • 打赏
  • 举报
回复
windows有api可以做
Private Declare Function ExtractIconEx Lib "shell32.dll" Alias "ExtractIconExA" (ByVal lpszFile As String, ByVal _
nIconIndex As Long, phiconLarge As Long, phiconSmall As Long, ByVal nIcons As Long) As Long


下面是VB6的代码,你可改写成C#的

使用ExtractIconEX API函数从文件中提取图标,并返回图标句柄,然后利用该句柄,使用DrawIcon函数将图标绘制到目标设备中。最后清除句柄以释放系统资源。

创建新工程后,在工程中添加对Standard OLE Types的引用,然后在工程中添加一个标准模块。将下面的代码粘贴到标准模块中:

Option Explicit

Private Type PicBmp
Size As Long
tType As Long
hBmp As Long
hPal As Long
Reserved As Long
End Type
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End Type
Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PicBmp, RefIID As GUID, _
ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long

Private Declare Function ExtractIconEx Lib "shell32.dll" Alias "ExtractIconExA" (ByVal lpszFile As String, ByVal _
nIconIndex As Long, phiconLarge As Long, phiconSmall As Long, ByVal nIcons As Long) As Long

Private Declare Function DestroyIcon Lib "user32" (ByVal hicon As Long) As Long

Public Function GetIconFromFile(FileName As String, IconIndex As Long, UseLargeIcon As Boolean) As Picture

'参数:
'FileName - 包含有图标的文件 (EXE or DLL)
'IconIndex - 欲提取的圉标的索引,从零开始
'UseLargeIcon-如设置为True,则提取大图标,否则提取小图标
'返回值: 包含标标的Picture对象

Dim hlargeicon As Long
Dim hsmallicon As Long
Dim selhandle As Long

' IPicture requires a reference to "Standard OLE Types."
Dim pic As PicBmp
Dim IPic As IPicture
Dim IID_IDispatch As GUID

If ExtractIconEx(FileName, IconIndex, hlargeicon, hsmallicon, 1) > 0 Then

If UseLargeIcon Then
selhandle = hlargeicon
Else
selhandle = hsmallicon
End If

' Fill in with IDispatch Interface ID.
With IID_IDispatch
.Data1 = &H20400
.Data4(0) = &HC0
.Data4(7) = &H46
End With
' Fill Pic with necessary parts.
With pic
.Size = Len(pic) ' Length of structure.
.tType = vbPicTypeIcon ' Type of Picture (bitmap).
.hBmp = selhandle ' Handle to bitmap.
End With

' Create Picture object.
Call OleCreatePictureIndirect(pic, IID_IDispatch, 1, IPic)

' Return the new Picture object.
Set GetIconFromFile = IPic

DestroyIcon hsmallicon
DestroyIcon hlargeicon

End If
End Function
在窗体中添加一个PictureBox控件和一个命令按钮,把下面的代码加入到命令按钮的Click事件中:

Set Picture1.Picture = GetIconFromFile("c:\windows\moricons.dll", _ 0, True)

按F5运行程序,点击命令按钮后,PictureBox会将文件moricons.dll中的图标索引为零的图标画到PictureBox中。



110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧