win32 - IFolderView2::GetCurrentFolderFlags的使用


转载:https://www.cnblogs.com/strive-sun/p/14068047.html

下面的例子是将桌面的图标隐藏起来,使用了FWF_NOICONS样式。

#include      // Shell API
#include   // CComPtr & Co.
#include <string> 
#include  
#include 
#include 
#define SUCCEEDED(hr) (((HRESULT)(hr)) >= 0)

// Throw a std::system_error if the HRESULT indicates failure.
template< typename T >
void ThrowIfFailed(HRESULT hr, T&& msg)
{
    if (FAILED(hr))
        throw std::system_error{ hr, std::system_category(), std::forward(msg) };
}

// RAII wrapper to initialize/uninitialize COM
struct CComInit
{
    CComInit() { ThrowIfFailed(::CoInitialize(nullptr), "CoInitialize failed"); }
    ~CComInit() { ::CoUninitialize(); }
    CComInit(CComInit const&) = delete;
    CComInit& operator=(CComInit const&) = delete;
};

// Query an interface from the desktop shell view.
void FindDesktopFolderView(REFIID riid, void** ppv, std::string const& interfaceName)
{
    CComPtr spShellWindows;
    ThrowIfFailed(
        spShellWindows.CoCreateInstance(CLSID_ShellWindows),
        "Failed to create IShellWindows instance");

    CComVariant vtLoc(CSIDL_DESKTOP);
    CComVariant vtEmpty;
    long lhwnd;
    CComPtr spdisp;
    ThrowIfFailed(
        spShellWindows->FindWindowSW(
            &vtLoc, &vtEmpty, SWC_DESKTOP, &lhwnd, SWFO_NEEDDISPATCH, &spdisp),
        "Failed to find desktop window");

    CComQIPtr spProv(spdisp);
    if (!spProv)
        ThrowIfFailed(E_NOINTERFACE, "Failed to get IServiceProvider interface for desktop");

    CComPtr spBrowser;
    ThrowIfFailed(
        spProv->QueryService(SID_STopLevelBrowser, IID_PPV_ARGS(&spBrowser)),
        "Failed to get IShellBrowser for desktop");

    CComPtr spView;
    ThrowIfFailed(
        spBrowser->QueryActiveShellView(&spView),
        "Failed to query IShellView for desktop");
  
    ThrowIfFailed(
        spView->QueryInterface(riid, ppv),
        "Could not query desktop IShellView for interface " + interfaceName);
}


void ToggleDesktopIcons()
{
    CComPtr spView;
    FindDesktopFolderView(IID_PPV_ARGS(&spView), "IFolderView2");

    DWORD flags = 0;
    ThrowIfFailed(
        spView->GetCurrentFolderFlags(&flags),
        "GetCurrentFolderFlags failed");
    ThrowIfFailed(
        spView->SetCurrentFolderFlags(FWF_NOICONS, flags ^FWF_NOICONS),
        "SetCurrentFolderFlags failed");

  
}



int wmain(int argc, wchar_t** argv)
{
    try
    {
        CComInit init;

        ToggleDesktopIcons();

        std::cout << "Desktop icons have been toggled.\n";
    }
    catch (std::system_error const& e)
    {
        std::cout << "ERROR: " << e.what() << ", error code: " << e.code() << "\n";
        return 1;
    }

    return 0;
}