CComboBox - Icon vor jedem Eintrag, wie?



  • Hallo,

    wie bekomme ich vor jeden Eintrag ein Icon?
    Man kann ja nicht wirklich bestimmen, vor welchen Eintrag welches Icon kommt. Es gibt die Funktion SetIcon, aber die bewirkt nicht wirklich was. Genau genommen garnichts 😉

    combo->SetIcon(LoadIcon(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_ICON_FLOPPY)),false);
    combo->AddString("hurz");
    

    so dachte ich irgendwie.. aber Sinn macht es nicht unbedingt..

    Danke für jede Hilfe.



  • entweder OwnerDraw ComboBox oder ComboBoxEx.



  • Schau dir das mal an
    http://www.codeguru.com/listbox/IconLB.html
    und übertrage die Ideen auf CComboBox



  • Das einfachste ist sicher eine CComboBoxEx.

    Die Icons malst du alle nebeneinander in ein Bitmap, daraus konstruierst du eine CImageList (Member vom Dialog). mit cbx.SetImageList der ComboBox bescheid sagen, wo die Boilder herkommen sollen, und dann noch eine kleine Hilfsfunktion:

    /// \param cbx: Pointer to the ComboBoxEx ctrl
    /// \param item: insertion index, or -1 to append at end
    /// \param text: Text of the new item
    /// \param img: icon index in the image list (provided by SetImageList)
    /// \param selImg: icon image in the image list to display when the item is selected
    /// \param lp: caller defined parameter associated with the item ("ItemData")
    bool CBxInsertItem(CComboBoxEx * wnd, int item, LPCTSTR text, int img = -1, int selImg = -1, LPARAM lp = 0)
    {
      _ASSERTE(::IsWindow(wnd->GetSafeHwnd()));
      if (!::IsWindow(wnd->GetSafeHwnd())) 
        return -1;
    
      COMBOBOXEXITEM cbx;
      ZeroMemory(&cbxItem, sizeof(cbxItem));
      if (item < 0)     // append item at end:
        item = wnd->GetItemCount();  
      cbx.mask = CBEIF_TEXT | CBEIF_LPARAM;
      cbx.iItem = item;
      cbx.pszText = text;
      if (img >= 0) {  // add image
        cbx.mask |= CBEIF_IMAGE;
        cbx.iImage = img;
      }
      if (selIng >= 0) {  // add selected image
        cbx.mask |= CBEIF_SELECTEDIMAGE;
        cbx.iImage = selImg;
      }
      return wnd->InsertItem(&cbx);
    
    }
    

Anmelden zum Antworten