Setting the Background Image for a List Control

Click here for a larger image.
Environment: The platform I used is Windows 2000. I compiled the code in VC++6.0.
I have been a member of CodeGuru for quite some time and had taken lot of help from respectable members. In gratitude for the help I got, and expecting to get more, I am presenting this very simple article on how to place a background image in a list control. Zafir Anjum had suggested a way but sadly the code was not made available for download.
I use a simple dialog-based application. Here are the steps to place a background image in ListControl:
- Use AppWizard to make a dialog-based application.
- On the dialog resource, place a listcontrol (Drag & Drop).
- Make the control a Report Control.
- Make a listcontrol variable for the same; in my case, it's m_List1.
In the OnInitDialog(), place the ListControl code as follows.
BOOL CListBkImageDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this
// automatically when the application's main window is not
// a dialog.
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// START HERE
// Insert Columns in the List Control
m_List1.InsertColumn(0,"HEADER_1",LVCFMT_LEFT,180,1);
m_List1.InsertColumn(1,"HEADER_2",LVCFMT_LEFT,180,1);
m_List1.InsertColumn(2,"HEADER_3",LVCFMT_LEFT,193,1);
ListView_SetExtendedListViewStyle(m_List1.m_hWnd,
LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES|
LVS_EX_FLATSB|LVS_EX_HEADERDRAGDROP );
// Insert Background Image into the ListControl
LVBKIMAGE bki;
if (m_List1.GetBkImage(&bki) && (bki.ulFlags ==
LVBKIF_SOURCE_NONE))
{
m_List1.SetBkImage(TEXT("C:\\mahi.bmp"),TRUE);
// Use your own Image Address Here
}
// END HERE
return TRUE; // return TRUE unless you set the focus to
// a control
}
Here, the SetBkImage() function takes the address of the image; in other words, a NULL terminated string as a parameter and a Boolean variable. The Boolean variable decides whether the image is tiled or not. After this, it's essential to make the COM libraries initialized. Because CListCtrl::SetBkImage makes use of OLE COM functionality, the OLE libraries must be initialized.
Go to the App class of the application and add the CoInitialize() function as shown below:
BOOL CListBkImageApp::InitInstance()
{
AfxEnableControlContainer();
CoInitialize(NULL); // Initialize COM libraries
......
.......
return FALSE;
}
That's it. Enjoy a background image in the ListControl. Simple, right?
To make the program more worthwhile, I have added a browse button. Press the button and you get a file dialog that asks you to select an image. Just select an image and that will become your new ListControl Background Image.
Please encourage me by giving your valuable input because this is my first article for CodeGuru.

Comments
A Better Approach
Posted by keedo60 on 11/28/2004 02:16pmRelease/Debug prob!!
Posted by Legacy on 01/03/2004 12:00amOriginally posted by: [ZiKRo]
When i'm running 'win32 debug' the image loads ok into the ListCtrl .. but when i'm running 'win32 release' the image doen't load at all :/ .. with no source change!!!
i'm loading the image by using res: protocol and i'm using AfxOleInit() into InitInstance()
also /OPT:REF link arg in release building discards all refs from
OLEAUT32.DLL
& COMDLG32.DLL.
Any ideas ?? Am i doing something wrong ??
ReplyHow to stretch the bitmap?
Posted by Legacy on 07/25/2003 12:00amOriginally posted by: MAV
How to stretch the bitmap?
Reply
Is it possible to print the backgroundimage?
Posted by Legacy on 05/20/2003 12:00amOriginally posted by: Daniel
Hello
How can I print the background image??
ReplyI have used the CPrintListCtrl but it only
prints the text in the listctrl.
Good Job But Some Flaws...
Posted by Legacy on 05/07/2003 12:00amOriginally posted by: Lakshmi
Good Job !!! But the Background Image gets Erased line by line when an Entry is added to the list. Any Ideas... ??
-
-
Reply
ReplyMaking transparent ITEM
Posted by jundax on 06/30/2005 07:38ammuch more better solution(it realy works!): m_ListCtrl.SetBkColor(CLR_NONE); m_ListCtrl.SetTextBkColor(CLR_NONE);
ReplyA Better Approach
Posted by keedo60 on 11/28/2004 02:11pmIt's quite good!
Posted by Legacy on 05/06/2003 12:00amOriginally posted by: George Gao
Excellent work!
ReplyThe article is very interesting!
Thanks!