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:

  1. Use AppWizard to make a dialog-based application.
  2. On the dialog resource, place a listcontrol (Drag & Drop).
  3. Make the control a Report Control.
  4. 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.

Downloads

Download source - 482 Kb