完成windows隐藏小托盘

This commit is contained in:
yang 2022-09-26 10:57:16 +08:00
parent 693b8cb74d
commit 93321724ad
15 changed files with 349 additions and 61 deletions

View File

@ -2,5 +2,5 @@
RoomIt是一款基于JavaFx的简便的屏幕画笔工具。 RoomIt是一款基于JavaFx的简便的屏幕画笔工具。
# 功能 # 功能
已完成:画逐渐透明线、矩形、圆形、颜色选择框、形状的删除操作、鼠标变大、涂鸦板回退操作、清空操作、保存截图操作。 已完成:画逐渐透明线、矩形、圆形、颜色选择框、形状的删除操作、鼠标变大、涂鸦板回退操作、清空操作、保存截图操作、windows隐藏小托盘
未完成:windows隐藏小托盘、设置界面(各种形状的参数设置、快捷键、将配置写入文件)、优化性能 未完成:设置界面(各种形状的参数设置、快捷键、将配置写入文件)、优化性能

View File

@ -0,0 +1,66 @@
package org.rococy.roomit;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.paint.Paint;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import org.rococy.roomit.listener.TrayIconMouseListener;
import org.rococy.roomit.util.ScreenUtils;
import javax.imageio.ImageIO;
import java.awt.*;
import java.io.IOException;
import java.net.URL;
import java.util.Objects;
/**
* @author Rococy
*/
public class MainWindow extends Application {
@Override
public void start(Stage stage) throws IOException, AWTException {
// 加载FXML文件
FXMLLoader fxmlLoader = new FXMLLoader(MainWindow.class.getResource("fxml/mainWindow.fxml"));
Scene scene = new Scene(fxmlLoader.load(), ScreenUtils.getScreenWidth(), ScreenUtils.getScreenHeight());
// 场景背景透明
scene.setFill(Paint.valueOf("#00000000"));
// 设置场景
stage.setScene(scene);
// 设置全屏
stage.setFullScreen(true);
// 设置退出全屏的提示文字
stage.setFullScreenExitHint("");
// 置顶
stage.setAlwaysOnTop(true);
// 设置窗口透明
stage.initStyle(StageStyle.TRANSPARENT);
// 将窗口显示出来
// stage.show();
new TrayWindow().show(1000, 1000);
// 初始化托盘图标
initTrayIcon();
}
private void initTrayIcon() throws IOException, AWTException {
// 获取系统托盘
SystemTray systemTray = SystemTray.getSystemTray();
// 加载托盘图片 16*16的png图片
URL imageUrl = Objects.requireNonNull(this.getClass().getResource("/org/rococy/roomit/img/a.png"));
TrayIcon trayIcon = new TrayIcon(ImageIO.read(imageUrl), "RoomIt");
// 托盘图片自适应大小
trayIcon.setImageAutoSize(true);
trayIcon.addMouseListener(new TrayIconMouseListener());
systemTray.add(trayIcon);
}
public static void main(String[] args) {
launch(args);
}
}

View File

