Windows 检索当前用户打印机的图标

Windows 检索当前用户打印机的图标,windows,winapi,vb6,printing,icons,Windows,Winapi,Vb6,Printing,Icons,我正在尝试模拟MS Office打印对话框的打印机选择组合框。下拉列表包含左侧带有大打印机图标的打印机名称。在Vista上,传真打印机有一个漂亮的传真图标,标记了共享打印机和默认打印机。最好是能够查看更多的打印机信息,就像浏览器查看控制面板->打印机一样 你知道从哪里开始吗 在SHGetFileInfo方面取得了一定的成功,但非常欢迎您的意见 [os:windows,代码语言:any]您没有说明如何调用SHGetFileInfo,但我想您需要设置SHGFI_PIDL标志并使用完全限定的PIDL(

我正在尝试模拟MS Office打印对话框的打印机选择组合框。下拉列表包含左侧带有大打印机图标的打印机名称。在Vista上,传真打印机有一个漂亮的传真图标,标记了共享打印机和默认打印机。最好是能够查看更多的打印机信息,就像浏览器查看控制面板->打印机一样

你知道从哪里开始吗

SHGetFileInfo
方面取得了一定的成功,但非常欢迎您的意见


[os:windows,代码语言:any]

您没有说明如何调用SHGetFileInfo,但我想您需要设置SHGFI_PIDL标志并使用完全限定的PIDL(可能还有SHGFI_USEFILEATTRIBUTES)


要获得共享/默认覆盖图标,请设置SHGFI_ADDOVERLAYS标志

,这是我最后想到的。您将需要各种OLE接口。我确信这个类型库可以以更好的方式移植到VB6,但无论如何,结果如下:

Option Explicit

Private Const CSIDL_PRINTERS    As Long = &H4
Private Const SHGFI_PIDL        As Long = &H8
Private Const SHGFI_ICON        As Long = &H100
Private Const SHGFI_DISPLAYNAME As Long = &H200
Private Const MAX_PATH          As Long = 260

Private Declare Function SHGetDesktopFolder Lib "shell32" (ppshf As IShellFolder) As Long
Private Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As Long) As Long
Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal pv As Long)
Private Declare Function SHGetFileInfo Lib "shell32" Alias "SHGetFileInfoA" (pszPath As Any, ByVal dwFileAttributes As Long, psfi As SHFILEINFO, ByVal cbFileInfo As Long, ByVal uFlags As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal dwLength As Long)
Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (lpPictDesc As PICTDESC, riid As Any, ByVal fPictureOwnsHandle As Long, ppRet As IPicture) As Long

Private Type SHFILEINFO
    hIcon               As Long
    iIcon               As Long
    dwAttributes        As Long
    szDisplayName       As String * MAX_PATH
    szTypeName          As String * 80
End Type

Private Type PICTDESC
    Size                As Long
    Type                As Long
    hBmpOrIcon          As Long
    hPal                As Long
End Type

