#Compile Exe
#Include "Win32api.inc"
%frmName_lstMembers             =    1500
%frmName_txtPerson              =    1505
%frmName_btnExecute             =    1510
%frmName_txtAge                 =    1515
%frmName_txtLastName            =    1520
%frmName_txtFirstName           =    1525
%frmName_lblMembers             =    1530
%frmName_lblAge                 =    1535
%frmName_lblLastName            =    1540
%frmName_lblFirstName           =    1545

Type WndEventArgs
  wParam As Long
  lParam As Long
  hWnd   As Dword
  hInst  As Dword
End Type
Declare Function FnPtr(wea As WndEventArgs) As Long

Type MessageHandler
  wMessage As Long
  dwFnPtr As Dword
End Type
Global frmName_MsgHdlr() As MessageHandler


Function strGetWindowText(hParent As Dword, iCtrlId As Long) As String
  Local pszStr As Asciiz Ptr
  Local hCtrl As Dword
  Local iLen As Long

  hCtrl=GetDlgItem(hParent,iCtrlId)           'Retrieve handle of control with GetDlgItem()
  iLen=GetWindowTextLength(hCtrl)             'Retrieve length of text in control
  Incr iLen                                   'add extra byte for null terminator
  pszStr=GlobalAlloc(%GPTR,iLen)              'allocate buffer from global memory
  Call GetWindowText(hCtrl,ByVal pszStr,iLen) 'bring text into buffer (pass address of buffer)
  strGetWindowText=@pszStr                    'assignment brings PB OLE String Engine into picture
  Call GlobalFree(pszStr)                     'release string buffer for GetWindowText()
End Function


Sub SendListBoxMessage(hParent As Dword, iCtrlId As Long, iMsg As Long, wParam As Dword, strText As String)
  Local hCtrl As Dword                                  'Get Control ID of ListBox (from CreateWindowEx())

  hCtrl=GetDlgItem(hParent,iCtrlId)                     'Retrieve handle of control with GetDlgItem()
  Call SendMessage(hCtrl,iMsg,wParam,Strptr(strText))   'Api SendMessage Function
End Sub


Sub frmName_Load(Wea As WndEventArgs)
  Call SendListBoxMessage(Wea.hWnd,%frmName_lstMembers,%LB_INSERTSTRING, 0,"Stan Helton")
  Call SendListBoxMessage(Wea.hWnd,%frmName_lstMembers,%LB_INSERTSTRING, 1,"Fred Harris")
  Call SendListBoxMessage(Wea.hWnd,%frmName_lstMembers,%LB_INSERTSTRING, 2,"Rodney Hicks")
  Call SendListBoxMessage(Wea.hWnd,%frmName_lstMembers,%LB_INSERTSTRING, 3,"Chris Holbrook")
End Sub


Function frmName_OnCreate(wea As WndEventArgs) As Long   'Here Is Your Form_Load()
  Local pCreateStruct As CREATESTRUCT Ptr
  Local hCtrl As Dword

  pCreateStruct=wea.lParam
  wea.hInst=@pCreateStruct.hInstance
  hCtrl=CreateWindowEx(512,"listbox","",1344274497,12,34,171,108,wea.hWnd,%frmName_lstMembers,wea.hInst,ByVal 0)
  hCtrl=CreateWindowEx(512,"edit","",1342177280,208,122,379,25,wea.hWnd,%frmName_txtPerson,wea.hInst,ByVal 0)
  hCtrl=CreateWindowEx(0,"button","Execute",1342193664,454,28,103,57,wea.hWnd,%frmName_btnExecute,wea.hInst,ByVal 0)
  hCtrl=CreateWindowEx(512,"edit","",1342177280,300,82,127,23,wea.hWnd,%frmName_txtAge,wea.hInst,ByVal 0)
  hCtrl=CreateWindowEx(512,"edit","",1342177280,300,45,127,23,wea.hWnd,%frmName_txtLastName,wea.hInst,ByVal 0)
  hCtrl=CreateWindowEx(512,"edit","",1342177280,300,10,127,23,wea.hWnd,%frmName_txtFirstName,wea.hInst,ByVal 0)
  hCtrl=CreateWindowEx(0,"static","VB Converter Members",1342177280,14,12,169,21,wea.hWnd,%frmName_lblMembers,wea.hInst,ByVal 0)
  hCtrl=CreateWindowEx(0,"static","Age",1342177280,210,82,73,25,wea.hWnd,%frmName_lblAge,wea.hInst,ByVal 0)
  hCtrl=CreateWindowEx(0,"static","Last Name",1342177280,210,44,71,23,wea.hWnd,%frmName_lblLastName,wea.hInst,ByVal 0)
  hCtrl=CreateWindowEx(0,"static","First Name",1342177280,210,10,73,21,wea.hWnd,%frmName_lblFirstName,wea.hInst,ByVal 0)
  Call frmName_Load(Wea)

  frmName_OnCreate=0
