Just a tip..for passing lists and other values to Callbacks
Posted: Thu Oct 27, 2011 7:41 pm
What I've taken to doing that helps when I need access to local Linked-Lists and other valued using WndProc() callbacks, is to wrap all my variables into a structure and use that. It's a pretty simple notion, but may help a lot of people (shrug). I just thought it was worthy of posting, because I just helped a friend with a problem of his this way...
It's just a handy style of programming in PB.
It's just a handy style of programming in PB.
Code: Select all
Structure CLASS_LIST
ListId.s
ClassName.s
EndStructure
Structure MAIN_VARIABLES
okToCloseWindow.b
myMiscValue.l
myButton1.l
myButton2.l
List myClasses.CLASS_LIST()
List ClassShortList.s()
List myIntegers.i()
EndStructure
Procedure WndProc( hWnd.l, uMsg.i, wParam.l, lParam.l )
Static *lpMv.MAIN_VARIABLES
Select uMsg
Case #WM_APP + 1234
*lpMv = lParam
ProcedureReturn 1
Case #WM_COMMAND
If wParam >> 16 = #BN_CLICKED
Select lParam
Case *lpMv\myButton1
Debug "Button 1 Clicked!"
ForEach *lpMv\myIntegers()
Debug *lpMv\myIntegers()
Next
Case *lpMv\myButton2
Debug "Button 2 Clicked!"
EndSelect
EndIf
Case #WM_MOUSEMOVE
x = lParam & $0000FFFF
y = (lParam >> 16)
If ((x < 10) And (y < 10))
*lpMv\okToCloseWindow = -1
EndIf
EndSelect
ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure
Procedure Main()
mv.MAIN_VARIABLES ;<-- Init our variable structure (OBJECT)
mv\okToCloseWindow = 0
For i = 1 To 10
AddElement(mv\myIntegers())
mv\myIntegers() = Random(2000000)
Next
hWnd = OpenWindow(1, 0, 0, 500, 500, "Test Window", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
myMsg.s = "Moving the mouse pointer To the upper-left hand corner of client area To close window."
MessageBox_(hWnd, @myMsg, @"Note", #MB_ICONINFORMATION|#MB_OK)
SetWindowCallback(@WndProc(), 1)
SendMessage_(hWnd, #WM_APP + 1234, 0, @mv) ;- Before we do anything, send a custom message to the WndProc()
; to let it know the pointer of our MAIN_VARIABLE
mv\myButton1 = GadgetID( ButtonGadget(#PB_Any, 20, 20, 100, 20, "Button 1") )
mv\myButton2 = GadgetID( ButtonGadget(#PB_Any, 20, 50, 100, 20, "Button 2") )
Repeat
event = WaitWindowEvent()
If event = #PB_Event_CloseWindow
mv\okToCloseWindow = -1
EndIf
Until mv\okToCloseWindow <> 0
CloseWindow(1)
ProcedureReturn 0
EndProcedure
Main()