java窗口透明 鼠标事件,创建一个忽略鼠标和按键事件的JavaFX透明窗口

I want to make a JavaFX application that basically overlays the entire user screen with a Canvas object, so basically I can draw anything on the user's screen.

Making a window that covers the whole screen is simple. Making it essentially transparent can be achieved with this tutorial: https://assylias.wordpress.com/2013/12/08/383/

So the one and only thing stopping me is the fact that obviously, the window, albeit being transparent it will still capture user mouse and key events.

Is there a way I can achieve this? For a more concrete example, imagine I want to make a red circle surround the user's mouse cursor wherever it goes, but the user input will not be interrupted.

解决方案

What you want isn't possible in plain JavaFX.

You can check out my answer here, that's the closest thing. But you can't overlay a transparent canvas over the entire desktop and forward the mouse events to the underlying windows.

Having the Canvas semi-transparent would catch all events, but you could see the underlying windows. But when you have the Canvas fully transparent, your application wouldn't catch any events.

However, your "concrete example" could be solved in a different way. Here's the code:

import java.awt.MouseInfo;

import java.awt.Point;

import java.awt.PointerInfo;

import javafx.animation.AnimationTimer;

import javafx.application.Application;

import javafx.scene.Group;

import javafx.scene.Scene;

import javafx.scene.paint.Color;

import javafx.scene.shape.Circle;

import javafx.stage.Stage;

import javafx.stage.StageStyle;

public class CircleAroundCursor extends Application {

double radius = 50;

@Override

public void start(Stage primaryStage) {

Group root = new Group();

Circle circle = new Circle( radius * 2,radius * 2,radius);

circle.setStroke(Color.RED);

circle.setFill(Color.TRANSPARENT);

root.getChildren().add(circle);

Scene scene = new Scene(root, Color.TRANSPARENT);

scene.getRoot().setStyle("-fx-background-color: transparent");

primaryStage.initStyle(StageStyle.TRANSPARENT);

primaryStage.setScene(scene);

primaryStage.show();

primaryStage.setAlwaysOnTop(true);

AnimationTimer loop = new AnimationTimer() {

@Override

public void handle(long now) {

PointerInfo info = MouseInfo.getPointerInfo();

Point p = info.getLocation();

primaryStage.setX(p.getX() - radius * 2);

primaryStage.setY(p.getY() - radius * 2);

}

};

loop.start();

}

public static void main(String[] args) {

launch(args);

}

}

This at least solves "I want to make a red circle surround the user's mouse cursor wherever it goes, but the user input will not be interrupted"

Note: Here AWT classes are mixed with FX classes. You may need to use an EDT & FX thread handling. It does work without though.

Screenshot:

uoBQr.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值