// Hinzufügen einer Bitmap zur Toolbar, liefert Index des ersten Buttons der // Bitmap HRESULT CTRiASToolBar::AddBitmap (HINSTANCE hInst, UINT uiRsc, int nNumButtons, int *piOffset) { AFX_MANAGE_STATE(AfxGetModuleState()); ASSERT_VALID(this); ASSERT(::IsWindow(m_hWnd)); if (NULL != piOffset) *piOffset = -1; // für alle Fälle #if !defined(_USE_SEC_CLASSES) // Größen für das Control ausrechnen HBITMAP hBmp = NULL; if (NULL == hInst) hBmp = (HBITMAP)uiRsc; else { HRSRC hRsrcImageWell = ::FindResource(hInst, (LPCSTR)uiRsc, RT_BITMAP); if (hRsrcImageWell == NULL) return E_FAIL; // Bitmap not found hBmp = AfxLoadSysColorBitmap(hInst, hRsrcImageWell); } if (NULL == hBmp) return E_FAIL; // need complete bitmap size to determine size of images BITMAP bitmap; VERIFY(::GetObject(hBmp, sizeof(BITMAP), &bitmap)); int iWidth = bitmap.bmWidth / nNumButtons; int iHeight = bitmap.bmHeight; CSize sizeImage(iWidth, iHeight+1); CSize sizeButton(iWidth + 7, iHeight + 7); SetSizes(sizeButton, sizeImage); // Bitmap zu Control hinzufügen TBADDBITMAP tbab; tbab.hInst = NULL; tbab.nID = (UINT)hBmp; int iOffset = (int) DefWindowProc(TB_ADDBITMAP, (WPARAM)nNumButtons, (LPARAM)&tbab); if (NULL != piOffset) *piOffset = iOffset; return NOERROR; #else // einfach weiterreichen return m_pMainFrm -> AddBitmap (hInst, uiRsc, nNumButtons, piOffset); #endif // _USE_SEC_CLASSES }
// Support loading a toolbar from a resource BOOL CFlatToolbar::LoadToolBar(LPCTSTR lpszResourceName) { ASSERT_VALID(this); ASSERT(lpszResourceName != NULL); // determine location of the bitmap in resource fork HINSTANCE hInst = AfxFindResourceHandle(lpszResourceName, RT_TOOLBAR); HRSRC hRsrc = ::FindResource(hInst, lpszResourceName, RT_TOOLBAR); if (hRsrc == NULL) return FALSE; HGLOBAL hGlobal = LoadResource(hInst, hRsrc); if (hGlobal == NULL) return FALSE; CToolBarData* pData = (CToolBarData*)LockResource(hGlobal); if (pData == NULL) return FALSE; ASSERT(pData->wVersion == 1); UINT* pItems = new UINT[pData->wItemCount]; for (int i = 0; i < pData->wItemCount; i++) pItems[i] = pData->items()[i]; BOOL bResult = SetButtons(pItems, pData->wItemCount); delete[] pItems; if (bResult) { // set new sizes of the buttons CSize sizeImage(pData->wWidth, pData->wHeight); CSize sizeButton(pData->wWidth + 7, pData->wHeight + 7); SetSizes(sizeButton, sizeImage); // load bitmap now that sizes are known by the toolbar control bResult = LoadBitmap(lpszResourceName); } UnlockResource(hGlobal); FreeResource(hGlobal); return bResult; }