B4J Question [SOLVED]Moving the window of an external program does not work

micro

Well-Known Member
Licensed User
Longtime User
Hi to all
From code I start with the shell an external program and after opening I have to move the window in the x-axis.
This is my code that gives no errors but the window always stays in the same position

B4X:
shellprg1.InitializeDoNotHandleQuotes("shellprg1", pathprog, Array(Null))
        shellprg1.Run(-1)
       'get windows open
        Sleep(500) '< Is necessary to give the program time to start up
        Dim WU As JavaObject
        WU.InitializeStatic("com.sun.jna.platform.WindowUtils")
        Dim Lw As List = WU.RunMethod("getAllWindows",Array(True))
        For Each JO As JavaObject In Lw
            Log(JO.RunMethod("getTitle",Null))
            If JO.RunMethod("getTitle",Null).As(String) = "Menu di Attivazione" Then
                attprog1=True
                Dim awtrect As JavaObject = JO.RunMethod("getLocAndSize", Null)
                Log(awtrect)
                Dim xpos As Int = awtrect.GetField("x") + 200
                Dim ypos As Int = awtrect.GetField("y")
                'awtrect.SetField("x", Array(xpos))
                awtrect.RunMethod("setLocation", Array(xpos, ypos)) '<<<<<<<Not work
                Exit
            End If
        Next
        Wait For shellprg1_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)

where I'm wrong?
Thanks
 

Magma

Expert
Licensed User
Longtime User
I didn’t tried your code..

But if you already have run the ext. Window app before run you app (no from jshell) and then run your app to just move it .. what happens?

Is moving? If yes... then the prob. Is shellprg1.run(-1).. if not is something more..
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
I've looked a bit deeper and found that getAllWindows returns a set of DesktopWindow instances. DesktopWindow, is a minimal informational JNA class inheriting directly from Object and does not have any methods to control size and position. It does have the Windows hWnd handle available using getHWND() but I don't know how to further use this to position the window in Java. I guess you will only be able reposition it using its hWND and Windows API calls which I believe JNA does implement.
 
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
This Work

B4X:
Dim jo As JavaObject
Dim jo2 As JavaObject = jo.InitializeStatic("com.sun.jna.platform.win32.User32").GetField("INSTANCE")
Dim hWnd As JavaObject = jo2.RunMethod("FindWindow", Array(Null, "Menu di Attivazione")) 'Menu di Attivazione = window to find
If hWnd.IsInitialized Then
    jo2.RunMethod("SetWindowPos", Array(hWnd, Null, xpos, ypos, width, height, 0))
End If

I have to see if I can find other solutions
Thanks to all
 
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
Now it's seems more complete

B4X:
Dim JO As JavaObject
Dim jo2 As JavaObject = JO.InitializeStatic("com.sun.jna.platform.win32.User32").GetField("INSTANCE")
Dim hWnd As JavaObject = jo2.RunMethod("FindWindow", Array(Null, "Menu di Attivazione"))
If hWnd.IsInitialized Then
    Dim wu As JavaObject
    wu.InitializeStatic("com.sun.jna.platform.WindowUtils")
    Dim awtrect As JavaObject = wu.RunMethod("getWindowLocationAndSize", Array(hWnd))
    Dim xpos As Int = awtrect.GetField("x") + 200 '< Move window
    Dim ypos As Int = awtrect.GetField("y")
    Dim w As Int = awtrect.GetField("width")
    Dim h As Int = awtrect.GetField("height")
    jo2.RunMethod("SetWindowPos", Array(hWnd, Null, xpos, ypos, w, h, 0))
End If

thanks again
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…