关闭杀软

DeeLMind大约 1 分钟

关闭杀软

模拟点击open in new window

use uiautomation::Result;
use uiautomation::UIAutomation;
use uiautomation::UIElement;
use uiautomation::UITreeWalker;

use std::thread;
use std::time::Duration;

use rdev::listen;
use rdev::{Event, EventType};
use rdev::{simulate, Button, SimulateError};

fn slice_string(s: &str, start: usize, end: usize) -> String {
    s.chars().skip(start).take(end - start).collect()
}

fn print_event(event: Event) {
    match event.event_type {
        EventType::MouseMove { x, y } => {
            println!("当前鼠标位置: x = {}, y = {}", x, y);
        }
        _ => {}
    }
}

fn move_mouse_and_click(x: f64, y: f64) {
    // 移动鼠标到指定坐标
    simulate(&EventType::MouseMove { x, y }).expect("MouseMove");
    thread::sleep(Duration::from_secs(2));
    // 模拟鼠标左键点击
    simulate(&EventType::ButtonPress(Button::Left)).expect("ButtonPress");
    simulate(&EventType::ButtonRelease(Button::Left)).expect("ButtonRelease");
}

fn main() {
    let automation = UIAutomation::new().unwrap();
    let walker = automation.get_control_view_walker().unwrap();
    let root = automation.get_root_element().unwrap();
    print_element(&walker, &root, 0).unwrap();
}

fn print_element(walker: &UITreeWalker, element: &UIElement, level: usize) -> Result<()> {
    for _ in 0..level {
        print!(" ")
    }
    // println!("{} - {}", element.get_classname()?, element.get_name()?);

    match element.get_name() {
        Ok(name) => {
            // println!("获取的名称是: {}", name);
            // 匹配元素名称为 "火绒安全软件"
            if slice_string(name.as_str(), 0, 6) == "火绒安全软件".to_string() {
                element.right_click();
                // if let Err(error) = listen(print_event) {
                //     println!("错误: {:?}", error);
                // }
                // 1700 1040

                move_mouse_and_click(3630.0,1140.0);
                thread::sleep(Duration::from_secs(2));
                move_mouse_and_click(3000.0,705.0);
                panic!("匹配成功,程序退出");
            }
        }
        Err(e) => {
            println!("发生错误: {:?}", e);
        }
    }

    // 继续遍历其他子元素
    if let Ok(child) = walker.get_first_child(&element) {
        print_element(walker, &child, level + 1)?;

        let mut next = child;
        while let Ok(sibling) = walker.get_next_sibling(&next) {
            print_element(walker, &sibling, level + 1)?;

            next = sibling;
        }
    }

    Ok(())
}
上次编辑于:
贡献者: DeeLMind