@ -1,44 +0,0 @@
package org.rococy.roomit;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.paint.Paint;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import org.rococy.roomit.util.ScreenUtils;
import java.io.IOException;
/**
* @author Rococy
*/
public class RoomItApplication extends Application {
@Override
public void start(Stage stage) throws IOException {
// 加载FXML文件
FXMLLoader fxmlLoader = new FXMLLoader(RoomItApplication.class.getResource("fxml/window.fxml"));
Scene scene = new Scene(fxmlLoader.load(), ScreenUtils.getScreenWidth(), ScreenUtils.getScreenHeight());
// 场景背景透明
scene.setFill(Paint.valueOf("#00000000"));
// 设置场景
stage.setScene(scene);
// 设置全屏
stage.setFullScreen(true);
// 设置退出全屏的提示文字
stage.setFullScreenExitHint("");
// 置顶
stage.setAlwaysOnTop(true);
// 设置窗口透明
stage.initStyle(StageStyle.TRANSPARENT);
// 将窗口显示出来
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}

View File

@ -0,0 +1,44 @@
package org.rococy.roomit;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.paint.Paint;
import javafx.stage.Modality;
import javafx.stage.Stage;
/**
* @author Rococy
* @date 2022/9/26
*/
public class SettingWindow extends Application {
@Override
public void start(Stage stage) throws Exception {
// 加载FXML文件
FXMLLoader fxmlLoader = new FXMLLoader(SettingWindow.class.getResource("fxml/settingWindow.fxml"));
Scene scene = new Scene(fxmlLoader.load());
// 场景填充白色背景
scene.setFill(Paint.valueOf("#00000000"));
// 设置场景
stage.setScene(scene);
// 设置标题
stage.setTitle("Roomit-设置窗口");
// 只有一个x的任务栏
stage.setResizable(false);
stage.initModality(Modality.APPLICATION_MODAL);
// 置顶
stage.setAlwaysOnTop(true);
// 将窗口显示出来
stage.show();
}
public void show() {
Stage stage = new Stage();
try {
start(stage);
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,47 @@
package org.rococy.roomit;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.paint.Paint;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
/**
* 托盘右键的窗口
*
* @author Rococy
* @date 2022/9/25
*/
public class TrayWindow extends Application {
@Override
public void start(Stage stage) throws Exception {
// 加载FXML文件
FXMLLoader fxmlLoader = new FXMLLoader(TrayWindow.class.getResource("fxml/trayWindow.fxml"));
Scene scene = new Scene(fxmlLoader.load());
// 场景填充白色背景
scene.setFill(Paint.valueOf("#00000000"));
// 设置场景
stage.setScene(scene);
// 取消任务栏
stage.initStyle(StageStyle.TRANSPARENT);
// 置顶
stage.setAlwaysOnTop(true);
// 将窗口显示出来
stage.show();
}
public void show(double x, double y) {
Stage stage = new Stage();
// Screen.getPrimary().getOutputScaleX获取屏幕缩放比
stage.setX(x / Screen.getPrimary().getOutputScaleX() - 210);
stage.setY(y / Screen.getPrimary().getOutputScaleY() - 210);
try {
start(stage);
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -33,7 +33,7 @@ import java.util.ResourceBundle;
* @author Rococy * @author Rococy
* @date 2022/9/8 * @date 2022/9/8
*/ */
public class WindowController implements Initializable { public class MainWindowController implements Initializable {
@FXML @FXML
private Container container; private Container container;
@ -68,12 +68,6 @@ public class WindowController implements Initializable {
*/ */
private double globalMouseX, globalMouseY; private double globalMouseX, globalMouseY;
/**
* 置顶置底
*/
private static final int TO_FRONT = 0;
private static final int TO_BACK = 100;
/** /**
* 画线的画板 * 画线的画板
*/ */
@ -124,6 +118,7 @@ public class WindowController implements Initializable {
case B -> this.captureToClipBoard(); case B -> this.captureToClipBoard();
} }
} }
} }
@FXML @FXML
@ -176,8 +171,8 @@ public class WindowController implements Initializable {
*/ */
private void initDrawingBoard() { private void initDrawingBoard() {
container.addChildren(canvasGroup, shapePane); container.addChildren(canvasGroup, shapePane);
canvasGroup.setViewOrder(TO_FRONT); canvasGroup.toFront();
shapePane.setViewOrder(TO_BACK); shapePane.toBack();
} }
/** /**
@ -185,7 +180,7 @@ public class WindowController implements Initializable {
*/ */
private void initColorPicker() { private void initColorPicker() {
colorPicker.getStyleClass().add("button"); colorPicker.getStyleClass().add("button");
colorPicker.setViewOrder(TO_FRONT); colorPicker.toFront();
colorPicker.setOnAction(e -> { colorPicker.setOnAction(e -> {
// 获取16进制颜色 // 获取16进制颜色
@ -226,13 +221,13 @@ public class WindowController implements Initializable {
*/ */
private void switchPane() { private void switchPane() {
Handler shapeHandler = () -> { Handler shapeHandler = () -> {
shapePane.setViewOrder(TO_FRONT); shapePane.toFront();
canvasGroup.setViewOrder(TO_BACK); canvasGroup.toBack();
}; };
this.handleMouseChange(() -> { this.handleMouseChange(() -> {
shapePane.setViewOrder(TO_BACK); shapePane.toBack();
canvasGroup.setViewOrder(TO_FRONT); canvasGroup.toFront();
}, shapeHandler, shapeHandler); }, shapeHandler, shapeHandler);
} }

View File

@ -0,0 +1,15 @@
package org.rococy.roomit.controller;
import javafx.fxml.FXML;
import javafx.scene.layout.HBox;
/**
* @author Rococy
* @date 2022/9/26
*/
public class SettingWindowController {
@FXML
private HBox container;
}

View File

@ -0,0 +1,36 @@
package org.rococy.roomit.controller;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.layout.VBox;
import org.rococy.roomit.SettingWindow;
/**
* @author Rococy
* @date 2022/9/25
*/
public class TrayWindowController {
@FXML
private VBox container;
private SettingWindow settingWindow;
@FXML
public void openSettingWindow() {
// 关闭当前窗口
container.getScene().getWindow().hide();
// 打开设置窗口
if (settingWindow == null) {
settingWindow = new SettingWindow();
}
settingWindow.show();
}
@FXML
public void systemExit() {
Platform.exit();
System.exit(0);
}
}

View File

@ -0,0 +1,58 @@
package org.rococy.roomit.listener;
import javafx.application.Platform;
import org.rococy.roomit.TrayWindow;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
/**
* @author Rococy
* @date 2022/9/25
*/
public class TrayIconMouseListener implements MouseListener {
private static final int MOUSE_LEFT_BUTTON = 1, MOUSE_RIGHT_BUTTON = 3;
private TrayWindow trayWindow;
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
switch (e.getButton()) {
// 左键
case MOUSE_LEFT_BUTTON -> {
}
// 右键
case MOUSE_RIGHT_BUTTON -> Platform.runLater(() -> {
if (trayWindow == null) {
trayWindow = new TrayWindow();
}
trayWindow.show(e.getXOnScreen(), e.getYOnScreen());
});
}
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}

View File

@ -0,0 +1,35 @@
#container {
-fx-background-color: #fff;
-fx-background-radius: 4px;
}
.label {
-fx-pref-width: 200px;
-fx-padding: 8px;
-fx-font-size: 13px;
-fx-text-alignment: center;
-fx-border-style: solid;
-fx-border-width: 0 0 1 0;
-fx-border-color: #ccc;
-fx-cursor: hand;
}
.first-label {
-fx-background-radius: 4px 4px 0 0;
}
.last-label {
-fx-background-radius: 0 0 4px 4px;
-fx-border-width: 0 0 0 0;
}
.label:hover {
-fx-font-weight: 600;
-fx-text-fill: #fff;
-fx-background-color: linear-gradient(to right, #f64f59dd, #c471eddd, #12c2e9dd);
}

View File

@ -4,7 +4,7 @@
<?import org.rococy.roomit.control.Container?> <?import org.rococy.roomit.control.Container?>
<Container xmlns:fx="http://javafx.com/fxml" <Container xmlns:fx="http://javafx.com/fxml"
fx:controller="org.rococy.roomit.controller.WindowController" fx:controller="org.rococy.roomit.controller.MainWindowController"
fx:id="container" fx:id="container"
stylesheets="@../css/index.css" stylesheets="@../css/index.css"
id="container" id="container"

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Button?>
<HBox xmlns:fx="http://javafx.com/fxml"
fx:controller="org.rococy.roomit.controller.SettingWindowController"
stylesheets="@../css/setting.css"
fx:id="container" id="container">
<VBox styleClass="slide">
<!-- 形状 -->
<Button text="形状"></Button>
<!-- 快捷键 -->
<Button text="快捷键"></Button>
</VBox>
</HBox>

View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Label?>
<VBox xmlns:fx="http://javafx.com/fxml"
fx:controller="org.rococy.roomit.controller.TrayWindowController"
stylesheets="@../css/tray.css"
fx:id="container"
id="container">
<Label text="设 置" onMouseClicked="#openSettingWindow" styleClass="label, first-label"/>
<Label text="退 出" onMouseClicked="#systemExit" styleClass="label, last-label"/>
</VBox>

Binary file not shown.

After

Width:  |  Height:  |  Size: 530 B