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.
KlavaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KlavaAuthor Commented:
The problem has been solved bymyself
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.