求助:如何获得任意文件扩展名关联的图标

江南雨田 2012-11-26 02:29:05
本人想根据任意文件扩展名得到关联的图标,比如:bmp、pdf、xls、doc、txt等等,网上也搜了相关内容,有得到“EXE、dll”文件的图标,也有用非VFP代码获得图标的程序,就是没有找到VFP的。
以下是excel论坛有人用VBA编的可以实现,有哪位高人能将他转换为VFP的代码,要么谁有其他现成的相关VFP代码分享一下,谢谢!

http://www.officefans.net/cdb/viewthread.php?tid=77504


*----------------------------------
Option Explicit

Private Const MAX_PATH = 260
Private Type BITMAPINFOHEADER '40 bytes
biSize As Long
biWidth As Long
biHeight As Long
biPlanes As Integer
biBitCount As Integer
biCompression As Long
biSizeImage As Long
biXPelsPerMeter As Long
biYPelsPerMeter As Long
biClrUsed As Long
biClrImportant As Long
End Type

Private Type RGBQUAD
rgbBlue As Byte
rgbGreen As Byte
rgbRed As Byte
rgbReserved As Byte
End Type

Private Type BITMAPINFO
bmiHeader As BITMAPINFOHEADER
bmiColors As RGBQUAD
End Type


Private Type SHFILEINFO
hIcon As Long ' out: icon
iIcon As Long ' out: icon index
dwAttributes As Long ' out: SFGAO_ flags
szDisplayName As String * 260 ' out: display name (or path)
szTypeName As String * 80 ' out: type name
End Type


Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function CreateDIBSection Lib "gdi32" (ByVal hdc As Long, pBitmapInfo As BITMAPINFO, ByVal un As Long, ByVal lplpVoid As Long, ByVal handle As Long, ByVal dw As Long) As Long
Private Declare Function GetDIBits Lib "gdi32" (ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
'Private Declare Function GetDC Lib "user32" (ByVal Hwnd As Long) As Long
Private Declare Function DrawIcon Lib "user32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal hIcon As Long) As Long
Private Declare Function SHGetFileInfo Lib "shell32.dll" Alias "SHGetFileInfoA" (ByVal pszPath As String, ByVal dwFileAttributes As Long, psfi As SHFILEINFO, ByVal cbFileInfo As Long, ByVal uFlags As Long) As Long
Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, lpInitData As Long) As Long
Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
Private Declare Function FillRect Lib "user32" (ByVal hdc As Long, lpRect As RECT, ByVal hBrush As Long) As Long
Private Declare Function DrawIconEx Lib "user32" (ByVal hdc As Long, ByVal xLeft As Long, ByVal yTop As Long, ByVal hIcon As Long, ByVal cxWidth As Long, ByVal cyWidth As Long, ByVal istepIfAniCur As Long, ByVal hbrFlickerFreeDraw As Long, ByVal diFlags As Long) As Long

Private Const vbSrcCopy = &HCC0020
Private Const SHGFI_ICON = &H100
Private Const SHGFI_USEFILEATTRIBUTES = &H10
Private Const FILE_ATTRIBUTE_NORMAL = &H80
Private Const IMAGE_ICON = 1
Private Const FILE_ATTRIBUTE_ARCHIVE = &H20
Private Const FILE_ATTRIBUTE_COMPRESSED = &H800
Private Const FILE_ATTRIBUTE_DIRECTORY = &H10
Private Const FILE_ATTRIBUTE_HIDDEN = &H2
Private Const BI_RGB = 0&
Private Const DIB_RGB_COLORS = 0
Private Const LR_COPYFROMRESOURCE = &H4000
Private Const DI_MASK = &H1 ' 绘图时使用图标的MASK部分(如单独使用, 可获得图标的掩模)
Private Const DI_IMAGE = &H2 ' 绘图时使用图标的XOR部分(即图标没有透明区域)
Private Const DI_NORMAL = DI_MASK Or DI_IMAGE

Enum filetype
normalFile = FILE_ATTRIBUTE_NORMAL
folder = FILE_ATTRIBUTE_DIRECTORY
End Enum

Public Sub extractIcontoBMP(fileName As String, filetype As filetype, targetFile As String, Optional dimension As Long = 32)

Dim iBitmap As Long
Dim DC As Long
Dim iDC As Long
Dim sfi As SHFILEINFO
Dim bi24BitInfo As BITMAPINFO
Dim bBytes() As Byte
Dim hBrush As Long
Dim iconWidth As Integer
Dim iconHeight As Integer

iconWidth = dimension
iconHeight = dimension

With bi24BitInfo.bmiHeader
.biBitCount = 24
.biCompression = BI_RGB
.biPlanes = 1
.biSize = Len(bi24BitInfo.bmiHeader)
.biWidth = iconWidth
.biHeight = iconHeight
End With

'得到指定文件的SHFILEINFO信息,这里主要用的是hicon
SHGetFileInfo fileName, filetype, sfi, Len(sfi), SHGFI_USEFILEATTRIBUTES Or SHGFI_ICON

'在屏幕上创建一个设备场景(DC - Device Context)
DC = CreateDC("display", vbNullString, 0, 0)

'创建一个与特定设备场景(这里是上一句的DC)一致的内存设备场景
iDC = CreateCompatibleDC(DC)

'创建一个DIBSection
iBitmap = CreateDIBSection(iDC, bi24BitInfo, DIB_RGB_COLORS, ByVal 0&, ByVal 0&, ByVal 0&)

'将iBitmap对象选入iDC设备场景
SelectObject iDC, iBitmap

