I’m working on a Java application where I want to create a transparent, non-clickable window with only a clickable button. To manipulate the window’s native properties, I am using JNA (Java Native Access) to call functions from user32.dll. However, I’m encountering issues with loading and using certain functions from the DLL.
Here’s the scenario:
Transparent Window: I’m using SetLayeredWindowAttributes to make the window transparent.
Non-Clickable Area: I need to use CreateRectRgnA and SetWindowRgn to make the window non-clickable outside the button area.
Clickable Button: I’ve added a button that should remain functional, and it works as expected.
The problem is that some functions, specifically CreateRectRgnA, SetWindowRgn, and SetLayeredWindowAttributes, are throwing errors when I try to call them using JNA. For example:
Error 1: I get an UnsatisfiedLinkError when trying to load user32.dll.
Error 2: Functions like SetWindowRgn and CreateRectRgnA are not
working as expected.Error 3: I receive a java.lang.NullPointerException when attempting to
access specific native function calls.
import com.sun.jna.Native;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinUser;
import javax.swing.*;
import java.awt.*;
public class NonClickableWindowWithButton {
public static void main(String[] args) {
// Create the window (JFrame without decoration)
JFrame window = new JFrame("Non-Clickable Window");
window.setUndecorated(true); // Remove title bar and borders
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(400, 300);
window.setLocationRelativeTo(null);
// Make the window transparent
window.setBackground(new Color(0, 0, 0, 0)); // Fully transparent
window.setLayout(new FlowLayout());
// Add a clickable button
JButton button = new JButton("Clickable");
button.addActionListener(e -> System.out.println("Button clicked!"));
window.add(button);
// Make the window visible
window.setVisible(true);
// Make the window non-clickable, except for the button
makeWindowNonClickable(window);
}
private static void makeWindowNonClickable(JFrame window) {
User32 user32 = User32.INSTANCE;
WinDef.HWND hwnd = getHWND(window);
if (hwnd == null) {
System.err.println("Unable to get the window handle.");
return;
}
// Make the window transparent to clicks
int exStyle = user32.GetWindowLong(hwnd, WinUser.GWL_EXSTYLE);
user32.SetWindowLong(hwnd, WinUser.GWL_EXSTYLE, exStyle | WinUser.WS_EX_LAYERED | WinUser.WS_EX_TRANSPARENT);
}
private static WinDef.HWND getHWND(JFrame window) {
return new WinDef.HWND(Native.getComponentPointer(window));
}
}
interface User32 extends com.sun.jna.platform.win32.User32 {
User32 INSTANCE = Native.load("user32", User32.class);
int GetWindowLong(WinDef.HWND hwnd, int nIndex);
int SetWindowLong(WinDef.HWND hwnd, int nIndex, int dwNewLong);
}
1