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


Comments

  • A Better Approach

    Posted by keedo60 on 11/28/2004 02:16pm

    Erasing the image is unavoidable unless you
    know a way to make the text background 
    transparent.  I don't know of one.
    
    A better approach is to set background color of 
    the control, set text background to same color, 
    and then set text foreground to whatever you 
    like.  This has the same effect of transparent 
    text background.  And you don't need to 
    initialize COM to achieve this -- and you don't 
    need owner drawn control either.  Just add these 
    three lines wherever you initialize your control 
    -- usually OnInitDialog().
    
    	m_ListCtrl.SetBkColor(0x00E6EEF2);
    	m_ListCtrl.SetTextBkColor(0x00E6EEF2);
    	m_ListCtrl.SetTextColor(0x00253342);
    
    You can also set these using a COLORREF variable 
    instead of hard-coding as I have done.  This way 
    you could do a registry call to determine active 
    Windows colors and set your control to match.
    
    The only caveat to this is that MFC looks for a 
    BGR order in the hex code, which is opposite from 
    what most photo editors, HTML editors and other 
    color mappers give you in a hex color code.  So 
    if your photo editor gives you #AABBCC you will 
    have to set your COLORREF variable to 0x00CCBBAA.
    
    That's all there is to it.  Experiment with 
    colors, gridlines, borders, etc, and you can 
    create some awesome appearances without skinning 
    or messing with owner draw.

    Reply
  • Release/Debug prob!!

    Posted by Legacy on 01/03/2004 12:00am

    Originally 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 ??

    Reply
  • How to stretch the bitmap?

    Posted by Legacy on 07/25/2003 12:00am

    Originally posted by: MAV

    How to stretch the bitmap?

    Reply
  • Is it possible to print the backgroundimage?

    Posted by Legacy on 05/20/2003 12:00am

    Originally posted by: Daniel

    Hello

    How can I print the background image??
    I have used the CPrintListCtrl but it only
    prints the text in the listctrl.

    Reply
  • Good Job But Some Flaws...

    Posted by Legacy on 05/07/2003 12:00am

    Originally posted by: Lakshmi

    Good Job !!! But the Background Image gets Erased line by line when an Entry is added to the list. Any Ideas... ??

    • Making transparent ITEM

      Posted by jundax on 06/30/2005 07:38am

      much more better solution(it realy works!): m_ListCtrl.SetBkColor(CLR_NONE); m_ListCtrl.SetTextBkColor(CLR_NONE);

      Reply
    • A Better Approach

      Posted by keedo60 on 11/28/2004 02:11pm

      Erasing the image is unavoidable unless you
      know a way to make the text background 
      transparent.  I don't know of one.
      
      A better approach is to set background color of 
      the control, set text background to same color, 
      and then set text foreground to whatever you 
      like.  This has the same effect of transparent 
      text background.  And you don't need to 
      initialize COM to achieve this -- and you don't 
      need owner drawn control either.  Just add these 
      three lines wherever you initialize your control 
      -- usually OnInitDialog().
      
      	m_ListCtrl.SetBkColor(0x00E6EEF2);
      	m_ListCtrl.SetTextBkColor(0x00E6EEF2);
      	m_ListCtrl.SetTextColor(0x00253342);
      
      You can also set these using a COLORREF variable 
      instead of hard-coding as I have done.  This way 
      you could do a registry call to determine active 
      Windows colors and set your control to match.
      
      The only caveat to this is that MFC looks for a 
      BGR order in the hex code, which is opposite from 
      what most photo editors, HTML editors and other 
      color mappers give you in a hex color code.  So 
      if your photo editor gives you #AABBCC you will 
      have to set your COLORREF variable to 0x00CCBBAA.
      
      That's all there is to it.  Experiment with 
      colors, gridlines, borders, etc, and you can 
      create some awesome appearances without skinning 
      or messing with owner draw.

      Reply
    Reply
  • It's quite good!

    Posted by Legacy on 05/06/2003 12:00am

    Originally posted by: George Gao

    Excellent work!
    The article is very interesting!
    Thanks!

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Email is the most common communication vehicle used by organizations of all shapes and sizes. Among the billions of email messages sent every day are sensitive information, critical requests, and other essential business data. IT staff bear the burden of ensuring the confidentiality, integrity, and availability of the information contained within the communication. This white paper explores the email security landscape, an assessment of the threats organizations face,  and the building blocks of an effective …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds