Help with WindowCallback

Just starting out? Need help? Post your questions and find answers here.
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Help with WindowCallback

Post by NoahPhense »

I've never used one, but I see them all over and would like to learn how
to use them. I'd like to use them in the form of a procedure as such:

Code: Select all

Procedure WindowCallback(hWnd, Message, wParam, lParam)
Kale has a beautiful 'all api' example here, but I'm just looking for the
regular (pb) way first.. then maybe later, I'll kick the api'way around a
bit. ;)

viewtopic.php?t=11535

Any information is appreciated, a simple looking example would be great.

- np
Dr_Pixel
User
User
Posts: 36
Joined: Fri Oct 24, 2003 1:36 pm

Post by Dr_Pixel »

Here is a very simple example - I use the callback only to prevent my window from being resized too small.

All other events are processed in the main loop, in the standard PB way.

Code: Select all

Global MinWinX,MinWinY

MinWinX=300  ;minimum desired width
MinWinY=300  ;minimum desired height

Procedure WindowCallback(WindowID, Message, wParam, lParam)
  Result = #PB_ProcessPureBasicEvents 
  Select Message
    Case #WM_GETMINMAXINFO  ;window is about to be resized
      Result = 0 
      *ptr.MINMAXINFO       = lParam 
      *ptr\ptMinTrackSize\x = MinWinX 
      *ptr\ptMinTrackSize\y = MinWinY 
    
  EndSelect 
  ProcedureReturn Result 
EndProcedure

;======================== TEST ============================

hWnd=OpenWindow(0,0,0,550,550,#PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_ScreenCentered," Resize Me")

SetWindowCallback(@WindowCallback()) 


Repeat
  ev=WaitWindowEvent()
  
  ;regular window events would be processed here
  
Until ev=#PB_Event_CloseWindow

End
[/code]
Dr Pixel
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

You might find this one helpfull..
viewtopic.php?p=39654#39654

Timo
quidquid Latine dictum sit altum videtur
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

..

Post by NoahPhense »

Hey guys, thanks for the great responses!

Code: Select all

; So -- this is what people are talking about
; when they refer to high/low bits?
; 
; Forgive my ignorance. I shall continue.
;
; lParam = 32bits
; 
; 11110000111100001010101010101010 
;                 ^ 
;                 |beginning of high bits? 
; 
; By using "Bitwise AND" &, I can use the
; actual binary Or hex values. Then X becomes
; the value of the low bits? 
; 
; And by using the >> (shift right) 16.. we're
; just knocking off the first 16bits and utlizing
; the last of the 32bits And making it Y? 
;
X = lParam & $FFFF 
;X = lParam & %1111111111111111 
;X = WindowX() 
;      
Y = lParam >> 16 
;Y = WindowY()
*Most excellent. See, from one good example, I learned how to use
callbacks, and I think I learned two new operators.

- np
Last edited by NoahPhense on Sun Jul 18, 2004 7:19 pm, edited 1 time in total.
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

..

Post by NoahPhense »

Thanks guys, I think I'm getting the hang of it..

Code: Select all

; // declare,define,enumarate // ------------------------------------ //
Declare Open_Window_0()
Declare JumpLocations()
Declare MyWindowCallback(WindowID, Message, wParam, lParam)

Enumeration:#Window_0:EndEnumeration
Enumeration:#Button_0:EndEnumeration
Enumeration:#StatusBar_0:EndEnumeration
Enumeration:#Image0:EndEnumeration

Global hStatusBar

Quit.b = 0

Dim SB_Fields(2)

Image0 = CatchImage(0, ?Image0)

; // .begin // ------------------------------------------------------ //
Open_Window_0()

SetWindowCallback(@MyWindowCallback()) 
StatusBarIcon(#StatusBar_0, 0, Image0)
StatusBarIcon(#StatusBar_0, 1, Image0)
StatusBarText(#StatusBar_0, 0, "SIZE:")
StatusBarText(#StatusBar_0, 1, "LOCATION:")

Repeat
  Event = WaitWindowEvent()
  If Event = #PB_EventGadget
    GadgetID = EventGadgetID()
    If GadgetID = #Button_0
      If JumpLocations()
        ; ok
      EndIf
    EndIf
  EndIf
Until Event = #PB_EventCloseWindow And Quit = 1

FreeStatusBar(#StatusBar_0)

End

; // MyWindowCallback // -------------------------------------------- //
Procedure MyWindowCallback(WindowID, Message, wParam, lParam) 
  Result = #PB_ProcessPureBasicEvents 
  Select WindowID
    ; Window_0
    Case WindowID(#Window_0)
      Select Message
        Case #WM_MOVE 
          X = lParam & $FFFF 
          Y = lParam >> 16
          If X > 60000
            EnableWindow_(WindowID(#Window_0),#False)
            MessageBox_(0,"I'm going back..","tsk tsk",#mb_systemmodal)
            If JumpLocations()
            EndIf
            EnableWindow_(WindowID(#Window_0),#True)
          EndIf
          StatusBarText(#StatusBar_0, 1, "LOCATION: X: "+Str(X)+" Y: "+Str(Y))
          Result = 0
        Case #WM_SIZE
          X = lParam & $FFFF 
          Y = lParam >> 16
          ww.f = WindowWidth()
          SB_Fields(0) = ((ww*50.00)/100.00)
          SB_Fields(1) = ww
          SendMessage_(hStatusBar,#SB_SETPARTS,2,@SB_Fields()) 
          ResizeGadget(#Button_0,-1,-1,X/2, Y/2)
          StatusBarText(#StatusBar_0,0, "SIZE X: " + Str(WindowWidth()) + " Y: " + Str(WindowHeight()))
          Result = 0
        Case #WM_GETMINMAXINFO  ;window is about to be resized
          *ptr.MINMAXINFO       = lParam 
          *ptr\ptMinTrackSize\x = 408
          *ptr\ptMinTrackSize\y = 227
          Result = 0
        Case #WM_CLOSE
          Quit = 1
          End
      EndSelect
   EndSelect
  ProcedureReturn Result 
EndProcedure 

; // Open_Window_0 // ----------------------------------------------- //
Procedure Open_Window_0()
  If OpenWindow(#Window_0, 0, 0, 400, 200,  #PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar , "My First CallBack")
    hStatusBar = CreateStatusBar(#StatusBar_0, WindowID())
    If hStatusBar
      ; ok
    EndIf
    If CreateGadgetList(WindowID())
      ButtonGadget(#Button_0, 5,5, WindowWidth()/2, WindowHeight()/2, "PureBasic")
    EndIf
  EndIf
  AddStatusBarField(WindowWidth()/2)
  AddStatusBarField(WindowWidth()/2)
EndProcedure

;// ok, now im just being silly // ---------------------------------- //
Procedure.b JumpLocations()
  SW = (GetSystemMetrics_(#SM_CXSCREEN))
  SH = (GetSystemMetrics_(#SM_CYSCREEN))
  If SetWindowPos_(WindowID(#Window_0), #HWND_TOP, SW/4, SH/4, SW/2, SH/2, #SWP_SHOWWINDOW)
    rtn = 1
  Else
    rtn = 0
  EndIf
  ProcedureReturn rtn
EndProcedure

; // DataSection // ------------------------------------------------- //
DataSection
Image0:
  IncludeBinary "C:\Program Files\NSIS\Contrib\Graphics\Icons\box-install.ico"
EndDataSection
- np
Post Reply