Private Sub Command1_Click()
    Dim IID_IShellFolder As IShellFolderEx_TLB.GUID
    Dim IID_IPicture(0 To 3) As Long
    Dim pidlPrinters()  As Byte
    Dim pidlCurrent()   As Byte
    Dim pidlAbsolute()  As Byte
    Dim pDesktopFolder  As IShellFolder
    Dim pPrintersFolder As IShellFolder
    Dim pEnumIds        As IEnumIDList
    Dim lPtr            As Long
    Dim uInfo           As SHFILEINFO
    Dim uPict           As PICTDESC
    Dim sPrinterName    As String
    Dim oPrinterIcon    As StdPicture
    
    '--- init consts
    IID_IShellFolder.Data1 = &H214E6 '--- {000214E6-0000-0000-C000-000000000046}
    IID_IShellFolder.Data4(0) = &HC0
    IID_IShellFolder.Data4(7) = &H46
    IID_IPicture(0) = &H7BF80980 '--- {7BF80980-BF32-101A-8BBB-00AA00300CAB}
    IID_IPicture(1) = &H101ABF32
    IID_IPicture(2) = &HAA00BB8B
    IID_IPicture(3) = &HAB0C3000
    '--- init local vars
    uPict.Size = Len(uPict)
    uPict.Type = vbPicTypeIcon
    Call SHGetDesktopFolder(pDesktopFolder)
    '--- retrieve enumerator of Printers virtual folder
    Call SHGetSpecialFolderLocation(0, CSIDL_PRINTERS, lPtr)
    pidlPrinters = pvToPidl(lPtr)
    Call pDesktopFolder.BindToObject(VarPtr(pidlPrinters(0)), 0, IID_IShellFolder, pPrintersFolder)
    Call pPrintersFolder.EnumObjects(0, SHCONTF_NONFOLDERS, pEnumIds)
    '--- loop printers
    Do While pEnumIds.Next(1, lPtr, 0) = 0 '--- S_OK
        pidlCurrent = pvToPidl(lPtr)
        '--- combine pidls: Printers + Current
        ReDim pidlAbsolute(0 To UBound(pidlPrinters) + UBound(pidlCurrent))
        Call CopyMemory(pidlAbsolute(0), pidlPrinters(0), UBound(pidlPrinters) - 1)
        Call CopyMemory(pidlAbsolute(UBound(pidlPrinters) - 1), pidlCurrent(0), UBound(pidlCurrent) - 1)
        '--- retrieve info
        Call SHGetFileInfo(pidlAbsolute(0), 0, uInfo, Len(uInfo), SHGFI_PIDL Or SHGFI_DISPLAYNAME Or SHGFI_ICON)
        sPrinterName = Left(uInfo.szDisplayName, InStr(uInfo.szDisplayName, Chr$(0)) - 1)
        '--- extract icon
        uPict.hBmpOrIcon = uInfo.hIcon
        Call OleCreatePictureIndirect(uPict, IID_IPicture(0), True, oPrinterIcon)
        '--- show
        Set Picture = oPrinterIcon
        MsgBox sPrinterName
    Loop
End Sub

Private Function pvToPidl(ByVal lPtr As Long) As Byte()
    Dim lTotal      As Long
    Dim nSize       As Integer
    Dim baPidl()    As Byte
    
    Do
        Call CopyMemory(nSize, ByVal (lPtr + lTotal), 2)
        lTotal = lTotal + nSize
    Loop While nSize <> 0
    ReDim baPidl(0 To lTotal + 1)
    Call CopyMemory(baPidl(0), ByVal lPtr, lTotal + 2)
    Call CoTaskMemFree(lPtr)
    pvToPidl = baPidl
