.const
Listbox        db  'LISTBOX',0
FontMS         db  'MS Sans Serif',0
szSlashB       db  '\',0
szAll          db  '*.*',0
szClear        db  '          ',0

.data?
hList          dd  ?
hFontL         dd  ?
hFind          dd  ?

lf             LOGFONT   <?>

.code

;________________________________________________________________________________
WndProc proc  uses edx  hwnd:DWORD, wMsg, wParam, lParam
LOCAL    FindData:WIN32_FIND_DATA
LOCAL    dis:DRAWITEMSTRUCT
LOCAL    tm:TEXTMETRIC
LOCAL    hBrush:DWORD, Len, Ypos
LOCAL    szBuff[256]:BYTE

      .if wMsg == WM_CREATE

;---------- [Create the Listbox] ----------
         INVOKE     CreateWindowEx, 0, addr Listbox, 0, WS_CHILD or WS_VISIBLE or\
                    WS_BORDER or WS_VSCROLL or LBS_HASSTRINGS or LBS_SORT or\
                    LBS_OWNERDRAWFIXED or LBS_NOTIFY,
                    110, 5, 127, 75, hwnd, 85, hInst, 0
            mov     hList, eax

;---------- [Create the Font] ----------
         INVOKE     lstrcpy, addr lf.lfFaceName, addr FontMS
            mov     lf.lfHeight, -12
            mov     lf.lfWeight, 500
         INVOKE     CreateFontIndirect, addr lf
            mov     hFontL, eax    ; Listbox font

;---------- [Set the Font and the line height of the Listbox] ----------
         INVOKE     SendMessage, hList, WM_SETFONT, hFontL, 1
         INVOKE     SendMessage, hList, LB_SETITEMHEIGHT, 0, 13  ; List items

;---------- [Get the current dir and load the Listbox with its files] ----------
         INVOKE     GetCurrentDirectory, lengthof szBuff, addr szBuff
         INVOKE     lstrcat, addr szBuff, addr szSlashB
         INVOKE     lstrcat, addr szBuff, addr szAll
         INVOKE     FindFirstFile, addr szBuff, addr FindData
           test     eax, eax
             js     NoFile
            mov     hFind, eax
            jmp     Addem

Getem:
         INVOKE     FindNextFile, hFind, addr FindData
           test     eax, eax
             jz     AllDone

Addem:
         .if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||\
             (FindData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) ||\
             (FindData.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
               jmp     Getem
         .endif
;         INVOKE     lstrlen, addr FindData.cFileName
;            sub     eax, 3
;         INVOKE     lstrcpyn, addr szBuff, addr FindData.cFileName, eax
         INVOKE     SendMessage, hList, LB_ADDSTRING, 0, addr FindData.cFileName ;szBuff
            jmp     Getem

AllDone:
         INVOKE     FindClose, hFind

NoFile:

      .elseif wMsg == WM_DRAWITEM
         .if wParam == 85
               mov     esi, lParam
               lea     edi, dis
               mov     ecx, sizeof dis
               rep     movsb
            .if dis.itemID != -1
               INVOKE     SendMessage, hList, LB_GETTEXT, dis.itemID, addr szBuff
                  mov     Len, eax
               INVOKE     GetTextMetrics, dis.hdc, addr tm
                  mov     eax, dis.rcItem.bottom
                  add     eax, dis.rcItem.top
                  sub     eax, tm.tmHeight
                  shr     eax, 1
                  mov     Ypos, eax
;               .if (dis.itemState & ODS_FOCUS)
               .if (dis.itemState & ODS_SELECTED)
                  INVOKE     CreateSolidBrush, 00800000h      ; Highlight background color
                     mov     hBrush, eax
                  INVOKE     FillRect, dis.hdc, addr dis.rcItem, eax
                  INVOKE     SetTextColor, dis.hdc, 00ffffffh ; Highlight text color
                  INVOKE     SetBkColor, dis.hdc, 00800000h   ; Highlight text background color
               .else
                  INVOKE     CreateSolidBrush, 00ffffe0h      ; Background color
                     mov     hBrush, eax
                  INVOKE     FillRect, dis.hdc, addr dis.rcItem, eax
                  INVOKE     SetTextColor, dis.hdc, 000000ffh ; Text color - dis.itemData
                  INVOKE     SetBkColor, dis.hdc, 00ffffe0h   ; Text background color
               .endif        
                  add     dis.rcItem.left, 15
               INVOKE     TextOut, dis.hdc, dis.rcItem.left, Ypos, addr szBuff, Len
                  sub     dis.rcItem.left, 14
                  mov     dis.rcItem.right, 14
                  add     dis.rcItem.top, 1
                  sub     dis.rcItem.bottom, 1
               INVOKE     DeleteObject, hBrush
               INVOKE     CreateSolidBrush, 00000000h         ; Frame color
                  mov     hBrush, eax
               INVOKE     FrameRect, dis.hdc, addr dis.rcItem, eax
                  inc     dis.rcItem.left
                  inc     dis.rcItem.top
                  dec     dis.rcItem.right
                  dec     dis.rcItem.bottom
                  mov     ebx, dis.itemID
               INVOKE     SetBkColor, dis.hdc, 00ffff00h ;dword ptr Color01[ebx*4]
               INVOKE     DrawText, dis.hdc, addr szClear, 6, addr dis.rcItem, DT_LEFT or DT_SINGLELINE
               INVOKE     DeleteObject, hBrush
            .endif
               mov     dis.itemState, ODS_DEFAULT
               mov     dis.hdc, 0
         .endif

      .elseif wMsg == WM_COMMAND
         mov     eax, wParam
         mov     ebx, eax
         shr     ebx, 16

;---------- [Extract the selected item] ----------
         .if ebx == LBN_SELCHANGE
            INVOKE     SendMessage, hList, LB_GETCURSEL, 0, 0
            .if eax == LB_ERR
                  jmp     NoSel
            .endif
               mov     ebx, eax
            INVOKE     SendMessage, hList, LB_GETTEXT, ebx, addr szBuff
            INVOKE     MessageBox, NULL, addr szBuff, addr AppName, MB_OK
         .endif

NoSel:

