;Win11b.ASM : Listbox /LBS_NOTIFY [Mouseclick Only] (October 7, 2023)
;============================================================================
 
%ifdef 
To assemble: nasm -f win32 ListBox1.asm -o ListBox1.obj
   To link : golink.exe /entry:start ListBox1.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'

;Macros 


                            [section .data]

;Messages
MsgAx0 db "AX = 0",0
MsgAx1 db "AX = 1",0
MsgAx2 db "AX = 2",0
MsgAx3 db "AX = 3",0

;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 "Listbox :Arrow Keys To Move - Double-Click To Select - LBS_NOTIFY",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, 600, 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
        jz MsgEnd
 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
		
        xor eax,eax
        cmp dword argv(uM),WM_DESTROY
        	jnz wpCommand?
		jmp wpExit

wpCommand?:	
        cmp dword argv(uM), WM_COMMAND
        	jz wpIsControl?
		jmp DefWndProc	;Didn't handle any msg. Return to DefWndProc

wpIsControl?:
        mov eax,dword argv(wP)
        mov edx,dword argv(lP)	;long param into eax
          cmp edx,0  ; lP <>0 =control handle
        jz wpIsKey?

;lParam =handle
          cmp ax,LISTBOX_ID
          	jz wpIsClick?
          jmp wpReturn
wpIsClick?:
          shr eax,10h
          cmp ax,LBN_DBLCLK
            jz near GetIndex
        jmp wpReturn
;lParam =0
wpIsKey?:
        cmp ax,1
          jz GetIndex
        cmp ax,2
          jz near wpExit          		 
		jmp wpReturn

IsDblClk?: 
        mov eax,dword argv(wP)
        shr eax,10h
        cmp ax,LBN_DBLCLK
			jz near GetIndex
		jmp wpReturn            			

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, [hWnd],Buf,MsgLBSel,MB_OK

;Restore focus to the listbox after each action

        invoke SetFocus, [hListBox] ;
		jmp wpReturn

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

wpReturn:
	xor eax,eax
    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_STANDARD,\
            10,10,498,198,\
                          dword argv(hParent),\
                         LISTBOX_ID,\
                 dword [hInstance],\
                          NULL

ret 4       ; adjust stack for hParent arg.

;LBS_STANDARD includes WS_BORDER, WS_VSCROLL, LBS_SORT, and LBS_NOTIFY
endproc ; CreateListBox

%ifdef

April 28, 2011
November 5, 2016
May 4, 2018
October 7, 2023
;mcamember

%endif