Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can i get an window bounds? #35

Closed
niuhuan opened this issue Apr 19, 2023 · 3 comments
Closed

How can i get an window bounds? #35

niuhuan opened this issue Apr 19, 2023 · 3 comments

Comments

@niuhuan
Copy link

niuhuan commented Apr 19, 2023

I tried get bound with get_property_value.

the println out put arr : false false false VARENUM(8197).

and

thread 'main' panicked at 'called Result::unwrap() on an Err value: Error { code: 4, message: "" }', src\main.rs:110:20

hr.get_array().unwrap();

I think it be an double* or SAFEARRAY.

I'm sorry for my poor English. I would like to know how to obtain the position and size of the top window

fn do_capture() {
// get window
    debug!("--- do capture ---");
    let foreground_window = unsafe { GetForegroundWindow() };
    debug!("foreground_window : {:?}", foreground_window);
    if foreground_window.0 == 0 {
        // todo 提示没有捕获到窗口
        return;
    }
// root element
    let ua = UIAutomation::new().unwrap();
    let handle = Handle::from(foreground_window);
    let root_element = ua.element_from_handle(handle).unwrap();
    debug!("root_element : {:?}", root_element);
// get propery
    let hr = root_element
        .get_property_value(UIA_BoundingRectanglePropertyId.into())
        .unwrap();
//
    println!(
        "arr : {} {} {} {:?}",
        hr.is_null(),
        hr.is_array(),
        hr.is_string(),
        hr.get_type(),
    );

    hr.get_array().unwrap();

    // let x: f32 = arr.get_element(0).unwrap();
    // let y: f32 = arr.get_element(1).unwrap();
    // let w: f32 = arr.get_element(2).unwrap();
    // let h: f32 = arr.get_element(3).unwrap();
    //
    // debug!("hr : {x},{y},{w},{h},");

}
@leexgone
Copy link
Owner

I found that the Variant struct has an error when handling array types. I'll fix that in the next version.
Now, you can use get_bounding_rectangle() instead. This will return a Rect struct.

For example:

        let window = unsafe { GetForegroundWindow() };
        if window.0 == 0 {
            return;
        }

        let automation = UIAutomation::new().unwrap();
        let element = automation.element_from_handle(window.into()).unwrap();
        let rect = element.get_bounding_rectangle().unwrap();
        println!("Window Rect = {}", rect);

@niuhuan
Copy link
Author

niuhuan commented Apr 19, 2023

十分感谢您的回答。您可以现在或更新之后关闭这个issue。

@leexgone
Copy link
Owner

fixed on v0.5.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants