ListCtrl设置背景图标

源代码及可执行文件下载地址:http://files.cnblogs.com/rainboy2010/ListCtrlBKImage.zip

ListCtrl设置背景图标

主要代码如下:

BOOL CmfctestDlg::ListCtrl_SetBackground(HWND hListCtrl,LPCTSTR pszImagePath,int width,int height)

{

    ListView_SetTextBkColor(hListCtrl,CLR_NONE);



	COLORREF clrBK=ListView_GetBkColor(hListCtrl);



	HICON hIcon=(HICON)::LoadImage(AfxGetResourceHandle(),pszImagePath,IMAGE_ICON,width,height,LR_DEFAULTSIZE);

	if(!hIcon)return FALSE;



	HBITMAP hBitmap=NULL;

	HWND  hwndDesktopDC=::GetDesktopWindow();

	HDC hDesktopDC=::GetDC(hwndDesktopDC);

	if(hDesktopDC)

	{

		HDC hdcMem=CreateCompatibleDC(hDesktopDC);

		if(hdcMem)

		{

			hBitmap=CreateCompatibleBitmap(hDesktopDC,width,height);

			if(hBitmap)

			{

               HBITMAP hOldBitmap=(HBITMAP)::SelectObject(hdcMem,hBitmap);

			   CRect rect(0,0,width,height);

			   ::SetBkColor(hdcMem,clrBK);

               ::ExtTextOut(hdcMem,0,0,ETO_OPAQUE,&rect,NULL,0,NULL);

			   ::DrawIconEx(hdcMem,0,0,hIcon,width,height,0,NULL,DI_NORMAL);

			

			   ::SelectObject(hdcMem,hOldBitmap);

			}

			::DeleteDC(hdcMem);

		}

		::ReleaseDC(hwndDesktopDC,hDesktopDC);



	}

	DestroyIcon(hIcon);



	if(hBitmap==NULL)

	{

		return FALSE;

	}



	LVBKIMAGE lvBK;

	lvBK.hbm=hBitmap;

	lvBK.xOffsetPercent=100;

	lvBK.yOffsetPercent=100;

	lvBK.ulFlags=LVBKIF_TYPE_WATERMARK;



	ListView_SetBkImage(hListCtrl,&lvBK);



	return TRUE;





}

  

你可能感兴趣的:(list)