vb中如何获得快捷方式图标并在按钮上显示
用vb做成一个类似于windows快速启动栏的东西(要求源代码,如果只是思路就不必了,思路我大概知道)要求获取“c:\link\”下的所有快捷方式,并将其快捷方式图标在按...
用vb做成一个类似于windows快速启动栏的东西(要求源代码,如果只是思路就不必了,思路我大概知道)
要求获取“c:\link\”下的所有快捷方式,并将其快捷方式图标在按钮(或picturebox)中显示
自己做了一部分,但是不知道,vb中如何获取快捷方式图标并显示
谢……
获取图标部分最好有解释
不好意思,我问的是从快捷方式获取(怎样获得快捷方式的指向,及各种信息),文件图标的获取我也会而且还有更简单的方法,或许谁能告诉我怎样获取快捷方式指向,及快捷方式中包含的各种信息 展开
要求获取“c:\link\”下的所有快捷方式,并将其快捷方式图标在按钮(或picturebox)中显示
自己做了一部分,但是不知道,vb中如何获取快捷方式图标并显示
谢……
获取图标部分最好有解释
不好意思,我问的是从快捷方式获取(怎样获得快捷方式的指向,及各种信息),文件图标的获取我也会而且还有更简单的方法,或许谁能告诉我怎样获取快捷方式指向,及快捷方式中包含的各种信息 展开
3个回答
展开全部
先给你说说怎么获得文件图标的代码吧
'模块中的代码
Option Explicit
Public Const SHGFI_DISPLAYNAME = &H200
Public Const SHGFI_EXETYPE = &H2000
Public Const SHGFI_LARGEICON = &H0
Public Const SHGFI_SHELLICONSIZE = &H4
Public Const SHGFI_SMALLICON = &H1
Public Const SHGFI_SYSICONINDEX = &H4000
Public Const SHGFI_TYPENAME = &H400
Public Const BASIC_SHGFI_FLAGS = SHGFI_TYPENAME Or SHGFI_SHELLICONSIZE Or SHGFI_SYSICONINDEX Or SHGFI_DISPLAYNAME Or SHGFI_EXETYPE
Public Const MAX_PATH = 260
Public Const ILD_TRANSPARENT = &H1
Public Type SHFILEINFO
hIcon As Long
iIcon As Long
dwAttributes As Long
szDisplayName As String * MAX_PATH
szTypeName As String * 80
End Type
Public Declare Function SHGetFileInfo Lib _
"shell32.dll" Alias "SHGetFileInfoA" _
(ByVal pszPath As String, _
ByVal dwFileAttributes As Long, _
psfi As SHFILEINFO, _
ByVal cbSizeFileInfo As Long, _
ByVal uFlags As Long) As Long
Public Declare Function ImageList_Draw Lib "comctl32.dll" _
(ByVal himl As Long, ByVal i As Long, _
ByVal hDCDest As Long, ByVal x As Long, _
ByVal y As Long, ByVal flags As Long) As Long
Public shinfo As SHFILEINFO
Public Const SHGFI_USEFILEATTRIBUTES = &H10
Public Const SHGFI_ICON = &H100
'===================================================
'新建一个窗体,在窗体上添加一个TextBox用来输入文件路径
'和两个picturebox用来显示提取到的图标
'以下是窗体中的代码
Private Sub Text1_Change() '要显示的图标路径
Dim hImgSmall As Long
Dim fName As String '驱动器号、文件夹名、文件名
Dim r As Long
Dim hImgLarge As Long
Dim Info1 As String, Info2 As String
fName = Text1.Text
hImgSmall& = SHGetFileInfo(fName$, 0&, shinfo, Len(shinfo), SHGFI_ICON Or SHGFI_SMALLICON Or SHGFI_SYSICONINDEX Or SHGFI_USEFILEATTRIBUTES)
hImgLarge& = SHGetFileInfo(fName$, 0&, shinfo, Len(shinfo), SHGFI_ICON Or BASIC_SHGFI_FLAGS Or SHGFI_SYSICONINDEX Or SHGFI_USEFILEATTRIBUTES)
Info1 = Left$(shinfo.szDisplayName, InStr(shinfo.szDisplayName, Chr$(0)) - 1)
Info2 = Left$(shinfo.szTypeName, InStr(shinfo.szTypeName, Chr$(0)) - 1)
Debug.Print Info1; Info2
Picture1.Picture = LoadPicture()
Picture1.AutoRedraw = True
Picture2.Picture = LoadPicture()
Picture2.AutoRedraw = True
r = ImageList_Draw(hImgSmall&, shinfo.iIcon, Picture1.hDC, 0, 0, ILD_TRANSPARENT)
r = ImageList_Draw(hImgLarge&, shinfo.iIcon, Picture2.hDC, 3, 3, ILD_TRANSPARENT)
Set Picture1.Picture = Picture1.Image
Set Picture2.Picture = Picture2.Image
End Sub
有了上面的代码,你在Text1中输入一个文件路径就可以看到图标了,但我还要给你说的是,你直接输快捷方式的路径是不对的,你要先获得快捷方式所指向的文件路径,然后显示这个文件路径的图标才是正确的,挺简单的。
还有什么不懂的,你可以来找我!
晕~
获得快捷方式的信息更简单,看下面:
Option Explicit
'注意要引用:Microsoft Shell Controls And Automation
Private Sub Command1_Click()
Dim FolderPath As String
Dim ShortcutName As String
Dim WorkDir As String
Dim Arguments As String, Description As String
Dim IconIdx As Long, ShowCommand As Long
FolderPath = "C:\Documents and Settings\Administrator\桌面\" '快捷方式所在的目录
ShortcutName = "ToolBox.exe.lnk" '快捷方式的文件名,注意要加lnk
Dim IconFile As String
Call GetShellLinkInfo(FolderPath, ShortcutName, WorkDir, Arguments, Description, IconFile, IconIdx, ShowCommand)
End Sub
Private Sub GetShellLinkInfo(ByVal FolderPath As String, ByVal ShortcutName As String, WorkDir As String, _
Arguments As String, Description As String, IconFile As String, IconIdx As Long, _
ShowCommand As Long)
Dim mShell As Shell, mFile As FolderItem, mFolder As Folder
Dim lnk As ShellLinkObject, i As Long
Set mShell = New Shell
Set mFolder = mShell.NameSpace(FolderPath)
On Error Resume Next
Set mFile = mFolder.Items.Item(ShortcutName)
If Err Then
MsgBox ShortcutName & " is inaccessable!"
Err.Clear
GoTo exit_sub
Else
If mFile.IsLink Then
Set lnk = mFile.GetLink
WorkDir = lnk.WorkingDirectory
Arguments = lnk.Arguments
Description = lnk.Description
IconIdx = lnk.GetIconLocation(IconFile)
ShowCommand = lnk.ShowCommand
MsgBox "Name: " & mFile.Name & vbCrLf & _
"Description: " & lnk.Description & vbCrLf & _
"Path: " & lnk.Path & vbCrLf & _
"WorkingDirectory: " & lnk.WorkingDirectory & vbCrLf, vbInformation
Else
MsgBox ShortcutName & " is not a shortcut!", vbInformation
End If
End If
exit_sub:
Set lnk = Nothing
Set mFile = Nothing
Set mFolder = Nothing
Set mShell = Nothing
End Sub
'模块中的代码
Option Explicit
Public Const SHGFI_DISPLAYNAME = &H200
Public Const SHGFI_EXETYPE = &H2000
Public Const SHGFI_LARGEICON = &H0
Public Const SHGFI_SHELLICONSIZE = &H4
Public Const SHGFI_SMALLICON = &H1
Public Const SHGFI_SYSICONINDEX = &H4000
Public Const SHGFI_TYPENAME = &H400
Public Const BASIC_SHGFI_FLAGS = SHGFI_TYPENAME Or SHGFI_SHELLICONSIZE Or SHGFI_SYSICONINDEX Or SHGFI_DISPLAYNAME Or SHGFI_EXETYPE
Public Const MAX_PATH = 260
Public Const ILD_TRANSPARENT = &H1
Public Type SHFILEINFO
hIcon As Long
iIcon As Long
dwAttributes As Long
szDisplayName As String * MAX_PATH
szTypeName As String * 80
End Type
Public Declare Function SHGetFileInfo Lib _
"shell32.dll" Alias "SHGetFileInfoA" _
(ByVal pszPath As String, _
ByVal dwFileAttributes As Long, _
psfi As SHFILEINFO, _
ByVal cbSizeFileInfo As Long, _
ByVal uFlags As Long) As Long
Public Declare Function ImageList_Draw Lib "comctl32.dll" _
(ByVal himl As Long, ByVal i As Long, _
ByVal hDCDest As Long, ByVal x As Long, _
ByVal y As Long, ByVal flags As Long) As Long
Public shinfo As SHFILEINFO
Public Const SHGFI_USEFILEATTRIBUTES = &H10
Public Const SHGFI_ICON = &H100
'===================================================
'新建一个窗体,在窗体上添加一个TextBox用来输入文件路径
'和两个picturebox用来显示提取到的图标
'以下是窗体中的代码
Private Sub Text1_Change() '要显示的图标路径
Dim hImgSmall As Long
Dim fName As String '驱动器号、文件夹名、文件名
Dim r As Long
Dim hImgLarge As Long
Dim Info1 As String, Info2 As String
fName = Text1.Text
hImgSmall& = SHGetFileInfo(fName$, 0&, shinfo, Len(shinfo), SHGFI_ICON Or SHGFI_SMALLICON Or SHGFI_SYSICONINDEX Or SHGFI_USEFILEATTRIBUTES)
hImgLarge& = SHGetFileInfo(fName$, 0&, shinfo, Len(shinfo), SHGFI_ICON Or BASIC_SHGFI_FLAGS Or SHGFI_SYSICONINDEX Or SHGFI_USEFILEATTRIBUTES)
Info1 = Left$(shinfo.szDisplayName, InStr(shinfo.szDisplayName, Chr$(0)) - 1)
Info2 = Left$(shinfo.szTypeName, InStr(shinfo.szTypeName, Chr$(0)) - 1)
Debug.Print Info1; Info2
Picture1.Picture = LoadPicture()
Picture1.AutoRedraw = True
Picture2.Picture = LoadPicture()
Picture2.AutoRedraw = True
r = ImageList_Draw(hImgSmall&, shinfo.iIcon, Picture1.hDC, 0, 0, ILD_TRANSPARENT)
r = ImageList_Draw(hImgLarge&, shinfo.iIcon, Picture2.hDC, 3, 3, ILD_TRANSPARENT)
Set Picture1.Picture = Picture1.Image
Set Picture2.Picture = Picture2.Image
End Sub
有了上面的代码,你在Text1中输入一个文件路径就可以看到图标了,但我还要给你说的是,你直接输快捷方式的路径是不对的,你要先获得快捷方式所指向的文件路径,然后显示这个文件路径的图标才是正确的,挺简单的。
还有什么不懂的,你可以来找我!
晕~
获得快捷方式的信息更简单,看下面:
Option Explicit
'注意要引用:Microsoft Shell Controls And Automation
Private Sub Command1_Click()
Dim FolderPath As String
Dim ShortcutName As String
Dim WorkDir As String
Dim Arguments As String, Description As String
Dim IconIdx As Long, ShowCommand As Long
FolderPath = "C:\Documents and Settings\Administrator\桌面\" '快捷方式所在的目录
ShortcutName = "ToolBox.exe.lnk" '快捷方式的文件名,注意要加lnk
Dim IconFile As String
Call GetShellLinkInfo(FolderPath, ShortcutName, WorkDir, Arguments, Description, IconFile, IconIdx, ShowCommand)
End Sub
Private Sub GetShellLinkInfo(ByVal FolderPath As String, ByVal ShortcutName As String, WorkDir As String, _
Arguments As String, Description As String, IconFile As String, IconIdx As Long, _
ShowCommand As Long)
Dim mShell As Shell, mFile As FolderItem, mFolder As Folder
Dim lnk As ShellLinkObject, i As Long
Set mShell = New Shell
Set mFolder = mShell.NameSpace(FolderPath)
On Error Resume Next
Set mFile = mFolder.Items.Item(ShortcutName)
If Err Then
MsgBox ShortcutName & " is inaccessable!"
Err.Clear
GoTo exit_sub
Else
If mFile.IsLink Then
Set lnk = mFile.GetLink
WorkDir = lnk.WorkingDirectory
Arguments = lnk.Arguments
Description = lnk.Description
IconIdx = lnk.GetIconLocation(IconFile)
ShowCommand = lnk.ShowCommand
MsgBox "Name: " & mFile.Name & vbCrLf & _
"Description: " & lnk.Description & vbCrLf & _
"Path: " & lnk.Path & vbCrLf & _
"WorkingDirectory: " & lnk.WorkingDirectory & vbCrLf, vbInformation
Else
MsgBox ShortcutName & " is not a shortcut!", vbInformation
End If
End If
exit_sub:
Set lnk = Nothing
Set mFile = Nothing
Set mFolder = Nothing
Set mShell = Nothing
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询