I have a treeview in my program for which I am adding a bitmap of an open and closed folders. I have included messageboxes to show where the program fails. It fails at the loadbitmap function. I appreciate it if anybody can tell me what is wrong. Below is the code.

hTreeView = CreateWindowEx(0, WC_TREEVIEW, (LPCTSTR)NULL,
     WS_CHILD | WS_VISIBLE | WS_THICKFRAME | TVS_HASBUTTONS | TVS_HASLINES
     | TVS_LINESATROOT | TVS_DISABLEDRAGDROP | TVS_TRACKSELECT | TVS_SINGLEEXPAND
     | TVS_FULLROWSELECT, 0, wndHeight(hToolBar), rcClient.right / 5, 
     rcClient.bottom - wndHeight (hToolBar) - wndHeight (hStatusBar),
    hwnd, (HMENU)IDC_TREEVIEW, GetModuleHandle(NULL), NULL);
 
 
 
    hImageList = ImageList_Create (16, 16, ILC_COLOR16, 2,4);
 
 
 
    if (hImageList == FALSE)
   MessageBox (NULL, TEXT ("Could not create image list"), 
   TEXT ("WARNING"), MB_ICONERROR) ;
 
 
    hBitMap = LoadBitmap (((LPCREATESTRUCT) lParam)->hInstance, MAKEINTRESOURCE(IDB_TREE));
 
 
    if (hBitMap == NULL)
   MessageBox (NULL, TEXT ("Could not load bitmap into image list"), 
   TEXT ("WARNING"), MB_ICONERROR) ;
 
 
    ImageList_Add (hImageList, hBitMap, NULL);
    DeleteObject (hBitMap);
 
 
    if (ImageList_GetImageCount(hImageList) < 2)
     MessageBox (NULL, TEXT ("Could not add images to list"), 
   TEXT ("WARNING"), MB_ICONERROR) ;
 
 
    SendMessage (GetDlgItem(hwnd, IDC_TREEVIEW), TVM_SETIMAGELIST, 
     0, (LPARAM)hImageList);

So basically I get the second error messagebox that says: "Could not load bitmap into image list"

Thank you in advance.

Dani AI

Generated

A few practical notes that tie the thread together and help if the same symptom appears later. As suggested, a failing resource load usually comes down to one of: the wrong HINSTANCE, a bad resource ID/type, or a corrupt/non-BITMAP resource. later confirmed the bitmap itself was the culprit — but it’s worth using a short checklist so future debugging is faster and less painful.

  • Verify the resource: confirm the ID in the .rc matches what you pass (MAKEINTRESOURCE(IDB_xxx)) and that the resource type is actually BITMAP (not a PNG or custom data).
  • Use the right HINSTANCE: for an EXE use GetModuleHandle(NULL) (or the hInstance passed to CreateWindow), for a DLL use the DLL’s instance. Incorrect hInstance is a very common silent failure.
  • Prefer LoadImage over LoadBitmap for robustness: LoadImage with LR_CREATEDIBSECTION avoids palette/DDB problems and lets you specify LR_LOADFROMFILE when testing with external files.
  • Image list tips: create the list with modern flags (e.g. ILC_COLOR32 | ILC_MASK) if you need alpha/transparency, and use ImageList_AddMasked or ImageList_Add/ReplaceIcon as appropriate. Always check return values and ImageList_GetImageCount after adding.

For : a standard ListBox has no built-in HIMAGELIST support. The usual approaches are (a) make the listbox owner-drawn (LBS_OWNERDRAWFIXED or _VARIABLE) and draw images yourself in WM_DRAWITEM via ImageList_Draw, or (b) switch to a ListView, which supports image lists natively and makes item reordering and hit-testing much easier. Example owner-draw sketch:

case WM_DRAWITEM: {
  LPDRAWITEMSTRUCT dis = (LPDRAWITEMSTRUCT)lParam;
  if (dis->CtlType == ODT_LISTBOX && dis->itemID != -1) {
    int img = (int)SendMessage(dis->hwndItem, LB_GETITEMDATA, dis->itemID, 0);
    ImageList_Draw(hImageList, img, dis->hDC, dis->rcItem.left + 2, dis->rcItem.top, ILD_TRANSPARENT);
    // draw text to the right of the image...
  }
  break;
}

These steps should make resource loads and image handling more predictable and easier to debug.

Recommended Answers

All 3 Replies

Well, it seems likely the problem is in one of the two parameters you pass to the LoadBitmap function. You should check the return value of GetLastError after LoadBitmap fails, and look it up here.

Thank you very much for you help I haven't thought about GetlLastError before. Anyways, I found out that the code is totally correct but my bitmap file was corrupted. It is working now. But I might come back again on this Treeview control since I never used before

Thank you again.

listen all. how can I use HIMAGELIST on listbox? I want to use HIMAGELIST on listbox because I want to move listbox items wherever I want.

commented: Naughty naughty ... don't bump threads! +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.