Newer
Older
screenshot-app / src / main / java / com / screenshot / service / WindowService.java
@malexple malexple on 15 Dec 3 KB Add app
package com.screenshot.service;

import com.sun.jna.Native;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.RECT;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class WindowService {
    private static final int MAX_TITLE_LENGTH = 1024;
    private final User32 user32;

    public WindowService() {
        this.user32 = User32.INSTANCE;
    }

    /**
     * Найти окно по заголовку (частичное совпадение)
     */
    public HWND findWindowByTitle(String titlePart) {
        if (titlePart == null || titlePart.isEmpty()) {
            return null;
        }

        final HWND[] result = {null};

        user32.EnumWindows((hwnd, arg) -> {
            char[] windowText = new char[MAX_TITLE_LENGTH];
            user32.GetWindowText(hwnd, windowText, MAX_TITLE_LENGTH);
            String title = Native.toString(windowText);

            if (title.toLowerCase().contains(titlePart.toLowerCase())) {
                if (user32.IsWindowVisible(hwnd)) {
                    result[0] = hwnd;
                    return false; // Прекратить перебор
                }
            }
            return true; // Продолжить перебор
        }, null);

        return result[0];
    }

    /**
     * Получить прямоугольник окна
     */
    public Rectangle getWindowRectangle(HWND hwnd) {
        if (hwnd == null) {
            return 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;

        // Проверка на минимизированное окно
        if (width <= 0 || height <= 0) {
            return null;
        }

        return new Rectangle(x, y, width, height);
    }

    /**
     * Получить заголовок окна
     */
    public String getWindowTitle(HWND hwnd) {
        if (hwnd == null) {
            return "Desktop";
        }
        char[] windowText = new char[MAX_TITLE_LENGTH];
        user32.GetWindowText(hwnd, windowText, MAX_TITLE_LENGTH);
        return Native.toString(windowText);
    }

    /**
     * Получить список всех видимых окон
     */
    public List<String> getAllVisibleWindows() {
        List<String> windows = new ArrayList<>();

        user32.EnumWindows((hwnd, arg) -> {
            if (user32.IsWindowVisible(hwnd)) {
                char[] windowText = new char[MAX_TITLE_LENGTH];
                user32.GetWindowText(hwnd, windowText, MAX_TITLE_LENGTH);
                String title = Native.toString(windowText).trim();
                if (!title.isEmpty()) {
                    windows.add(title);
                }
            }
            return true;
        }, null);

        return windows;
    }

    /**
     * Активировать окно (вывести на передний план)
     */
    public void bringToFront(HWND hwnd) {
        if (hwnd != null) {
            user32.SetForegroundWindow(hwnd);
        }
    }
}