-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from leexgone/dev
v0.1.8
- Loading branch information
Showing
6 changed files
with
223 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# Rust for windows uiautomation | ||
|
||
`uiatomation-rs`包是一套windows自动化体系的封装包,可以辅助你快速进行Windows自动化程序的开发。 | ||
|
||
## 使用 | ||
|
||
使用时将下面的依赖行加入到Cargo.toml文件中: | ||
|
||
``` toml | ||
[dependencies] | ||
uiautomation = "0.1.8" | ||
``` | ||
|
||
你可以直接使用封装好的API进行操作。 | ||
|
||
## 示例程序 | ||
|
||
### 打印所有界面元素 | ||
|
||
``` rust | ||
use uiautomation::Result; | ||
use uiautomation::UIAutomation; | ||
use uiautomation::UIElement; | ||
use uiautomation::UITreeWalker; | ||
|
||
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()?); | ||
|
||
if let Ok(child) = walker.get_first_child(&element) { | ||
print_element(walker, &child, level + 1)?; | ||
|
||
let mut next = child; | ||
while let Ok(sibing) = walker.get_next_sibling(&next) { | ||
print_element(walker, &sibing, level + 1)?; | ||
|
||
next = sibing; | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
``` | ||
|
||
### 打开记事本并模拟写入文本 | ||
|
||
``` rust | ||
use uiautomation::core::UIAutomation; | ||
use uiautomation::processes::Process; | ||
|
||
fn main() { | ||
Process::create("notepad.exe").unwrap(); | ||
|
||
let automation = UIAutomation::new().unwrap(); | ||
let root = automation.get_root_element().unwrap(); | ||
let matcher = automation.create_matcher().from(root).timeout(10000).classname("Notepad"); | ||
if let Ok(notepad) = matcher.find_first() { | ||
println!("Found: {} - {}", notepad.get_name().unwrap(), notepad.get_classname().unwrap()); | ||
|
||
notepad.send_keys("Hello,Rust UIAutomation!{enter}", 10).unwrap(); | ||
|
||
let window: WindowControl = notepad.try_into().unwrap(); | ||
window.maximize().unwrap(); | ||
} | ||
} | ||
``` | ||
|
||
### 支持Variant类型属性操作 | ||
|
||
``` rust | ||
use uiautomation::UIAutomation; | ||
use uiautomation::variants::Variant; | ||
use windows::Win32::UI::Accessibility::UIA_ControlTypePropertyId; | ||
use windows::Win32::UI::Accessibility::UIA_IsEnabledPropertyId; | ||
use windows::Win32::UI::Accessibility::UIA_NamePropertyId; | ||
|
||
fn main() { | ||
let automation = UIAutomation::new().unwrap(); | ||
let root = automation.get_root_element().unwrap(); | ||
|
||
let name: Variant = root.get_property_value(UIA_NamePropertyId).unwrap(); | ||
println!("name = {}", name.get_string().unwrap()); | ||
|
||
let ctrl_type: Variant = root.get_property_value(UIA_ControlTypePropertyId).unwrap(); | ||
let ctrl_type_id: i32 = ctrl_type.try_into().unwrap(); | ||
println!("control type = {}", ctrl_type_id); | ||
|
||
let enabled: Variant = root.get_property_value(UIA_IsEnabledPropertyId).unwrap(); | ||
let enabled_str: String = enabled.try_into().unwrap(); | ||
println!("enabled = {}", enabled_str); | ||
} | ||
``` | ||
|
||
### 模拟键盘输入 | ||
|
||
``` rust | ||
use uiautomation::core::UIAutomation; | ||
|
||
fn main() { | ||
let automation = UIAutomation::new().unwrap(); | ||
let root = automation.get_root_element().unwrap(); | ||
root.send_keys("{Win}D", 10).unwrap(); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "uiautomation" | ||
version = "0.1.7" | ||
version = "0.1.8" | ||
edition = "2021" | ||
license = "Apache-2.0" | ||
authors = ["Steven Lee <[email protected]>"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters