Share My Creation Simple drag pane window

https://www.dropbox.com/s/m98yhbotcejpixl/firstwindow.rar?dl=0

I am working on a project and I felt like sharing some simple code


moving a pane is actually harder then moving an image view so the code to mouve
a image view is even easier

As it is a simple pane you can easily manipulate:
position
height
animation
dock on the frame margins

B4X:
#Region  Project Attributes
    #MainFormWidth: 800
    #MainFormHeight: 600
  
#End Region

Sub Process_Globals
   Private fx As JFX
   Private MainForm As Form

   Dim c As Canvas
    Dim window As Pane

Dim x0,y0 As Int
End Sub

Sub AppStart (Form1 As Form, Args() As String)
   MainForm = Form1
   MainForm.Show
c.Initialize("")

  build_controls_palete

 
End Sub
Sub build_controls_palete

    window.Initialize("window")
      MainForm.RootPane.AddNode(window,100,100,200dip,400dip)
    'window.Style="-fx-background-color: #609c60ff;"


    Dim win As ImageView
    win.Initialize("")
    window.AddNode(win,2dip,2dip,window.PrefWidth-4dip,window.PrefHeight-4dip)

  
'draw a frame
    Dim temp As Image
    temp=pipe.frame(c,win,fx.Colors.RGB(200,245,200))
    win.SetImage(temp)
  
'add a button to window  
    Dim wclose As ImageView
    wclose.Initialize("wclose")
    window.AddNode(wclose,window.PrefWidth-30dip,5dip,25dip,25dip)
    wclose.SetImage(fx.LoadImage(File.DirAssets,"wclose.png"))


End Sub
Sub wclose_MouseClicked (EventData As MouseEvent)


window.RemoveAllNodes
window.RemoveNodeFromParent

End Sub

Sub window_MouseDragged (EventData As MouseEvent)

Dim x,y As Int
Dim jo As JavaObject = EventData
  x = jo.RunMethod("getScreenX", Null)
  y = jo.RunMethod("getScreenY", Null)
  
'move window or obj
        window.Left=x-x0
        window.top=y-y0
      
End Sub
Sub window_MousePressed (EventData As MouseEvent)

'get the reference position
    x0=MainForm.WindowLeft+10dip+EventData.x
    y0=MainForm.WindowTop+30dip+EventData.y
  
'this is when dealing with imageviews
'x0=eventdata.x
'y0=eventdata.y
    '30dip,10dip estimated mainform window frame
  
End Sub

usefull in case you want to resize window and maintain the frame size

a code module: pipe
B4X:
Sub frame(c As Canvas,clone As ImageView,color As Paint) As Image
''set the correct canvas dimension
c.Width=clone.Width
c.Height=clone.Height

' draw shape
Dim w,h,gros As Int
gros=5dip

w=c.Width
h=c.Height

'clear canvas
c.ClearRect(0,0,c.Width,c.Height)

'draw a color rectangular
c.DrawRect(0,0,c.Width,c.Height,fx.Colors.RGB(96,156,96),True,0)


'draw the frame shape
c.DrawRect(0,0,c.Width,30dip,color,True,0)
c.DrawRect(w-gros,0,gros,h,color,True,0)
c.DrawRect(0,h-gros,c.Width,gros,color,True,0)
c.DrawRect(0,0,gros,h,color,True,0)


Return c.Snapshot

End Sub
 
Last edited:
Top