;Win11A.ASM : Listbox :LBS_WANTKEYBOARDINPUT  (May 6, 2018)
;============================================================================
 
%ifdef 
To assemble: nasm -f win32 Win11A.asm -o Win11A.obj
   To link : golink.exe /entry:start Win11A.obj kernel.dll user.dll
%endif 

;==========================================================================
;The path to these include files must be changed to match your machine.

%include '\nasm32\inc\win32\windows.inc'
%include '\nasm32\inc\win32\kernel32.inc'
%include '\nasm32\inc\win32\user32.inc'
%include '\nasm32\inc\nasm32.inc'
%include '\windows.app\tools\macros\WinTut.mac'


                            [section .data]

;Handles

hInstance dd 0
hWnd dd 0
hListBox dd 0

;Various labels

Buf times 100 db 0
ListElement1 db "Item 1",0
ListElement2 db "Item 2",0
ListElement3 db "Item 3",0
szClass db "Main Win",0		;
szTitle db "Win11A : Arrow Keys To Move, Enter Key To Select - LBS_WANTKEYBOARDINPUT",0  ;
szListClass db "LISTBOX",0		;Reserved Windows class name
szListTitle db "Listbox of 3 items",0	;The name displayed on the listbox
LISTBOX_ID equ 1234h	; The control id for the listbox. An arbitrary number.
MsgLBSel db "ListBox Selection",0

wcx times WNDCLASSEX_size db 0
msg times MSG_size db 0

                           [section .text]

start:

proc ListA
      invoke GetModuleHandleA, dword NULL
      mov [hInstance], eax
      invoke WinMain,dword [hInstance],dword NULL,dword NULL,dword SW_SHOWNORMAL
      invoke ExitProcess,dword NULL
      ret
endproc

;-----------------------------  WinMain  -------------------------------------

proc WinMain ,hInst,hPinst,CmdLn,dwShow

;Load WNDCLASSEX
    _LoadWCX
         
;Register the window
    invoke RegisterClassExA,dword wcx

;Create the main Window
    invoke CreateWindowExA,\
                    0,\
                    szClass,\
                    szTitle,\
          WS_OVERLAPPEDWINDOW +WS_VISIBLE ,\
            100, 120, 650, 300,\
                          NULL,\
                          NULL,\
                 dword [hInstance],\
                          NULL
     mov [hWnd], eax

;Create the LIST control.
     invoke CreateListBox, eax  ;
     mov dword [hListBox],eax	;

;Assume there was no error
;Put keyboard focus to the LIST control.
     invoke SetFocus, eax

;Use the addstring macro to populate the list box
     _Addstring ListElement1
     _Addstring ListElement2
     _Addstring ListElement3

;Set highlight to first element
     invoke SendMessageA,dword [hListBox],LB_SETCURSEL,0,0

;Process Windows Messages
MsgGet:
    invoke GetMessageA, dword msg, dword NULL, dword NULL, dword NULL
    cmp eax,0
        jnz MsgProcess
    jmp MsgEnd

;ListBox control doesn't require IsDialogMessage
MsgProcess:
      invoke TranslateMessage, dword msg
      invoke DispatchMessageA, dword msg
      jmp MsgGet
MsgEnd:
         mov eax,dword [msg +MSG.wParam]
   ret
endproc ;WinMain

;-------------------------------- CALLBACK ---------------------------------

proc WndProc, hW, uM, wP, lP	
        cmp dword argv(uM),WM_DESTROY
        	jnz IsVKey?
		jmp wpExit

IsVKey?:
        cmp dword argv(uM), WM_VKEYTOITEM
           jz VKey
        jmp DefWndProc

VKey:
        mov eax,dword argv(wP)   
        cmp ax,VK_RETURN
          jz near GetIndex
        cmp ax,VK_ESCAPE
          jz near Escape
        mov eax, -1 
        ret

GetIndex:		
        invoke SendMessageA,dword [hListBox],LB_GETCURSEL,NULL,NULL
		        
;Get the data at this caret position in the listbox.
        invoke SendMessageA,[hListBox],LB_GETTEXT,eax,Buf

;Assume there was no error.
;Display the list entry.
        invoke MessageBoxA,dword argv(hW),Buf,MsgLBSel,MB_OK

;Restore focus to the listbox after each action
        invoke SetFocus, [hListBox] ;
        mov eax, -1
        ret       

Escape:
		invoke SendMessageA,dword argv(hW),WM_CLOSE,NULL,NULL
        mov eax, -1
        ret

DefWndProc:
    invoke DefWindowProcA, dword argv(hW), dword argv(uM),\
                dword argv(wP), dword argv(lP)
    ret

;Exit Function
wpExit:
    invoke PostQuitMessage, dword NULL
    xor eax,eax
    ret
endproc ;WndProc


;----------------------------------------------------------------------
;Create The LIST Control

proc CreateListBox, hParent
    invoke CreateWindowExA,\
                    NULL,\
                    szListClass,\
                    szListTitle,\
	WS_CHILD +WS_VISIBLE +LBS_WANTKEYBOARDINPUT,\
            10,30,200,100,\
                          dword argv(hParent),\
                         LISTBOX_ID,\
                 dword [hInstance],\
                          NULL

ret 4       ; adjust stack for hParent arg.

endproc ; CreateListBox



;May 6, 2018
;mcamember