1. 程式人生 > >《北塔教你做外掛 從RibbonX開始》第三講:再建Ribbon——ATL的實現方法

《北塔教你做外掛 從RibbonX開始》第三講:再建Ribbon——ATL的實現方法

                // IRibbonExtensibility Methods
                STDMETHOD(GetCustomUI)(BSTR RibbonID, BSTR * RibbonXml)
                {
                                 if(!RibbonXml)
                                                 return E_POINTER;

                                *RibbonXml = GetXMLResource(IDR_XML1); 
                                 return S_OK;
                }

                HRESULT HrGetResource( int nId,
                                LPCTSTR lpType,
                                LPVOID* ppvResourceData,      
                                DWORD* pdwSizeInBytes)
                {
                                HMODULE hModule = _AtlBaseModule.GetModuleInstance();
                                 if (!hModule)
                                                 return E_UNEXPECTED;
                                HRSRC hRsrc = FindResource(hModule, MAKEINTRESOURCE(nId), lpType);
                                 if (!hRsrc)
                                                 return HRESULT_FROM_WIN32(GetLastError());
                                HGLOBAL hGlobal = LoadResource(hModule, hRsrc);
                                 if (!hGlobal)
                                                 return HRESULT_FROM_WIN32(GetLastError());
                                *pdwSizeInBytes = SizeofResource(hModule, hRsrc);
                                *ppvResourceData = LockResource(hGlobal);
                                 return S_OK;
                }

                BSTR GetXMLResource( int nId)
                {
                                LPVOID pResourceData = NULL;
                                DWORD dwSizeInBytes = 0;
                                HRESULT hr = HrGetResource(nId, TEXT( "XML"),
                                                &pResourceData, &dwSizeInBytes);
                                 if (FAILED(hr))
                                                 return NULL;
                                 // Assumes that the data is not stored in Unicode.
                                CComBSTR cbstr(dwSizeInBytes, reinterpret_cast<LPCSTR>(pResourceData));
                                 return cbstr.Detach();
                }

                SAFEARRAY* GetOFSResource( int nId)
                {
                                LPVOID pResourceData = NULL;
                                DWORD dwSizeInBytes = 0;
                                 if (FAILED(HrGetResource(nId, TEXT("OFS" ),
                                                &pResourceData, &dwSizeInBytes)))
                                                 return NULL;
                                SAFEARRAY* psa;
                                SAFEARRAYBOUND dim = {dwSizeInBytes, 0};
                                psa = SafeArrayCreate(VT_UI1, 1, &dim);
                                 if (psa == NULL)
                                                 return NULL;
                                BYTE* pSafeArrayData;
                                SafeArrayAccessData(psa, ( void**)&pSafeArrayData);
                                memcpy(( void*)pSafeArrayData, pResourceData, dwSizeInBytes);
                                SafeArrayUnaccessData(psa);
                                 return psa;
                }