B4J Question Is it possible to get all windows' title which is opened by users ?

liulifeng77

Active Member
Licensed User
Longtime User
I'm using JAutoItx.jar, but I don't know the title of window(because opening different ppt file or different version of powerpoint, the title of window will be different), so I want to get all window's title, and check the "key" word(powerpoint).any suggestion will be appreciated! thanks!
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Download jna: https://www.b4x.com/android/forum/threads/get-ms-dos-path.84474/#post-535285

code:
B4X:
#AdditionalJar: jna-4.5.0
#AdditionalJar: jna-platform-4.5.0

Sub Process_Globals
   Private titles As List
   Private user32 As JavaObject
End Sub


Sub GetAllWindowsTitles As List
   titles.Initialize
   user32 = user32.InitializeStatic("com.sun.jna.platform.win32.User32").GetField("INSTANCE")
   Dim callback As Object = user32.CreateEvent("com.sun.jna.platform.win32.WinUser.WNDENUMPROC", "callback", "")
   user32.RunMethod("EnumWindows", Array(callback, Null))
   Return titles
End Sub

Sub Callback_Event (MethodName As String, Args() As Object) As Object
   If MethodName = "hashCode" Then Return 0
   If MethodName = "toString" Then Return ""
   If MethodName = "equals" Then Return False
   If MethodName = "callback" Then
       Dim title(512) As Char
       user32.RunMethod("GetWindowText", Array(Args(0), title, 512))
       Dim native As JavaObject
       Dim text As String = native.InitializeStatic("com.sun.jna.Native").RunMethod("toString", Array(title))
       text = text.Trim
       If text.Length > 0 Then titles.Add(text)
   End If
   Return True
End Sub

Note that it will only work properly in release mode. If you want to use it in debug mode then you should add it to a class and compile it to a library.
 
Last edited:
Upvote 0

liulifeng77

Active Member
Licensed User
Longtime User
thank you Erel, I found that if run sub GetAllWindowsTitles 2 times or more times, it will show error message:TextMessage Error: (RuntimeException) java.lang.RuntimeException: Method: GetWindowText not matched.
can you give me some suggestion?
regards
 
Upvote 0

jroriz

Active Member
Licensed User
Longtime User
Download jna: https://www.b4x.com/android/forum/threads/get-ms-dos-path.84474/#post-535285

code:
B4X:
#AdditionalJar: jna-4.5.0
#AdditionalJar: jna-platform-4.5.0

Sub Process_Globals
   Private titles As List
   Private user32 As JavaObject
End Sub


Sub GetAllWindowsTitles As List
   titles.Initialize
   user32 = user32.InitializeStatic("com.sun.jna.platform.win32.User32").GetField("INSTANCE")
   Dim callback As Object = user32.CreateEvent("com.sun.jna.platform.win32.WinUser.WNDENUMPROC", "callback", "")
   user32.RunMethod("EnumWindows", Array(callback, Null))
   Return titles
End Sub

Sub Callback_Event (MethodName As String, Args() As Object) As Object
   If MethodName = "hashCode" Then Return 0
   If MethodName = "toString" Then Return ""
   If MethodName = "equals" Then Return False
   If MethodName = "callback" Then
       Dim title(512) As Char
       user32.RunMethod("GetWindowText", Array(Args(0), title, 512))
       Dim native As JavaObject
       Dim text As String = native.InitializeStatic("com.sun.jna.Native").RunMethod("toString", Array(title))
       text = text.Trim
       If text.Length > 0 Then titles.Add(text)
   End If
   Return True
End Sub

Note that it will only work properly in release mode. If you want to use it in debug mode then you should add it to a class and compile it to a library.

Hi. I need to upgrade this sample and get the window size and position.
Any help?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Hi. I need to upgrade this sample and get the window size and position.
https://github.com/java-native-acce...rm/src/com/sun/jna/platform/win32/User32.java
Maybe
* This function retrieves the dimensions of the bounding rectangle of the
* specified window. The dimensions are given in screen coordinates that are
* relative to the upper-left corner of the screen.
*
* @param hWnd
* Handle to the window.
* @param rect
* Long pointer to a RECT structure that receives the screen
* coordinates of the upper-left and lower-right corners of the
* window.
* @return If the function succeeds, the return value is nonzero. If the
* function fails, the return value is zero.
*/
boolean GetWindowRect(HWND hWnd, RECT rect);
/**
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I didn't find anything related to how to get the window size and position.
GetWindowRect gets the rect of the window on your Desktop.
You have lower left and upper right position.
From this you can calculate top left and height. And also the position
 
Upvote 0

jroriz

Active Member
Licensed User
Longtime User
GetWindowRect gets the rect of the window on your Desktop.
You have lower left and upper right position.
From this you can calculate top left and height. And also the position
Could you help me with a code sample? I dont know how to acomplish this...
 
Upvote 0
Top