Hi Danilo; great code, as always. I've noticed that there doesn't seem to be any OSX examples on the forum that demonstrate such moving by dragging only designated areas of the window. This example illustrates how to handle such moves for both Windows and OSX:
Code: Select all
;==================================================
;
; Moving a window by clicking on the background
; or on any designated part of it
;
; for Windows and OSX
;
; by TI-994A - 26th October 2013
;
;==================================================
EnableExplicit
#NSMouseMoved = 5
Enumeration
#MainWindow
#Button1
#Button2
EndEnumeration
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
Structure RECT
left.i
top.i
right.i
bottom.i
EndStructure
CompilerEndIf
Define appQuit, xDraw, useHotspot, hotSpot.RECT, stateText.s
With hotSpot
\left = 0
\right = 400
\top = 30
\bottom = 70
EndWith
Procedure inHotSpot(*hotSpot.RECT)
Define result
With *hotSpot
If WindowMouseX(#MainWindow) > \left And
WindowMouseX(#MainWindow) < \right And
WindowMouseY(#MainWindow) > \top And
WindowMouseY(#MainWindow) < \bottom
result = 1
EndIf
EndWith
ProcedureReturn result
EndProcedure
Macro drawTitle()
StartDrawing(WindowOutput(#MainWindow))
Box(hotSpot\left, hotSpot\top, hotSpot\right - hotSpot\left,
hotSpot\bottom - hotSpot\top, RGB(0, 0, 150))
xDraw = (400 - TextWidth(stateText)) / 2
DrawText(xDraw, 40, stateText, RGB(255, 255, 255), RGB(0, 0, 150))
StopDrawing()
EndMacro
Macro moveMode()
If useHotspot
useHotspot = 0
SetGadgetText(#Button1, "change to specific area")
stateText = "click anywhere on this window to move it"
drawTitle()
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
CocoaMessage(0, WindowID(#MainWindow), "setMovableByWindowBackground:", #YES)
CompilerEndIf
Else
useHotspot = 1
SetGadgetText(#Button1, "change to full window")
stateText = "click anywhere on this bar to move the window"
drawTitle()
EndIf
EndMacro
OpenWindow(#MainWindow, 0, 0, 400, 300, "Move Borderless Window",
#PB_Window_BorderLess | #PB_Window_ScreenCentered)
ButtonGadget(#Button1, 10, 260, 180, 30, "change to specific area")
ButtonGadget(#Button2, 210, 260, 180, 30, "q u i t")
SetWindowColor(#MainWindow, RGB(150, 150, 250))
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
Define cocoaEvent, cocoaEventType
Define sharedApp = CocoaMessage(0, 0, "NSApplication sharedApplication")
CocoaMessage(0, WindowID(#MainWindow), "setMovableByWindowBackground:", #YES)
CocoaMessage(0, WindowID(#MainWindow), "setAcceptsMouseMovedEvents:", #YES)
MessageRequester("Borderless Window", "moving a window without a title bar")
CompilerEndIf
stateText = "click anywhere on this window to move it"
drawTitle()
Repeat
Select WaitWindowEvent()
Case #PB_Event_Gadget
Select EventGadget()
Case #Button1
moveMode()
Case #Button2
appQuit = 1
EndSelect
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
Case #WM_PAINT
drawTitle()
Case #WM_LBUTTONDOWN
If (useHotspot And inHotSpot(@hotSpot)) Or Not useHotspot
SendMessage_(WindowID(#MainWindow), #WM_NCLBUTTONDOWN, #HTCAPTION, 0)
EndIf
CompilerEndIf
EndSelect
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
cocoaEvent = CocoaMessage(0, sharedApp, "currentEvent")
If cocoaEvent
cocoaEventType = CocoaMessage(0, cocoaEvent, "type")
If cocoaEventType = #NSMouseMoved And useHotspot
If inHotSpot(@hotSpot)
CocoaMessage(0, WindowID(#MainWindow),
"setMovableByWindowBackground:", #YES)
Else
CocoaMessage(0, WindowID(#MainWindow),
"setMovableByWindowBackground:", #NO)
EndIf
EndIf
EndIf
CompilerEndIf
Until appQuit
The mechanics of the mouse events itself is quite short, and the rest is just the demonstration code.