B4J Question [Solved] Move a form on screen, better way ?

Laurent95

Active Member
Licensed User
Longtime User
Hello,

I need a bit help, if someone have a better way to move the form and can share it that would be nice.

The goal is to have under B4J a form like a Widget on B4A, this is solved in the example project attached.
But to move the form smoothly i need, if it's posible, to have another way, why ? :
- In the example with the right click it move but it's saccaded
- With the middle button it's best but it's very long, i increment movement by 1 pixel each time.

So if someone have another idea, that's welcome, thank you.
And i'm quite sure that my test code is maybe not the best way.
Sorry if there is a response to this problem, searched a lot but i didn't found any response.

Thank you, best regards.

Edit deleted the zip file: see last reply, the most smoothly code given by Stevel05 is updated in this version
 
Last edited:

Laurent95

Active Member
Licensed User
Longtime User
Hi,
Thank you i'll test it.

EDIT:
@Erel
Perfect, move smoothly 👍
But totally unable to limit the form to go off the screen. Tests are too bigs, it make motion run slower :eek:
Anyway, it's better than my solutions, thank you. And the user himself would be responsible for moving the form to the correct position 🤣.

If someone is interested.
Version 2 attached: right click with Erel's code, middle click to compare with my less smoothy code, too slow.

Nice evening all


Edit deleted the zip file: see last reply, the most smoothly code given by Stevel05 is updated in this version.
 
Last edited:
Upvote 0

Laurent95

Active Member
Licensed User
Longtime User
Impossible. Test it in release mode.
Hello,

Indeed it's better in release mode, anyway the form move smoothly and it's what i want.
Just still have some time the form who stop before the limits of the screen. But i can't find another way than in the project attached.
I put a Sleep(0) but not sure that change something, maybe it's my old Laptop who's slow also.

At least it's smooth and works ok most of time, thank you for the help.
Nice afternoon to all


Edit deleted the zip file: see last reply, the most smoothly code given by Stevel05 is updated in this version.
 
Last edited:
Upvote 0

stevel05

Expert
Licensed User
Longtime User
It works s expected here. The way it is written, if any of the tests fail then the form will stop. So if you are trying to get the form into the top left corner and the x value is 0 it won't let you drag it up any further .
 
Last edited:
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Try this instead:
B4X:
Private Sub imgIcon_MouseDragged (EventData As MouseEvent)
    If (rightBtnPressed) Then
        Dim jo As JavaObject = EventData
        MoveX = jo.RunMethod("getScreenX", Null) - oldX
        MoveY = jo.RunMethod("getScreenY", Null) - oldY
        MainForm.WindowLeft = Max(0,Min(fx.PrimaryScreen.MaxX - MainForm.WindowWidth,jo.RunMethod("getScreenX", Null) - oldX))
        MainForm.WindowTop = Max(0,Min(fx.PrimaryScreen.MaxY - MainForm.Windowheight,jo.RunMethod("getScreenY", Null) - oldY))
'        If ((MoveX>0) And (MoveY>0) And _
'            ((MoveX+MainForm.Width)<fx.PrimaryScreen.MaxX) And _
'            ((MoveY+MainForm.Height)<fx.PrimaryScreen.MaxY)) Then
'            MainForm.WindowLeft = jo.RunMethod("getScreenX", Null) - oldX
'            MainForm.WindowTop = jo.RunMethod("getScreenY", Null) - oldY
'        End If
 '       Sleep(0)
    End If
End Sub
#End Region
 
Upvote 0

Laurent95

Active Member
Licensed User
Longtime User
Try this instead:
B4X:
Private Sub imgIcon_MouseDragged (EventData As MouseEvent)
    If (rightBtnPressed) Then
        Dim jo As JavaObject = EventData
        MoveX = jo.RunMethod("getScreenX", Null) - oldX
        MoveY = jo.RunMethod("getScreenY", Null) - oldY
        [B]MainForm.WindowLeft = Max(0,Min(fx.PrimaryScreen.MaxX - MainForm.WindowWidth,jo.RunMethod("getScreenX", Null) - oldX))[/B]
        [B]MainForm.WindowTop = Max(0,Min(fx.PrimaryScreen.MaxY - MainForm.Windowheight,jo.RunMethod("getScreenY", Null) - oldY))[/B]
       ' Perfect that's the trick !!!
    End If
End Sub
#End Region
Hi stevel05,

Works perfect, even on my 17 years old Asus Laptop 👏
Thank you for your improvement, very performant, no more latence even it's only a dual core 👍

Have nice aftenoon,
Laurent
 
Upvote 0

Laurent95

Active Member
Licensed User
Longtime User
Last update, it's solved now and really smooth,
Thanks again @stevel05 👍
 

Attachments

  • transparent_form_finalVersion(Optimized code by Stevel05).zip
    60.5 KB · Views: 163
Upvote 0
Top