'创建一个白色(vbWhite)的刷子,用于填充背景,否则另存出的图片会是黑色背景,这里可选其他颜色(如vbred,&Hdddddd)达到其他效果
hBrush = CreateSolidBrush(vbWhite)

'将sfi.hicon指向的图标绘入iDC,iconWidth和iconHeight将是最终的bmp图像尺寸
DrawIconEx iDC, 0, 0, sfi.hIcon, iconWidth, iconHeight, ByVal 0, hBrush, DI_NORMAL

'重定义bBytes字节流的长度以容纳整个DIBBits,计算方式很简单:=每个像素3个字节*图像宽度*图像高度
ReDim bBytes(1 To bi24BitInfo.bmiHeader.biWidth * bi24BitInfo.bmiHeader.biHeight * 3) As Byte

'将iDC设备场景内的图片转换成二进制存到bBytes()数组中,在后面保存到文件
GetDIBits iDC, iBitmap, 0, bi24BitInfo.bmiHeader.biHeight, bBytes(1), bi24BitInfo, DIB_RGB_COLORS

'别忘了释放内存
DeleteDC DC
DeleteDC iDC
DeleteObject iBitmap
DeleteObject hBrush

'----这段代码可以把位图变为灰度显示,但实际每像素仍以24位表示,因此文件尺寸不会变小--------------------
'同样如果处理一种或几咱颜色亮度,可以改变图片色调、亮度等
' Dim i As Long
' For i = 1 To UBound(bBytes) Step 3
' bBytes(i) = bBytes(i) * 0.3 + bBytes(i + 1) * 0.59 + bBytes(i + 2) * 0.11
' bBytes(i + 1) = bBytes(i)
' bBytes(i + 2) = bBytes(i)
' Next
'---------------------------------------------------------------------------------------------
'为了简单起见,这里没有验证目标文件名-targetFile的有效性,在实际应用中,如果用户输入不可预料,应该加以判别
Open targetFile For Binary As #1
Put 1, , CByte(66) 'B
Put 1, , CByte(77) 'M
Put 1, , CLng(UBound(bBytes) + LenB(bi24BitInfo.bmiHeader) + 14) '文件大小
Put 1, , CInt(0) '保留字节
Put 1, , CInt(0) '保留字节
Put 1, , CLng(LenB(bi24BitInfo.bmiHeader) + 14) '偏移量
Put 1, , CLng(LenB(bi24BitInfo.bmiHeader)) '本结构所占字节数
Put 1, , CLng(iconWidth) '宽度
Put 1, , CLng(iconHeight) '高度
Put 1, , CInt(1) '目标设备级别,必须为1
Put 1, , CInt(24) '每像素的位数
Put 1, , CLng(0) '位图压缩类型
Put 1, , CLng(UBound(bBytes)) '位图大小
Put 1, , CLng(0) '每米水平像素数
Put 1, , CLng(0) '每米竖直像素数
Put 1, , CLng(0) '位图实际使用的颜色表中的颜色数
Put 1, , CLng(0) '位图显示过程中重要的颜色数
Put 1, , bBytes 'RGB位图数据
Close #1
End Sub
...全文
506 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
江南雨田 2012-11-27
  • 打赏
  • 举报
回复
太棒了,就是我要的效果,谢谢!
十豆三 2012-11-27
  • 打赏
  • 举报
回复

cFilename='.xls' && 文件类型,如.xls .jpg 等等
utype=1 && 图标类型,0为大图标,1为小图标

Declare Integer SHGetFileInfo In shell32 String pszPath,Long dwFileAttributes,String @psfi,Long cbFileInfo,Long uFlags
Declare Integer GetDC In user32 Integer HWnd
Declare Short DrawIcon In user32 Integer hDC,Integer X,Integer Y,Integer hIcon
Declare Short DestroyIcon In user32 Integer hIcon
Declare Integer ReleaseDC In user32 Integer HWnd,Integer hdc
Local cBuffer,nResult,hIcon,lhDC
cBuffer=Replicate(Chr(0),1024)
nResult=SHGetFileInfo(cFilename,utype,@cBuffer,1024,272)
hIcon=CToBin(Substr(cBuffer,1,4),'4rs')
lhDC=GetDC(Thisform.HWnd)
DrawIcon(lhDC,0,0,hIcon)
DestroyIcon(hIcon)
ReleaseDC(Thisform.HWnd,lhDC)
江南雨田 2012-11-26
  • 打赏
  • 举报
回复
谢谢十三豆老师的回复。 这些我看过了,我需要提取的不只局限于可执行文件中图标以及exe/dll/cpl/scr文件图标。 我这里需要的是任意扩展名文件在本地计算机关联的图标,想实现这样的效果:有一个局域网共享文件夹方式的网络版软件,我编写了一个登录软件的用户相互之间可以互发邮件的程序,其中在打开邮件附件的界面,我想根据文件的扩展名自动显示此扩展名关联的图标。常用的软件我可以找到图标文件,但用户发的附件可能各种扩展名都有,因此需要一个只要有扩展名就能得到对应图标的程序。
十豆三 2012-11-26
  • 打赏
  • 举报
回复
很早就有了 VFP 的方法: <WIN API-VFP获取Win32格式可执行文件(exe/dll/cpl/scr)及ico文件中的图标> http://blog.csdn.net/apple_8180/article/details/5831778 <提取可执行文件中图标> http://blog.csdn.net/dkfdtf/article/details/5678990

2,723

社区成员

发帖
与我相关
我的任务
社区描述
VFP,是Microsoft公司推出的数据库开发软件,用它来开发数据库,既简单又方便。
社区管理员
  • VFP社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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