zoukankan      html  css  js  c++  java
  • 一步一步教你如何编写VC#,VB.NET或VC++代码玩转Windows Shell Known Folders

    All-In-One Code Framework (AIO) 最新check in了和Windows Shell Known Folders相关的sample code:

    CSShellKnownFolders

    CppShellKnownFolders

    VBShellKnownFolders

    你可以到http://cfx.codeplex.com/SourceControl/ListDownloadableCommits.aspx 下载 到这些sample。ReadMe.txt中有各个sample详细的说明,如step-by-step的创建过程等。

    Sample 简介:

    The Known Folder system provides a way to interact with certain high-profile folders that are present by default in Microsoft Windows. It also allows those same interactions with folders installed and registered with the Known Folder system by applications. This samples demonstrate those possible interactions in Visual C#, VB.NET, and VC++ as they are provided by the Known Folder APIs.

    A. Enumerate and print all known folders.

    B. Print some built-in known folders like FOLDERID_ProgramFiles in three different ways.

    C. Extend known folders with custom folders.

    部分代码示例:

    C# enumerate and print all known folders:

    Console.WriteLine("\nEnumerate all known folders"); 
    foreach (IKnownFolder kf in KnownFolders.All) 

        Console.WriteLine(
    "{0}: {1}", kf.CanonicalName, kf.Path); 
    }

    VC++ 添加自定义Known Folder:

    /*!
    * Register and create a known folder named "CodeFx KnownFolder" under the 
    * user profile folder: C:\Users\<username>\CodeFxKnownFolder. The known 
    * folder displays the localized name "CodeFx KnownFolder LocalizedName", and 
    * shows a folder icon with the CodeFx logo. 

    * CreateKnownFolder calls RegisterKnownFolder to register a known folder. In 
    * RegisterKnownFolder, first define the known folder through a 
    * KNOWNFOLDER_DEFINITION structure. You can specify the known folder's 
    * canonical name, localized name, tooltip, folder icon, etc. Then register 
    * the known folder through a call to IKnownFolderManager::RegisterFolder. 
    * IKnownFolderManager::RegisterFolder requires that the current process is 
    * run as administrator to succeed. 

    * After the known folder is register, CreateKnownFolder initializes and 
    * creates the folder with SHGetKnownFolderPath with the flags KF_FLAG_CREATE 
    * | KF_FLAG_INIT so that the Shell will write desktop.ini in the folder. This 
    * is how our customizations (i.e. pszIcon, pszTooltip, pszLocalizedName) get 
    * picked up by the Shell. If SHGetKnownFolderPath fails, the function 
    * UnregisterKnownFolder is invoked to undo the registration. 
    */ 
    HRESULT CreateKnownFolder(REFKNOWNFOLDERID kfid) 

        
    // Register the known folder 
        HRESULT hr = RegisterKnownFolder(kfid); 
        
    if (SUCCEEDED(hr)) 
        { 
            
    // Create the known folder with SHGetKnownFolderPath with the flags 
            
    // KF_FLAG_CREATE | KF_FLAG_INIT so that the Shell will write 
            
    // desktop.ini in the folder. This is how our customizations (i.e. 
            
    // pszIcon, pszTooltip, pszLocalizedName) get picked up by the Shell. 

            PWSTR pszPath 
    = NULL; 
            hr 
    = SHGetKnownFolderPath(kfid, KF_FLAG_CREATE | KF_FLAG_INIT, NULL, 
                
    &pszPath); 
            
    if (FAILED(hr)) 
            { 
                
    // Failed to initialize and create the known folder 
                _tprintf(_T("SHGetKnownFolderPath failed w/err 0x%08lx\n"), hr); 

                
    // Unregister the known folder because of the failure 
                UnregisterKnownFolder(kfid); 
            } 
            
    else 
            { 
                wprintf(L
    "The known folder is registered and created:\n%s\n"
                    pszPath); 

                
    // Must free the pszPath output of SHGetKnownFolderPath 
                CoTaskMemFree(pszPath); 
            } 
        } 
        
    return hr; 


    /*
    * Register a known folder. The function requires administrator privilege, 
    * so please make sure that the routine is run as administrator. 
    */ 
    HRESULT RegisterKnownFolder(REFKNOWNFOLDERID kfid) 

        HRESULT hr; 

        
    ////////////////////////////////////////////////////////////////////////
        // Define your known folder through a KNOWNFOLDER_DEFINITION structure. 
        
    // 

        KNOWNFOLDER_DEFINITION kfd 
    = {}; 
        kfd.category 
    = KF_CATEGORY_PERUSER; 
        kfd.pszName 
    = L"CodeFx KnownFolder";    // Known folder canonical name 
        kfd.pszDescription= L"This is a CodeFx sample known folder"

        
    // fidParent and pszRelativePath work together. pszRelativePath specifies 
        
    // a path relative to the parent folder specified in fidParent. 
        kfd.fidParent = FOLDERID_Profile; 
        kfd.pszRelativePath 
    = L"CodeFxKnownFolder"

        
    // pszParsingName points to Shell namespace folder path of the folder, 
        
    // stored as a null-terminated Unicode string. Applies to virtual folders 
        
    // only. For example, ::%CLSID_MyComputer%\::%CLSID_ControlPanel% is the 
        
    // parsing name of Control Panel. 
        GUID guid; 
        CoCreateGuid(
    &guid); 
        kfd.pszParsingName 
    = (PWSTR)CoTaskMemAlloc(sizeof(WCHAR) * GUID_SIZE); 
        
    if (kfd.pszParsingName) 
        { 
            StringFromGUID2(guid, kfd.pszParsingName, GUID_SIZE); 
        } 

        
    // Get the current exe module path for the pszTooltip, pszLocalizedName, 
        
    // and pszIcon fields. 
        WCHAR szExePath[MAX_PATH] = {}; 
        GetModuleFileName(NULL, szExePath, ARRAYSIZE(szExePath)); 
        size_t cch 
    = ARRAYSIZE(szExePath) + 10;    // +10 as a flexible buffer 

        
    // pszTooltip points to the default tool tip resource used for this known 
        
    // folder when it is created. This is a null-terminated Unicode string in 
        
    // this form: @Module name, Resource ID 
        
    // Here we use the current exe module to store the string resource. 
        kfd.pszTooltip = (PWSTR)CoTaskMemAlloc(sizeof(WCHAR) * cch); 
        
    if (kfd.pszTooltip) 
        { 
            ZeroMemory(kfd.pszTooltip, 
    sizeof(WCHAR) * cch); 
            StringCchPrintfW(kfd.pszTooltip, cch, L
    "@%s,-%d", szExePath, 
                IDS_CODEFXKF_TOOLTIP); 
        } 

        
    // pszLocalizedName points to the default localized name resource used 
        
    // when the folder is created. This is a null-terminated Unicode string 
        
    // in this form: @Module name, Resource ID 
        
    // Here we use the current exe module to store the string resource. 
        kfd.pszLocalizedName = (PWSTR)CoTaskMemAlloc(sizeof(WCHAR) * cch); 
        
    if (kfd.pszLocalizedName) 
        { 
            ZeroMemory(kfd.pszLocalizedName, 
    sizeof(WCHAR) * cch); 
            StringCchPrintfW(kfd.pszLocalizedName, cch, L
    "@%s,-%d", szExePath, 
                IDS_CODEFXKF_LOCALIZEDNAME); 
        } 

        
    // pszIcon points to the default icon resource used when the folder is 
        
    // created. This is a null-terminated Unicode string in this form: 
        
    // Module name, Resource ID 
        
    // Here we use the current exe module to store the icon resource. 
        kfd.pszIcon = (PWSTR)CoTaskMemAlloc(sizeof(WCHAR) * cch); 
        
    if (kfd.pszIcon) 
        { 
            ZeroMemory(kfd.pszIcon, 
    sizeof(WCHAR) * cch); 
            StringCchPrintfW(kfd.pszIcon, cch, L
    "%s,-%d", szExePath, 
                IDI_CODEFXKF_ICON); 
        } 

        
    ////////////////////////////////////////////////////////////////////////
        // Register the known folder through a call to RegisterFolder. 
        
    // 

        
    // Create IKnownFolderManager instance 
        IKnownFolderManager* pkfm = NULL; 
        hr 
    = CoCreateInstance(CLSID_KnownFolderManager, NULL, 
            CLSCTX_INPROC_SERVER, IID_PPV_ARGS(
    &pkfm)); 
        
    if (SUCCEEDED(hr)) 
        { 
            hr 
    = pkfm->RegisterFolder(kfid, &kfd); 
            
    if (FAILED(hr)) 
            { 
                _tprintf(_T(
    "IKnownFolderManager::RegisterFolder failed w/err ") \ 
                    _T(
    "0x%08lx\nPlease run as admin to register a known folder\n"), 
                    hr); 
            } 
            pkfm
    ->Release(); 
        } 

        
    return hr; 
    }
  • 相关阅读:
    BZOJ2555 SubString(后缀自动机+LCT)
    Luogu4770 NOI2018你的名字(后缀自动机+线段树合并)
    Luogu5284 十二省联考2019字符串问题(后缀树+拓扑排序)
    Codeforces Round #557 Div. 1 based on Forethought Future Cup
    【JS】window.print打印指定内容
    【PHP】Thinkphp 七牛云API对接
    【JS】JS实现时间戳转换成普通时间
    【CSS】非常简单的css实现div悬浮页面底部
    【PHP】php中json_decode()和json_encode()
    【Linux】安装mysql之设置远程访问权限
  • 原文地址:https://www.cnblogs.com/Jialiang/p/1579074.html
Copyright © 2011-2022 走看看