windows_api_utils/
lib.rs

1//! Windows API utilities for coordinate conversion, bit operations, and message parameter handling.
2//!
3//! This crate provides utilities for working with Windows API concepts including:
4//! - Coordinate conversion between client and screen coordinates
5//! - Bit manipulation (LOWORD, HIWORD, LOBYTE, HIBYTE)
6//! - Windows message parameter handling (WPARAM, LPARAM)
7//!
8//! # Features
9//!
10//! - **Coordinate Conversion**: ClientToScreen, ScreenToClient functionality
11//! - **Bit Manipulation**: Extract high/low words and bytes from integers
12//! - **Message Parameters**: Type-safe WPARAM and LPARAM handling
13//! - **Cross-platform**: Works on Windows, Linux, and macOS
14//! - **No-std support**: Core functionality available without std
15//! - **Feature Gating**: Enable only the functionality you need
16//! - **Serialization**: Optional serde support for message types
17//! - **Windows Interop**: Seamless compatibility with windows crate
18//!
19//! # Feature Flags
20//!
21//! - `coordinates`: Enables coordinate conversion types and traits
22//! - `bit-ops`: Enables bit manipulation utilities  
23//! - `messages`: Enables Windows message handling
24//! - `serde`: Enables serialization support for types
25//! - `windows-interop`: Enables seamless compatibility with windows crate
26//! - `full`: Enables all features
27//! - `minimal`: Disables all optional features (default: enabled)
28//!
29//! # Quick Start
30//!
31//! ## Using default features (coordinates + messages)
32//!
33//! ```toml
34//! [dependencies]
35//! windows-api-utils = "0.1"
36//! ```
37//!
38//! ```rust
39//! use windows_api_utils::prelude::*;
40//!
41//! // Coordinate conversion
42//! let window = Window::new(12345, Rect::new(100, 100, 500, 400), Default::default());
43//! let client_point = Point::new(50, 30);
44//! let screen_point = window.client_to_screen(client_point).unwrap();
45//!
46//! // Message handling
47//! let message = WindowMessage::mouse_move(100, 200, KeyModifiers::default());
48//! if let Some(mouse_event) = MessageParser::parse_mouse_message(message) {
49//!     println!("Mouse at ({}, {})", mouse_event.x, mouse_event.y);
50//! }
51//! ```
52//!
53//! ## Using with windows crate interop
54//!
55//! ```toml
56//! [dependencies]
57//! windows-api-utils = { version = "0.1", features = ["windows-interop"] }
58//! windows = "0.52"
59//! ```
60//!
61//! ```rust
62//! use windows_api_utils::prelude::*;
63//! use windows::Win32::Foundation::{WPARAM, LPARAM};
64//!
65//! // 完全兼容,无需转换!
66//! let win_wparam = WPARAM(0x1234);
67//! let our_wparam = WParam::from(win_wparam);
68//! let back_to_win: WPARAM = our_wparam.into();
69//!
70//! assert_eq!(win_wparam.0, back_to_win.0);
71//!
72//! // 坐标转换测试
73//! let win_lparam = LPARAM(0x00C80064); // x=100, y=200
74//! let our_lparam = LParam::from(win_lparam);
75//! let (x, y) = our_lparam.as_point();
76//! assert_eq!(x, 100);
77//! assert_eq!(y, 200);
78//!
79//! // from_point -> our_lparam -> win_lparam 完整流程测试
80//! let our_lparam_from_coords = LParam::from_point(300, 400);
81//! let win_lparam_from_our: LPARAM = our_lparam_from_coords.into();
82//! let (x_back, y_back) = our_lparam_from_coords.as_point();
83//! assert_eq!(x_back, 300);
84//! assert_eq!(y_back, 400);
85//!
86//! // 验证转换后的值是正确的坐标编码
87//! let expected_value = ((400 as u32) << 16) | (300 as u32);
88//! assert_eq!(win_lparam_from_our.0 as u32, expected_value);
89//! ```
90//!
91//! ## Using only coordinate features
92//!
93//! ```toml
94//! [dependencies]
95//! windows-api-utils = { version = "0.1", default-features = false, features = ["coordinates"] }
96//! ```
97//!
98//! ```rust
99//! use windows_api_utils::{Point, Rect, Window, CoordinateTransformer};
100//!
101//! let window = Window::new(12345, Rect::new(100, 100, 500, 400), Default::default());
102//! let client_point = Point::new(50, 30);
103//! let screen_point = window.client_to_screen(client_point).unwrap();
104//! ```
105//!
106//! ## Using only bit operations
107//!
108//! ```toml
109//! [dependencies]
110//! windows-api-utils = { version = "0.1", default-features = false, features = ["bit-ops"] }
111//! ```
112//!
113//! ```rust
114//! # // 注意:这个测试需要 bit-ops 特性
115//! # use windows_api_utils::{loword, hiword, make_long, BitUtils};
116//! #
117//! let value = 0x12345678;
118//! let low = loword(value);  // 0x5678
119//! let high = hiword(value); // 0x1234
120//! let reconstructed = make_long(high, low);
121//! ```
122//!
123//! ## Minimal usage (no-std compatible)
124//!
125//! ```toml
126//! [dependencies]
127//! windows-api-utils = { version = "0.1", default-features = false }
128//! ```
129//!
130//! ```rust
131//! use windows_api_utils::WindowsUtilsError;
132//!
133//! // Only error types available in minimal mode
134//! ```
135
136#![cfg_attr(not(feature = "std"), no_std)]
137#![warn(missing_docs)]
138
139// Core modules - always available
140pub mod error;
141
142// Feature-gated modules
143#[cfg(feature = "coordinates")]
144pub mod coordinates;
145
146#[cfg(feature = "bit-ops")]
147pub mod bit_ops;
148
149#[cfg(feature = "messages")]
150pub mod messages;
151
152// Re-export error types (always available)
153pub use error::*;
154
155// Conditional re-exports based on features
156#[cfg(feature = "coordinates")]
157pub use coordinates::*;
158
159#[cfg(feature = "bit-ops")]
160pub use bit_ops::*;
161
162#[cfg(feature = "messages")]
163pub use messages::*;
164
165/// Prelude for convenient imports based on enabled features.
166pub mod prelude {
167    pub use crate::WindowsUtilsError;
168    pub use crate::WindowsUtilsResult;
169
170    #[cfg(feature = "coordinates")]
171    pub use crate::{CoordinateTransformer, Point, Rect, Window, WindowStyle};
172
173    #[cfg(feature = "bit-ops")]
174    pub use crate::{hibyte, hiword, lobyte, loword, make_long, make_word, BitUtils, LowHighWord};
175
176    #[cfg(feature = "messages")]
177    pub use crate::{
178        windows_messages, KeyEvent, KeyModifiers, LParam, MessageParser, MouseButton, MouseEvent,
179        WParam, WindowMessage, LPARAM, LRESULT, WPARAM,
180    };
181
182    // Windows interop 预导入
183    #[cfg(feature = "windows-interop")]
184    pub use crate::messages::windows_interop;
185}