STDMETHODIMP CTaskbar7::SetTabsIcon(BSTR icon) { // Remove icon if icon path is empty if (CComBSTR(icon) == CComBSTR("")) { if (m_isWindows7) SetClassLong(m_hwnd, GCL_HICON, NULL); return S_OK; } USES_CONVERSION; Bitmap* bitmap = NULL; HRESULT hr = LoadImageFromFile(OLE2W(icon), &bitmap); // return a proper error if (!SUCCEEDED(hr)) return hr; // Do nothing on XP & Vista if (!m_isWindows7) { delete bitmap; return S_OK; } HICON hIcon; bitmap->GetHICON(&hIcon); SetClassLong(m_hwnd, GCL_HICON, (LONG)hIcon); // Cleanup the HICON & Bitmap DestroyIcon(hIcon); delete bitmap; return S_OK; }
// 读取图标(从内存中加载) BOOL LoadIconFromMem(BYTE* pByte, DWORD dwSize, HICON &hIcon) { // 根据文件大小分配HGLOBAL内存 HGLOBAL hGlobal = GlobalAlloc( GMEM_MOVEABLE | GMEM_NODISCARD, dwSize ); if ( !hGlobal ) { TRACE( _T( "Load (file): Error allocating memory\n" ) ); return FALSE; }; char *pData = reinterpret_cast<char*>(GlobalLock(hGlobal)); if ( !pData ) { TRACE( _T( "Load (file): Error locking memory\n" ) ); GlobalFree( hGlobal ); return FALSE; }; // 将文件内容读到HGLOBAL内存中 memcpy(pData, pByte, dwSize); GlobalUnlock( hGlobal ); // 利用hGlobal内存中的数据创建stream IStream *pStream = NULL; if ( CreateStreamOnHGlobal( hGlobal, TRUE, &pStream ) != S_OK ) { return FALSE; } Bitmap* pBitmap = Gdiplus::Bitmap::FromStream(pStream); // 要加上这一句,否则由GlobalAlloc得来的hGlobal内存没有被释放,导致内存泄露,由于 // CreateStreamOnHGlobal第二个参数被设置为TRUE,所以调用pStream->Release()会自动 // 将hGlobal内存(参见msdn对CreateStreamOnHGlobal的说明) pStream->Release(); HBITMAP hBitmap = NULL; Status status = pBitmap->GetLastStatus(); if(Ok == status) { status = pBitmap->GetHICON(&hIcon); delete pBitmap; if(Ok == status) { return TRUE; } } return FALSE; }
HRESULT CTaskbar7::LoadButton(int id, wstring path, wstring tooltip, int flags, THUMBBUTTON* button) { Bitmap* bitmap = NULL; HRESULT hr = LoadImageFromFile(path, &bitmap); if (!SUCCEEDED(hr)) return hr; button->dwMask = THB_ICON | THB_TOOLTIP | THB_FLAGS; button->iId = id; bitmap->GetHICON(&button->hIcon); wcscpy_s((wchar_t *)&button->szTip, 260, tooltip.c_str()); button->dwFlags = (THUMBBUTTONFLAGS)flags; delete bitmap; return S_OK; }
/************************************* * Overlay *************************************/ STDMETHODIMP CTaskbar7::SetOverlayIcon(BSTR path, BSTR description) { if (!m_pTaskbarList || m_parentHwnd == NULL) return S_OK; USES_CONVERSION; wstring image(OLE2W(path)); wstring desc(OLE2W(description)); // if no path is given, we remove the overlay if (image.empty()) { if (m_isWindows7) return m_pTaskbarList->SetOverlayIcon(m_parentHwnd, NULL, L""); return S_OK; } Bitmap* bitmap = NULL; HRESULT hr = LoadImageFromFile(image, &bitmap); if (!SUCCEEDED(hr)) return hr; // Do nothing on XP & Vista if (!m_isWindows7) { delete bitmap; return S_OK; } HICON pIcon; bitmap->GetHICON(&pIcon); // Sets the overlay icon hr = m_pTaskbarList->SetOverlayIcon(m_parentHwnd, pIcon, desc.c_str()); DestroyIcon(pIcon); delete bitmap; return hr; }
//---------------------------------------------------------------------------------------------------------------------- // DO IMAGE LIST //---------------------------------------------------------------------------------------------------------------------- void cListView :: do_image_list ( const cFigViewItemList * pList ) { mojo::cPtI IconSize = get_icon_size (); int iDimX = IconSize.x; int iDimY = IconSize.y; HIMAGELIST hIL = ImageList_Create ( iDimX, iDimY, LR_CREATEDIBSECTION | ILC_COLOR32, pList->qty(), 1 ); //------------------------------------------ // LOAD DEFAULT IMAGES //------------------------------------------ const int * aiBitmap = default_bitmap_ids (); for ( int i = 0; aiBitmap[i]; i++ ) { HANDLE hBitmap = LoadImage ( g_hInstance, MAKEINTRESOURCE ( aiBitmap[i] ), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION ); ImageList_Add ( hIL, (HBITMAP) hBitmap, NULL ); } int iItemIndex = 0; for ( cFigViewItem * p = pList->pHead; p; p = p->pNext, iItemIndex++ ) { bool bLoadedFromFile = false; int iImageIndex = 0; //------------------------------- // TRY TO LOAD IMAGE FROM FILE //------------------------------- if ( p->sIconPath.len() ) { if ( ! file_exists ( p->sIconPath.cstr() ) ) { mojo::put_ad_lib_memo ( mojo::cMemo::error, L"Image file not found", L"%s", p->sIconPath.cstr() ); cStrW s; s.f ( L"Image file not found: %s", p->sIconPath.cstr() ); LOG ( s.cstr() ); } else { HICON hIcon; Bitmap bm ( p->sIconPath.cstr() ); if ( ! ( Ok == bm.GetLastStatus() ) ) LOG_V ( L"Bitmap constructor failed with %s.", p->sIconPath.cstr() ); else { bm.GetHICON(&hIcon); if ( Ok != bm.GetLastStatus() ) LOG_V ( L"Bitmap::GetHICON() failed with %s", p->sIconPath.cstr() ); else { iImageIndex = ImageList_AddIcon( hIL, hIcon ); bLoadedFromFile = true; } } if ( ! bLoadedFromFile) mojo::put_ad_lib_memo ( mojo::cMemo::error, L"Unable to display image file", L"%s", p->sIconPath.cstr() ); } } if ( ! bLoadedFromFile ) { iImageIndex = 0;; } #if 0 LVITEM lvi; ZeroMemory ( &lvi, sizeof(lvi) ); lvi.mask = LVIF_IMAGE; lvi.iItem = iItemIndex; lvi.iImage = iImageIndex; ListView_SetItem ( hwnd, &lvi ); #endif } HIMAGELIST hResult = ListView_SetImageList ( hwnd, hIL, LVSIL_NORMAL ); hResult; }