Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 983
  • Last Modified:

CHeaderCtrl GetItem does not return correctly previosly set data

I need to attach images to my CListCtrl; and I am doing the standard things there. However, the structure does not seems to be set correctly. I am puzzled what I am doing wrong.

This is how I initialize my image list to the header:

  CHeaderCtrl *theHeaderCtrl = this->GetHeaderCtrl();
  if (theHeaderCtrl != NULL)
  {
    m_HeaderImageList.Create(IDB_LISTCTRL_COL_SORT, 10, 1, RGB(192,192,192));
    theHeaderCtrl->SetImageList(&m_HeaderImageList);
    HD_ITEM headerItem;
    for (int i=0; i<NumColumns; i++)
    {
      headerItem.mask = HDI_FORMAT;
      theHeaderCtrl->GetItem(i, &headerItem);
      headerItem.mask = HDI_IMAGE;
      headerItem.iImage= 0; // be default there is no image
      headerItem.fmt |= HDF_IMAGE | HDF_BITMAP_ON_RIGHT;
      theHeaderCtrl->SetItem(i, &headerItem);
    }
  }

but when I check what was set in a different method I get wrong results:

CHeaderCtrl *theHeaderCtrl = this->GetHeaderCtrl();
  if (theHeaderCtrl != NULL)
  {
    int numHeaderCol = theHeaderCtrl->GetItemCount(); // returns a correct number of items
    for (int i=0; i<numHeaderCol; i++)
    {
      theHeaderCtrl->GetItem(i, &headerItem);
      int image = headerItem.iImage;   // returns garbage instead of 0 !!!
    }
 }
What am I doing wrong? I would appreciate a details explanation.
0
Klava
Asked:
Klava
  • 3
2 Solutions
 
jkrCommented:
Have you tried the following?
    for (int i=0; i<NumColumns; i++)
    {
      ZeroMemory(&headerItem,sizeof(headerItem)); // <---!
      headerItem.mask = HDI_FORMAT;
      theHeaderCtrl->GetItem(i, &headerItem);
      headerItem.mask = HDI_IMAGE;
      headerItem.iImage= 0; // be default there is no image
      headerItem.fmt |= HDF_IMAGE | HDF_BITMAP_ON_RIGHT;
      theHeaderCtrl->SetItem(i, &headerItem);
    }
  }

Open in new window

0
 
KlavaAuthor Commented:
Just tried it. Unfortunately It did not help.

I need to be able to save and retrieve the latest image used for a specific column; and I hoped that theHeaderCtrl->SetItem/GetItem should be able to do so, but it is not
0
 
KlavaAuthor Commented:
I found the solution myself.
In order to get the right settings you should specify the same mask as it was used for SetItem, so

if used
headerItem.mask = HDI_FORMAT; | HDI_IMAGE;
 theHeaderCtrl->SetItem(i, &headerItem);

the fpr GetItem use this:
headerItem.mask = HDI_FORMAT; | HDI_IMAGE;
theHeaderCtrl->GetItem(i, &headerItem);

0
 
KlavaAuthor Commented:
The problem has been solved bymyself
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now