End Function
选项显式
专用常量CSIDL_打印机,长度=&H4
私人施工单位SHGFI_PIDL长度=&H8
私人Const SHGFI_图标长度=&H100
Private Const SHGFI_DISPLAYNAME作为Long=&H200
专用常量最大路径长度=260
私有声明函数SHGetDesktopFolder Lib“shell32”(ppshf作为IShellFolder)的长度为
私有声明函数SHGetSpecialFolderLocation Lib“shell32.dll”(ByVal hwndOwner为Long,ByVal nFolder为Long,pidl为Long)为Long
私有声明子CoTaskMemFree Lib“ole32.dll”(ByVal pv As Long)
私有声明函数SHGetFileInfo Lib“shell32”别名“SHGetFileInfoA”(pszPath为任意,ByVal dwFileAttributes为长,psfi为SHFILEINFO,ByVal cbFileInfo为长,ByVal uFlags为长)为长
Private Declare Sub CopyMemory Lib“kernel32”别名“rtlmovemory”(pDest为Any,pSource为Any,ByVal dwLength为Long)
私有声明函数OLEATEPICTUREINDIRECT Lib“olepro32.dll”(lpPictDesc作为PICTDESC,riid作为任意,ByVal fPictureOwnsHandle作为Long,ppRet作为IPacture)作为Long
私有类型SHFILEINFO
希肯只要
只要
将属性设置为长
szDisplayName作为字符串*最大路径
szTypeName作为字符串*80
端型
私有类型PICTDESC
大小与长度相同
尽可能长地打字
HBMPolicon尽可能长
hPal只要
端型
专用子命令1_Click()
将IID_IShellFolderEx_TLB.GUID作为IShellFolderEx_的Dim IID_IShellFolder
尺寸IID_i结构(0到3)与长度相同
Dim pidlPrinters()作为字节
Dim pidlCurrent()作为字节
Dim pidlAbsolute()作为字节
将pDesktopFolder作为IShellFolder进行调整
将文件夹设置为IShellFolder
作为IEnumIDList的暗淡pEnumIds
变暗lPtr为长
将uInfo设置为SHFILEINFO
将uPict变暗为PICTDESC
把我当作绳子
将图标变暗为StdPicture
'---init consts
IID_IShellFolder.Data1=&H214E6'--{000214E6-0000-0000-C000-0000000000 46}
IID_IShellFolder.Data4(0)=&HC0
IID_IShellFolder.Data4(7)=&H46
IID_i结构(0)=&H7BF80980'--{7BF80980-BF32-101A-8BBB-00AA00300CAB}
IID_i结构(1)=&H101ABF32
IID_i结构(2)=&HAA00BB8B
IID_i结构(3)=&HAB0C3000
'---init局部变量
uPict.Size=Len(uPict)
uPict.Type=vbPicTypeIcon
调用SHGetDesktopFolder(pDesktopFolder)
“---检索打印机虚拟文件夹的枚举数
调用SHGetSpecialFolderLocation(0,CSIDL_打印机,lPtr)
pidlPrinters=pvToPidl(lPtr)
调用pDesktopFolder.BindToObject(VarPtr(pidlPrinters(0)),0,IID_IShellFolder,ppr文件夹)
调用文件夹.枚举对象(0,SHCONTF_非文件夹,pEnumIds)
“---循环打印机
在pEnumIds时执行。下一步(1,lPtr,0)=0'--S_确定
pidlCurrent=pvToPidl(lPtr)
“---组合PIDL:打印机+当前
ReDim pidlAbsolute(0至UBound(pidlPrinters)+UBound(pidlCurrent))
调用CopyMemory(pidlAbsolute(0)、pidlPrinters(0)、UBound(pidlPrinters)-1)
调用CopyMemory(pidlAbsolute(UBound(pidlPrinters)-1)、pidlCurrent(0)、UBound(pidlCurrent)-1)
“---检索信息
调用SHGetFileInfo(pidlAbsolute(0)、0、uInfo、Len(uInfo)、SHGFI_PIDL或SHGFI_DISPLAYNAME或SHGFI_图标)
sPrinterName=Left(uInfo.szDisplayName,InStr(uInfo.szDisplayName,Chr$(0))-1)
'---提取图标
uPict.hbmpolicon=uInfo.hIcon
调用OleCreatePictureIndirect(uPict,IID_i结构(0),True,oPrinterIcon)
“---秀
设置图片=图标
MsgBox sPrinterName
环
端接头
私有函数pvToPidl(ByVal lPtr作为Long)作为Byte()
长的
以整数形式显示
Dim baPidl()作为字节
做
调用CopyMemory(nSize,ByVal(lPtr+lTotal),2)
lTotal=lTotal+nSize
循环,同时重新设置0
ReDim baPidl(0到lTotal+1)
调用CopyMemory(baPidl(0)、ByVal lPtr、lTotal+2)
呼叫CoTaskMemFree(lPtr)
pvToPidl=baPidl
端函数

是的,获得PIDL是一种痛苦。尤其是在VB6中