End Function


Sub frmName_btnExecute_Click(Wea As WndEventArgs)
  Local strtxtPersonText As String
  strtxtPersonText= _
  "Your Name Is " & _
  strGetWindowText(Wea.hWnd,%frmName_txtFirstName) & _
  " " & _
  strGetWindowText(Wea.hWnd,%frmName_txtLastName) & _
  " And You Are " & _
  strGetWindowText(Wea.hWnd,%frmName_txtAge) & _
  " Years Old."
  Call SetWindowText(GetDlgItem(Wea.hWnd,%frmName_txtPerson),Byval StrPtr(strtxtPersonText))
End Sub


Function frmName_OnCommand(wea As WndEventArgs) As Long
  Select Case As Long LoWrd(wea.wParam)
    Case %frmName_btnExecute
      Select Case As Long HiWrd(wea.wParam)
        Case %BN_CLICKED
          Call frmName_btnExecute_Click(wea)
      End Select
  End Select

  frmName_OnCommand=0
End Function


Function frmName_OnClose(wea As WndEventArgs) As Long
  Call PostQuitMessage(0)
  frmName_OnClose=0
End Function


Function frmName_WndProc(ByVal hWnd As Long,ByVal wMsg As Long,ByVal wParam As Long,ByVal lParam As Long) As Long
  Local wea As WndEventArgs
  Register iReturn As Long
  Register i As Long

  For i=0 To 2
    If wMsg=frmName_MsgHdlr(i).wMessage Then
       wea.hWnd=hWnd: wea.wParam=wParam: wea.lParam=lParam
       Call Dword frmName_MsgHdlr(i).dwFnPtr Using FnPtr(wea) To iReturn
       frmName_WndProc=iReturn
       Exit Function
    End If
  Next i

  frmName_WndProc=DefWindowProc(hWnd,wMsg,wParam,lParam)
End Function


Sub Attach_frmName_MsgHdlrs()
  Redim frmName_MsgHdlr(2) As MessageHandler  'Associate Windows Message With Message Handlers

  frmName_MsgHdlr(0).wMessage=%WM_CREATE                  : frmName_MsgHdlr(0).dwFnPtr=CodePtr(frmName_OnCreate)                  
  frmName_MsgHdlr(1).wMessage=%WM_COMMAND                 : frmName_MsgHdlr(1).dwFnPtr=CodePtr(frmName_OnCommand)                 
  frmName_MsgHdlr(2).wMessage=%WM_CLOSE                   : frmName_MsgHdlr(2).dwFnPtr=CodePtr(frmName_OnClose)                   
End Sub


Function WinMain(ByVal hIns As Long,ByVal hPrev As Long,ByVal lpCL As Asciiz Ptr,ByVal iShow As Long) As Long
  Local szAppName As Asciiz * 16
  Local winclass As WndClassEx
  Local hWnd As Dword
  Local Msg As tagMsg

  szAppName="frmName"
  Call Attach_frmName_MsgHdlrs()
  winclass.cbSize=SizeOf(winclass)
  winclass.style=%CS_HREDRAW Or %CS_VREDRAW
  winclass.lpfnWndProc=CodePtr(frmName_WndProc)
  winclass.cbClsExtra=0
  winclass.cbWndExtra=0
  winclass.hInstance=hIns
  winclass.hIcon=LoadIcon(%NULL, ByVal %IDI_APPLICATION)
  winclass.hCursor=LoadCursor(%NULL, ByVal %IDC_ARROW)
  winclass.hbrBackground=%COLOR_BTNFACE+1
  winclass.lpszMenuName=%NULL
  winclass.lpszClassName=VarPtr(szAppName)
  RegisterClassEx winclass
  hWnd=CreateWindowEx(0,szAppName,"Enter Your Name",%WS_OVERLAPPEDWINDOW,400,300,606,191,0,0,hIns,ByVal 0)
  Call ShowWindow(hWnd,iShow)
  While GetMessage(Msg,%NULL,0,0)
    TranslateMessage Msg
    DispatchMessage Msg
  Wend

  Function=msg.wParam
End Function
