简体   繁体   English

如何获得打印机图标

[英]how to get printer icons

sorry this may look like a duplicate question because a similar question was posted but the answers which were given weren't complete.抱歉,这可能看起来像一个重复的问题,因为发布了一个类似的问题,但给出的答案不完整。 i want to create a list of available printers stating the status of the printer and the icon associated with each printer.我想创建一个可用打印机列表,说明打印机的状态以及与每台打印机关联的图标。 at this point am able to get the list and their associated states, but where am failing is getting the icons.此时我能够获取列表及其相关状态,但是我失败的地方是获取图标。 from the earlier posted question the answer involved using SHGetFileInfo, but when i tried it no icons were returned.从早先发布的问题中,答案涉及使用 SHGetFileInfo,但是当我尝试它时没有返回任何图标。 i suspect there was something i need to do before calling SHGetFileInfo because the answer included a statement which involved combining two PIDLs, now because it wasn't actual code but a comment i couldn't fully understand how to implement it.我怀疑在调用 SHGetFileInfo 之前我需要做一些事情,因为答案包括一个涉及组合两个 PIDL 的语句,现在因为它不是实际代码,而是我无法完全理解如何实现它的注释。 here is a link to the question 这是问题的链接

for (int i=0; i < dwReturned; i++)
{
    HICON hIcon = NULL;

    LPITEMIDLIST pidlPrinters = NULL;
    LPITEMIDLIST pidl = NULL;
    LPENUMIDLIST pEnum = NULL;
    LPSHELLFOLDER pDesktopFolder = NULL;
    IShellFolder* psfPrinters = NULL;
    STRRET strName;
    TCHAR pszDisplayName[MAX_PATH];
    HRESULT hr;
    SHFILEINFO sfi;

    hr = SHGetFolderLocation(m_pParentWnd->m_hWnd, CSIDL_PRINTERS, 0, 0, &pidlPrinters);

    if(hr == S_OK)
    {
        hr = SHGetDesktopFolder(&pDesktopFolder);
    }

    if(hr == S_OK)
    {
        hr = pDesktopFolder->BindToObject(pidlPrinters, 0, IID_IShellFolder, reinterpret_cast<void**> 
             (&psfPrinters));
    }

    if(hr == S_OK)
    {
        hr = psfPrinters->EnumObjects(m_pParentWnd->m_hWnd, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, 
            &pEnum);
    }

    if(hr == S_OK)
    {
        while (pEnum->Next(1, &pidl, 0) == S_OK)
        {
            psfPrinters->GetDisplayNameOf(pidl,SHGDN_NORMAL, &strName);

            StrRetToBuf(&strName, pidl, pszDisplayName, MAX_PATH);

            //pInfo is PRINTER_INFO_2 *

            if (_wcsicmp(pszDisplayName, pInfo[i].pPrinterName) == 0)
            {
                SHGetFileInfo((LPCTSTR)pidl, 0, &sfi, sizeof(SHFILEINFO),  SHGFI_PIDL | SHGFI_ICON | 
                SHGFI_DISPLAYNAME | SHGFI_TYPENAME); 
                hIcon = sfi.hIcon;
            }

            IMalloc* Malloc = NULL;
            SHGetMalloc(&Malloc);
            Malloc->Free(pidl);
            Malloc->Release();
       }

       pDesktopFolder->Release();
       pEnum->Release();

       //Some other code
    }
}

You can use IShellFolder::GetUIObjectOf to extract the icon, the following is an example you can have a try.您可以使用IShellFolder::GetUIObjectOf来提取图标,下面是一个示例,您可以尝试一下。

            psfPrinters->GetDisplayNameOf(pidl, SHGDN_NORMAL, &strName);

            IExtractIcon* eIcon = NULL;
            hr = psfPrinters->GetUIObjectOf(NULL, 1, (LPCITEMIDLIST*)&pidl, IID_IExtractIcon, NULL, reinterpret_cast<void**>(&eIcon));
            if (hr == S_OK)
            {
                WCHAR iconFileLocation[200];
                int index;
                UINT flag = 0;
                hr = eIcon->GetIconLocation(NULL, iconFileLocation, sizeof(iconFileLocation), &index, &flag);
                if (hr == S_OK)
                {
                    UINT size = 100;
                    eIcon->Extract(iconFileLocation, index, &hIcon, NULL, size);
                    if (hr == S_OK)
                    {
                        // Draw printer icon on client area for checking purpose
                        DrawIcon(hdc, xNew, y, hIcon);
                        xNew += size;
                    }
                }
            }

Sample output of above code:上述代码的示例输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM