void InsertMenu(CListCtrl& list, CMenu* pMenu, UINT nIndex, // Menu id to remplace CImageList* pImageList) { CHeaderCtrl *pHeader = list.GetHeaderCtrl(); int nbCol = pHeader->GetItemCount(); int nAfter = -1, id; char buffer[maxTitleLength]; HDITEM hi; if (pImageList == NULL) pImageList = pHeader->GetImageList(); hi.mask = HDI_FORMAT|HDI_IMAGE|HDI_LPARAM|HDI_TEXT; hi.pszText = buffer; hi.cchTextMax = maxTitleLength; while (--nbCol >= 0) { pHeader->GetItem(nbCol, &hi); if (hi.lParam == 0) continue; // there is a sort possible column id = nIndex + nbCol; if (nAfter < 0) { if (!pMenu->ModifyMenu(nIndex, MF_BYCOMMAND|MF_STRING, id, buffer)) return; } else { pMenu->InsertMenu(nAfter, MF_BYCOMMAND|MF_STRING, id, buffer); } if (hi.iImage && pImageList) { static CBitmap bm, *pOldBm; IMAGEINFO ii; CDC dcMem; // fill a bitmap with the sort image pImageList->GetImageInfo(hi.iImage, &ii); int w = ii.rcImage.right - ii.rcImage.left; int h = ii.rcImage.bottom - ii.rcImage.top; dcMem.CreateCompatibleDC(NULL); if (bm.m_hObject) bm.DeleteObject(); bm.CreateCompatibleBitmap(&dcMem, w, h); pOldBm = dcMem.SelectObject(&bm); dcMem.FillSolidRect(0, 0, w, h, 0xFFFFFF); pImageList->Draw(&dcMem, hi.iImage, CPoint(0,0), ILD_NORMAL); dcMem.SelectObject(pOldBm); // display the sort icon pMenu->SetMenuItemBitmaps(id, MF_BYCOMMAND, &bm, &bm); } nAfter = id; } }