frmListbox.inc 'Include for ShowData.bas that has code pertaining to the Form/Dialog/Window with a listbox on it.
'**************************
'File Name: frmListBox.inc
'**************************
'******************************************************************************************
'Function Name: frmListBox_OnCreate Handles %WM_CREATE message for frmListBox
'******************************************************************************************
'******************************************************************************************
'Before the CreateWindow() call back in frmMainWindow_OnCommand() that creates this form
'returns, this procedure here will be called as a result of this window/form receiving a
'%WM_CREATE message. Within this procedure itself are also two CreateWindow() call. The
'first is to create the listbox that covers most of the form, and the second is for the
'button window along the bottom of the form the clicking of which will cause the listbox to
'fill with data. In order for the listbox on this form to respond to double click messages
'it was necessary to add the %LBS_NOTIFY style bit to its variable holding its styles.
'******************************************************************************************
Function frmListBox_OnCreate(wea As WndEventArgs) As Long
Local hLBox As Dword, dwStyle As Dword, hBut As Dword, hIns As Dword
Local pCreateStruct As CREATESTRUCT Ptr
Local szStr As Asciiz*64
Local rc As RECT
pCreateStruct=wea.lParam
hIns=@pCreateStruct.hInstance
dwStyle = %LBS_HASSTRINGS Or %WS_CHILD Or %WS_VISIBLE Or %WS_VSCROLL Or %LBS_NOTIFY
Call GetClientRect(wea.hWnd,rc)
hLBox=CreateWindow("listbox","",dwStyle,0,0,rc.nRight,rc.nBottom-40,wea.hWnd,%IDC_LIST_BOX,hIns,0)
szStr="Click This Button To Load Data Into Listbox"
dwStyle=%WS_CHILD Or %WS_VISIBLE
hBut=CreateWindow("button",szStr,dwStyle,75,rc.nBottom-35,360,25,wea.hWnd,%IDC_LB_LOAD,hIns,0)
frmListBox_OnCreate=0
End Function
'*********************************************************************************************
'Function: LoadListBox(hListBox As Dword) Loads hListBox with some useless example strings
'*********************************************************************************************
Sub LoadListBox(hListBox As Dword)
Local strText As String
Register i As Long
For i=1 To 100
strText=Str$(i) & " Simple Demo Showing How To Display And Scroll Text In List Box"
Call SendMessage(hListBox, %LB_ADDSTRING, 0, Strptr(strText))
Next i
End Sub
'*********************************************************************************************
'Function Name: frmListBox_OnCommand() Handles WM_COMMAND Message For frmListBox Window/Form
'*********************************************************************************************
'*********************************************************************************************
'%WM_COMMAND messages can result from quite a few sources. Select Case logic in this procedure
'is only interested in two of them, however. The first is when the button along the bottom of
'the form is clicked, and the second is when an item in the listbox is double clicked. This
'latter case is the more interesting of the two in that two seperate bits of data are packaged
'within the wea.wParam variable. The low order word of wea.wParam contains the control id of
'the control to which the message applies, and the high order word contains the particular
'listbox message being sent. If the control id is %IDC_LIST_BOX and the message is %LBN_DBLCLK
'then a message is sent to the listbox asking for the zero based index of the item within the
'list box that was double clicked. Did you get that? This is I believe the first time in these
'six or seven apps in this tutorial that we sent a message anywhere. So far, we have only been
'at the receiving end of this constant messageing business. But to be absolutely clear, that
'listbox is on this form, but to communicate with it we send it a message. The return value of
'that particular SendMessage() call with that particular configuration of message arguements is
'the zero based index we want (the line double clicked). Armed with the index of the line
'double clicked, we can send another message to the listbox requesting return of the line of
'text corresponding to the index of the item double clicked. Notice that the arguements of this
'latter SendMessage() call are different than for the first. This will universally be the case
'because SendMessage() is a very powerful and adaptable function where the arguements depend on
'the type of control the message is being sent to, and the response or functionality being
'requested.
'
'Perhaps we should take a look at SendMessage()'s parameters, one by one. The first parameter
'is the handle of the control. Do you remember where handles of controls come from? Since we're
'talking about this particular list box - the only list box on this form, we only need to look
'as far as frmListBox_OnCreate() just above to see the handle of the listbox was returned by the
'CreateWindow() call that created the listbox. But, in exmining frmListBox_OnCreate() have you
'picked up on the fact that hLBox is declared as a local Dword variable in that procedure? What
'that means to us here in this procedure is that it is of absolutely no good to us anymore. That
'variable and whatever value it had ceased to exist when that procedure exited.
'*********************************************************************************************
Function frmListBox_OnCommand(wea As WndEventArgs) As Long
Local iIndex As Dword,hLBox As Dword
Local szBuffer As Asciiz*64
hLBox=GetDlgItem(wea.hWnd,%IDC_LIST_BOX)
Select Case Lowrd(wea.wParam)
Case %IDC_LB_LOAD
Call LoadListBox(hLBox)
MsgBox("Double Click A Line To Extract From List Box!")
Case %IDC_LIST_BOX
If Hiwrd(wea.wParam)=%LBN_DBLCLK Then
iIndex=SendMessage(hLBox,%LB_GETCURSEL,0,0)
Call SendMessage(hLBox,%LB_GETTEXT,iIndex,Varptr(szBuffer))
MsgBox szBuffer,%MB_ICONINFORMATION, _
"See fnListBoxDisplay_OnCommand() For Extracting Strings From Listbox"
End If
End Select
frmListBox_OnCommand=0
End Function
'**********************************************************************************************
'Function Name: frmListBox_OnDestroy Handles WM_DESTROY Message For frmListBox Window
'**********************************************************************************************
Function frmListBox_OnDestroy(wea As WndEventArgs) As Long
Call ShowWindow(wea.hWnd, %SW_HIDE)
Call ShowWindow(FindWindowEx(ByVal 0,ByVal 0,"frmMainWindow","ShowData"),%SW_SHOWNORMAL)
frmListBox_OnDestroy=0
End Function
'******************************************************************************************
'Function Name: frmListBox Window Procedure For Windows of Class frmListBox
'******************************************************************************************
'******************************************************************************************
'Back in frmMainWindow, when the user clicks the second button, "View Strings Looped Into
'Listbox", frmMainWindow_OnCommand() is called with the wea.wParam variable containing in its
'lower two bytes the control id of the second button, i.e., %IDC_BUTTON_2=1202. Within that
'procedure is the CreateWindow() call that brings this form into existance. Thereafter, all
'messages pertaining to this form or any of its child controls will pass through this
'procedure
'******************************************************************************************
Function frmListBox(ByVal hWnd As Long,ByVal wMsg As Long,ByVal wParam As Long,ByVal lParam As Long) As Long
Local wea As WndEventArgs
Select Case wMsg
Case %WM_CREATE
wea.hWnd=hWnd : wea.wParam=wParam : wea.lParam=lParam
frmListBox=frmListBox_OnCreate(wea)
Exit Function
Case %WM_COMMAND
wea.hWnd=hWnd : wea.wParam=wParam : wea.lParam=lParam
frmListBox=frmListBox_OnCommand(wea)
Exit Function
Case %WM_DESTROY
wea.hWnd=hWnd : wea.wParam=wParam : wea.lParam=lParam
frmListBox=frmListBox_OnDestroy(wea)
Exit Function
End Select
frmListBox=DefWindowProc(hWnd,wMsg,wParam,lParam)
End Function