MFC怎么给CListCtrl添加背景图片?

2025-04-29 14:43:33
推荐回答(1个)
回答1:

if (bEnable)
{
/*
There are two ways to include a background image:
(1) Specify a valid filepath and load the bitmap handle from there
(2) Specify a valid URL using the res:protocol (preferred).

(2) Is preferable because you don't need to include a separate file
with your application. You can embed the BMP in the resource fork
and use the RES: protocol to have the ListControl load it. See
www.microsoft.com/...99.asp for more
information about the RES: syntax. This idea was suggested by Nick Hodapp
(www.codetools.com/...id=162)

Both methods are included for completeness, but the sample
application uses the embedded BMP. Note that specifying a HBITMAP
in the struct is not currently supported.
*/

// Load the resource and apply it to the background
TCHAR szBuffer[_MAX_PATH];
VERIFY(::GetModuleFileName(AfxGetInstanceHandle(), szBuffer, _MAX_PATH));
CString sPath;
sPath.Format(_T("res://%s/#2/#142"),szBuffer);

LVBKIMAGE bki;
bki.ulFlags = LVBKIF_STYLE_TILE | LVBKIF_SOURCE_URL ;
bki.pszImage = sPath.GetBuffer(sPath.GetLength());
bki.cchImageMax = sPath.GetLength();
VERIFY(m_cListCtrl.SetBkImage( &bki));

/*
Construct the path to the BMP. Although the CListCtrl::SetBkImage docs
indicate that you can use a n HBITMAP, the latest docs on the LVBKIMAGE
underlying struct indicates that the LVBKIMAGE.hbm member is not used.
*/
/*
TCHAR szBuffer[_MAX_PATH];
VERIFY(::GetModuleFileName(AfxGetInstanceHandle(), szBuffer, _MAX_PATH));
CString sPath = (CString)szBuffer;
sPath = sPath.Left(sPath.ReverseFind('\\') + 1);
sPath += "bk.bmp";
m_cListCtrl.SetBkImage( sPath.GetBuffer(sPath.GetLength()), TRUE);
sPath.ReleaseBuffer();
*/

// Whichever one you choose, log the path used
CString str;
str.Format(_T("Set background image: %s"), sPath.GetBuffer(sPath.GetLength()));
m_Log.AppendString(str);
sPath.ReleaseBuffer();

}
else
{
m_cListCtrl.SetBkImage( HBITMAP(0));
m_Log.AppendString(_T("Cleared background image."));

}