(continued)

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();
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);
}
}
SetIcon(m_hIcon, TRUE);
SetIcon(m_hIcon, FALSE);
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 );
LVBKIMAGE bki;
if (m_List1.GetBkImage(&bki) && (bki.ulFlags ==
LVBKIF_SOURCE_NONE))
{
m_List1.SetBkImage(TEXT("C:\\mahi.bmp"),TRUE);
}
return TRUE;
}
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);
......
.......
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.
Downloads
Download source - 482 Kb