B4J Question How to get the TaskBar Height and position?

Cableguy

Expert
Licensed User
Longtime User
Hi Guys and Girls,

Well, the title says it all, I guess...

I need to get the taskbar height, and position....
 
Solution
You can do that using jna, the code is below
B4X:
Sub Button1_Click
    Dim jo As JavaObject=Me
    jo.RunMethod("taskbar",Null)
End Sub

#if java
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef.RECT;
import com.sun.jna.platform.win32.WinDef.HWND;
    public static void taskbar() {
        User32 user32 = User32.INSTANCE;
        HWND hWnd = user32.FindWindow("Shell_TrayWnd", null);
        if (hWnd != null) {
            RECT rect = new RECT();
            user32.GetWindowRect(hWnd, rect);
            int x = rect.left;
            int y = rect.top;
            int width = rect.right - rect.left;
            int height = rect.bottom - rect.top;
            BA.Log("Task Bar Location: (" + x + ", " + y +...

teddybear

Well-Known Member
Licensed User
What is the postion of the TaskBar you meant, is it on the top,left right or bottom of the screen?
 
Upvote 0

teddybear

Well-Known Member
Licensed User
You can do that using jna, the code is below
B4X:
Sub Button1_Click
    Dim jo As JavaObject=Me
    jo.RunMethod("taskbar",Null)
End Sub

#if java
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef.RECT;
import com.sun.jna.platform.win32.WinDef.HWND;
    public static void taskbar() {
        User32 user32 = User32.INSTANCE;
        HWND hWnd = user32.FindWindow("Shell_TrayWnd", null);
        if (hWnd != null) {
            RECT rect = new RECT();
            user32.GetWindowRect(hWnd, rect);
            int x = rect.left;
            int y = rect.top;
            int width = rect.right - rect.left;
            int height = rect.bottom - rect.top;
            BA.Log("Task Bar Location: (" + x + ", " + y + ")");
            BA.Log("Task Bar Size: " + width + "x" + height);
        }
    }
#End If
Log
B4X:
'Right
Task Bar Location: (1827, 0)
Task Bar Size: 93x1080
'Top
Task Bar Location: (0, 0)
Task Bar Size: 1920x40
'Left
Task Bar Location: (0, 0)
Task Bar Size: 93x1080
'Bottom
Task Bar Location: (0, 1040)
Task Bar Size: 1920x40
 
Last edited:
Upvote 0
Solution

Cableguy

Expert
Licensed User
Longtime User
You can probably help getting this answered by posting some links to code examples found on the internet that give those values.
Amongst some results I found, this seem to be the most complete and pertinent piece of code:
B4X:
public enum Location {
    TOP, RIGHT, BOTTOM, LEFT;
}

private static final class Taskbar {
    public final Location location;
    public final int width, height;

    private Taskbar(Location location, int width, int height) {
        this.location = location;
        this.width = width;
        this.height = height;
    }

    public static Taskbar getTaskbar() {
        Rectangle other = GraphicsEnvironment.getLocalGraphicsEnvironment()
                .getMaximumWindowBounds();
        return new Taskbar(other.x != 0 ? Location.TOP
                : (other.y != 0 ? Location.LEFT
                        : (other.width == IFrame.width ? Location.BOTTOM
                                : Location.RIGHT)), IFrame.width
                - other.width, IFrame.height - other.height);
    }
}
According to the OP...
"Essentially, calling Taskbar.getTaskbar() will give a taskbar containing information on its location (TOP, RIGHT, BOTTOM, LEFT), its width, and its height."
 
Upvote 0

teddybear

Well-Known Member
Licensed User
B4X:
'Right
Task Bar Location: (1827, 0)
Task Bar Size: 93x1080
'Top
Task Bar Location: (0, 0)
Task Bar Size: 1920x40
'Left
Task Bar Location: (0, 0)
Task Bar Size: 93x1080
'Bottom
Task Bar Location: (0, 1040)
Task Bar Size: 1920x40
You can determine the position based on coordinates and aspect ratio
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
You can do that using jna, the code is below
B4X:
Sub Button1_Click
    Dim jo As JavaObject=Me
    jo.RunMethod("taskbar",Null)
End Sub

#if java
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef.RECT;
import com.sun.jna.platform.win32.WinDef.HWND;
    public static void taskbar() {
        User32 user32 = User32.INSTANCE;
        HWND hWnd = user32.FindWindow("Shell_TrayWnd", null);
        if (hWnd != null) {
            RECT rect = new RECT();
            user32.GetWindowRect(hWnd, rect);
            int x = rect.left;
            int y = rect.top;
            int width = rect.right - rect.left;
            int height = rect.bottom - rect.top;
            BA.Log("Task Bar Location: (" + x + ", " + y + ")");
            BA.Log("Task Bar Size: " + width + "x" + height);
        }
    }
#End If
Log
B4X:
'Right
Task Bar Location: (1827, 0)
Task Bar Size: 93x1080
'Top
Task Bar Location: (0, 0)
Task Bar Size: 1920x40
'Left
Task Bar Location: (0, 0)
Task Bar Size: 93x1080
'Bottom
Task Bar Location: (0, 1040)
Task Bar Size: 1920x40
Using your code I'm getting this error:
[IDE message - 3:07:29]
An error occurred.
src\b4j\example\b4xmainpage.java:4: error: package com.sun.jna.platform.win32 does not exist
import com.sun.jna.platform.win32.User32;
^
Note: src\b4j\example\main.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
only showing the first 1 errors, of 8 total; use -Xmaxerrs if you would like to see more

javac 19.0.2
 
Upvote 0

teddybear

Well-Known Member
Licensed User
You need these jars
B4X:
#AdditionalJar:jna-5.14.0.jar
#AdditionalJar:jna-platform-5.14.0.jar
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
ok, thanks, but were do I find them?

I found the .jar files online, seems to work OK.
 
Last edited:
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
The code logs the values, but how do I retrieve them so I can use them? My Java knowledge is almost limited to copy/paste
 
Upvote 0

teddybear

Well-Known Member
Licensed User
The code logs the values, but how do I retrieve them so I can use them? My Java knowledge is almost limited to copy/paste
The simplest way is to return a JSON string and then put them into a map.
B4X:
Sub Button1_Click
    Dim jo As JavaObject=Me
    Dim s As String=jo.RunMethod("taskbar",Null)
    Dim m As Map=s.As(JSON).ToMap
    Log(m)
End Sub

#if java
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef.RECT;
import com.sun.jna.platform.win32.WinDef.HWND;
    public static String taskbar() {
        User32 user32 = User32.INSTANCE;
        HWND hWnd = user32.FindWindow("Shell_TrayWnd", null);
        if (hWnd != null) {
            RECT rect = new RECT();
            user32.GetWindowRect(hWnd, rect);
            int x = rect.left;
            int y = rect.top;
            int width = rect.right - rect.left;
            int height = rect.bottom - rect.top;
            return "{\"x\":"+x+",\"y\":"+y+",\"w\":"+width+",\"h\":"+height+"}";
        }
        return null;
    }
#End If
 
Upvote 0
Top