' OwnerDraw ListBox, this can also be extended to comboboxes

$INCLUDE "RAPIDQ.INC"

DECLARE SUB ListBoxDrawItem(Index AS INTEGER, State AS BYTE, Rect AS QRECT)
DECLARE SUB ListBoxMeasureItem(Index AS INTEGER, Height AS INTEGER)

DIM Font AS QFont
    Font.Size = 16
DIM Bitmap(10) AS QBitmap
DIM SRect AS QRECT
  SRect.Top = 0
  SRect.Left = 0
  SRect.Right = 20
  SRect.Bottom = 20

Bitmap(0).BMP = "FLOPPY.BMP"
Bitmap(0).Transparent = True
Bitmap(1).BMP = "BOOK.BMP"
Bitmap(1).Transparent = True
Bitmap(2).BMP = "COFFEE.BMP"
Bitmap(2).Transparent = True
Bitmap(3).BMP = "GAME5.BMP"
Bitmap(3).Transparent = True
Bitmap(4).BMP = "GAME5.BMP"
Bitmap(4).Transparent = True

CREATE Form AS QForm
  Center
  Caption = "OwnerDraw ListBox"
  CREATE ListBox AS QListBox
    Align = alClient
    Font = Font
    Style = lbOwnerDrawVariable               '-- Variable height
    OnDrawItem = ListBoxDrawItem
    OnMeasureItem = ListBoxMeasureItem        '-- Note, order IS important!
    ItemHeight = Bitmap(0).Height
    AddItems "Floppy Disk","Open Book","Coffee","TicTacToe"
    ItemIndex = 0
  END CREATE
  ShowModal
END CREATE


SUB ListBoxMeasureItem(Index AS INTEGER, Height AS INTEGER)
  ' This subrountine is needed ONLY if your list box is lbOwnerDrawVariable
  Height = Bitmap(Index).Height+2
END SUB


SUB ListBoxDrawItem(Index AS INTEGER, State AS BYTE, Rect AS QRECT)
  IF State = 0 THEN
    '-- Selected
    ListBox.FillRect(Rect.Left, Rect.Top, Rect.Right, Rect.Bottom, &H00FF00)
  ELSE
    ListBox.FillRect(Rect.Left, Rect.Top, Rect.Right, Rect.Bottom, &HFFFFFF)
  END IF
  ListBox.TextOut(100, Rect.Top+(Rect.Bottom-Rect.Top)/4, ListBox.Item(index), 0, -1)
  ListBox.Draw(Rect.Left, Rect.Top, Bitmap(Index).BMP)
END SUB
