Buddy Windows
Posted: Mon Dec 31, 2007 6:01 am
Have a play with this code. The windows start out stuck together, and will stay that way until you move the child away. After that, either window will move anywhere independently of the other until they are moved to within 30px of their original "stuck" position, at which time they will rejoin and stick together until the child is again moved away. It's kinda cool imho:
Code: Select all
;=======================================================
; Program: Buddy Windows
; Author: Lloyd Gallant (netmaestro)
; Date: December 31, 2007
; Target OS: Microsoft Windows All
; Target Compiler: PureBasic 4.0 and later
; License: Free, unrestricted, no warranty
;=======================================================
Global stuck=1, tid
Procedure ReleaseWindow(void)
Repeat
Delay(1)
Until GetAsyncKeyState_(#VK_LBUTTON) & 32768 = 0
stuck = #True
EndProcedure
Procedure WinProc(hwnd, msg, wparam, lparam)
result = #PB_ProcessPureBasicEvents
Select msg
Case #WM_MOVING
Select hwnd
Case WindowID(0)
*view.RECT = lparam
curwidth = *view\right - *view\left
curheight = *view\bottom - *view\top
GetWindowRect_(WindowID(1), @cr.RECT)
xx = cr\left : yy = cr\top-curheight
If stuck
childwidth = cr\right - cr\left
childheight = cr\bottom - cr\top
MoveWindow_(WindowID(1),*view\left,*view\bottom+1,childwidth,childheight,#True)
Else
If Abs(*view\top-yy) <30 And Abs(xx-*view\left)<30 ; within the grab zone
With *view
\left = xx
\top = yy
\right = xx+curwidth
\bottom = yy+curheight
EndWith
If Not IsThread(tid)
tid = CreateThread(@ReleaseWindow(),0)
EndIf
result = #True
EndIf
EndIf
Case WindowID(1)
*view.RECT = lparam
curwidth = *view\right - *view\left
curheight = *view\bottom - *view\top
GetWindowRect_(WindowID(0), @pr.RECT)
xx = pr\left : yy = pr\bottom+1
If Abs(*view\top-yy) <30 And Abs(xx-*view\left)<30
If Not stuck
With *view
\left = xx
\top = yy
\right = xx+curwidth
\bottom = yy+curheight
EndWith
If Not IsThread(tid)
tid = CreateThread(@ReleaseWindow(),0)
EndIf
result = #True
EndIf
Else
stuck=#False
EndIf
EndSelect
EndSelect
ProcedureReturn result
EndProcedure
OpenWindow(0, 0, 0, 320, 240, "Main Window", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
GetWindowRect_(WindowID(0), wr.RECT)
OpenWindow(1,wr\left,wr\bottom+1,320,160,"Child Window",#PB_Window_TitleBar,WindowID(0))
SetWindowCallback(@WinProc())
SetActiveWindow(0)
Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow