' DDT GUI + WinAPI Interop Demo
' Shows how DIALOG/CONTROL handles flow into WinAPI calls

#COMPILE EXE
#DIM ALL

' WinAPI declarations for interop
DECLARE FUNCTION MessageBox LIB "user32.dll" ALIAS "MessageBoxA" ( _
    BYVAL hWnd AS DWORD, BYREF lpText AS STRING, _
    BYREF lpCaption AS STRING, BYVAL uType AS DWORD) AS LONG

DECLARE FUNCTION SetWindowText LIB "user32.dll" ALIAS "SetWindowTextA" ( _
    BYVAL hWnd AS DWORD, BYREF lpText AS STRING) AS LONG

DECLARE FUNCTION SendMessage LIB "user32.dll" ALIAS "SendMessageA" ( _
    BYVAL hWnd AS DWORD, BYVAL Msg AS DWORD, _
    BYVAL wParam AS DWORD, BYVAL lParam AS DWORD) AS DWORD

%WM_SETTEXT = &H000C
%MB_OK = &H00000000

FUNCTION PBMAIN() AS LONG
    LOCAL hDlg AS DWORD      ' Dialog handle - WinAPI HWND
    LOCAL hBtn AS DWORD      ' Button handle - WinAPI HWND
    LOCAL hTxt AS DWORD      ' Textbox handle - WinAPI HWND
    LOCAL hList AS DWORD     ' Listbox handle - WinAPI HWND
    LOCAL result AS LONG

    ' Create dialog - hDlg gets the HWND
    DIALOG NEW 0, "DDT + WinAPI Demo", 100, 100, 400, 350 TO hDlg

    ' Create controls - each TO var gets the control HWND
    CONTROL ADD BUTTON, hDlg, 101, "Click Me", 10, 10, 100, 30 TO hBtn
    CONTROL ADD TEXTBOX, hDlg, 102, "Edit me", 10, 50, 300, 25 TO hTxt
    CONTROL ADD LABEL, hDlg, -1, "Controls below use WinAPI handles:", 10, 90, 300, 20
    CONTROL ADD LISTBOX, hDlg, 103, "", 10, 120, 300, 80 TO hList
    CONTROL ADD CHECKBOX, hDlg, 104, "Enable Option", 10, 215, 200, 25
    CONTROL ADD COMBOBOX, hDlg, 105, "", 10, 250, 300, 80

    ' WinAPI interop: use handles directly with DECLARE'd APIs
    ' SetWindowText on textbox using its HWND
    SetWindowText(hTxt, "Set via WinAPI!")

    ' SendMessage to listbox to add items
    LOCAL i AS LONG
    FOR i = 1 TO 5
        SendMessage(hList, &H0180, 0, 0)  ' LB_ADDSTRING would need text ptr
    NEXT i

    ' Show the dialog with message loop
    DIALOG SHOW MODAL hDlg TO result

    ' Cleanup
    DIALOG END hDlg
    FUNCTION = result
END FUNCTION
