bool CCommonAppUtils::SetListCtrlBackgroundImage(HWND hListCtrl, UINT nID, int width /* = 128 */, int height /* = 128 */) { if ((((DWORD)CRegStdDWORD(_T("Software\\TortoiseGit\\ShowListBackgroundImage"), TRUE)) == FALSE)) return false; ListView_SetTextBkColor(hListCtrl, CLR_NONE); COLORREF bkColor = ListView_GetBkColor(hListCtrl); // create a bitmap from the icon HICON hIcon = (HICON)LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE(nID), IMAGE_ICON, width, height, LR_DEFAULTCOLOR); if (!hIcon) return false; RECT rect = {0}; rect.right = width; rect.bottom = height; HBITMAP bmp = NULL; HWND desktop = ::GetDesktopWindow(); if (desktop) { HDC screen_dev = ::GetDC(desktop); if (screen_dev) { // Create a compatible DC HDC dst_hdc = ::CreateCompatibleDC(screen_dev); if (dst_hdc) { // Create a new bitmap of icon size bmp = ::CreateCompatibleBitmap(screen_dev, rect.right, rect.bottom); if (bmp) { // Select it into the compatible DC HBITMAP old_dst_bmp = (HBITMAP)::SelectObject(dst_hdc, bmp); // Fill the background of the compatible DC with the given color ::SetBkColor(dst_hdc, bkColor); ::ExtTextOut(dst_hdc, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL); // Draw the icon into the compatible DC ::DrawIconEx(dst_hdc, 0, 0, hIcon, rect.right, rect.bottom, 0, NULL, DI_NORMAL); ::SelectObject(dst_hdc, old_dst_bmp); } ::DeleteDC(dst_hdc); } ::ReleaseDC(desktop, screen_dev); } } // Restore settings DestroyIcon(hIcon); if (bmp == NULL) return false; LVBKIMAGE lv; lv.ulFlags = LVBKIF_TYPE_WATERMARK; lv.hbm = bmp; lv.xOffsetPercent = 100; lv.yOffsetPercent = 100; ListView_SetBkImage(hListCtrl, &lv); return true; }
void NListView::ListView_SetBackgroundImage(HWND hListView,UINT uImage) { TCHAR szModuleName[MAX_PATH]; GetModuleFileName(NULL,szModuleName,SIZEOF_ARRAY(szModuleName)); LVBKIMAGE lvbki; lvbki.ulFlags = LVBKIF_STYLE_NORMAL|LVBKIF_SOURCE_URL; lvbki.xOffsetPercent = 45; lvbki.yOffsetPercent = 50; TCHAR szBitmap[512]; if(uImage == 0) { lvbki.pszImage = NULL; } else { /* 2 means an image resource, 3 would mean an icon. */ StringCchPrintf(szBitmap,SIZEOF_ARRAY(szBitmap), _T("res://%s/#2/#%d"),szModuleName,uImage); lvbki.pszImage = szBitmap; } ListView_SetBkImage(hListView,&lvbki); }