### Basic Window Setup in Rust Source: https://docs.rs/native-windows-gui/latest/native_windows_gui/struct.TextInput.html This is a minimal example of setting up a native Windows GUI application. It initializes the library, sets a global font, and builds a basic window with specified size, position, and title. This serves as the foundation for more complex UIs. ```rust fn main() { nwg::init().expect("Failed to init Native Windows GUI"); nwg::Font::set_global_family("Segoe UI").expect("Failed to set default font"); let mut window = Default::default(); let mut name_edit = Default::default(); let mut hello_button = Default::default(); let layout = Default::default(); nwg::Window::builder() .size((300, 115)) .position((300, 300)) .title("Basic example") .build(&mut window) ``` -------------------------------- ### Bitmap Loading Examples Source: https://docs.rs/native-windows-gui/latest/native_windows_gui/struct.Bitmap.html Examples demonstrating how to load bitmaps using different methods. ```APIDOC ## Examples ### Loading a Bitmap from a File ```rust use native_windows_gui as nwg; fn load_bitmap() -> nwg::Bitmap { nwg::Bitmap::from_file("Hello.bmp", true).unwrap() } ``` ### Loading a Bitmap using the Builder Pattern ```rust use native_windows_gui as nwg; fn load_bitmap_builder() -> nwg::Bitmap { let mut bitmap = nwg::Bitmap::default(); nwg::Bitmap::builder() .source_file(Some("Hello.bmp")) .strict(true) .build(&mut bitmap) .unwrap(); bitmap } ``` ``` -------------------------------- ### Dispatch thread events in Rust Source: https://docs.rs/native-windows-gui/latest/native_windows_gui/fn.dispatch_thread_events.html Examples of initializing the GUI and starting the event loop across various application types. ```rust fn main() { nwg::init().expect("Failed to init Native Windows GUI"); let _ui = SystemTray::build_ui(Default::default()).expect("Failed to build UI"); nwg::dispatch_thread_events(); } ``` ```rust fn main() { nwg::init().expect("Failed to init Native Windows GUI"); nwg::Font::set_global_family("Segoe UI").expect("Failed to set default font"); let _ui = BasicApp::build_ui(Default::default()).expect("Failed to build UI"); nwg::dispatch_thread_events(); } ``` ```rust fn main() { nwg::init().expect("Failed to init Native Windows GUI"); nwg::Font::set_global_family("Segoe UI").expect("Failed to set default font"); let _ui = BasicApp::build_ui(Default::default()).expect("Failed to build UI"); nwg::dispatch_thread_events(); } ``` ```rust fn main() { nwg::init().expect("Failed to init Native Windows GUI"); nwg::Font::set_global_family("Segoe UI").expect("Failed to set default font"); let _app = Calculator::build_ui(Default::default()).expect("Failed to build UI"); nwg::dispatch_thread_events(); } ``` ```rust fn main() { nwg::init().expect("Failed to init Native Windows GUI"); nwg::Font::set_global_family("Segoe UI").expect("Failed to set default font"); let _ui = MessageBank::build_ui(Default::default()).expect("Failed to build UI"); nwg::dispatch_thread_events(); } ``` ```rust fn main() { nwg::init().expect("Failed to init Native Windows GUI"); nwg::Font::set_global_family("Segoe UI").expect("Failed to set default font"); let _ui = FlexBoxApp::build_ui(Default::default()).expect("Failed to build UI"); nwg::dispatch_thread_events(); } ``` -------------------------------- ### Example Usage Source: https://docs.rs/native-windows-gui/latest/native_windows_gui/struct.Monitor.html Demonstrates how to use Monitor methods to create and center a window. ```APIDOC ```rust // Creating and centering a window in the main monitor use native_windows_gui as nwg; fn create_window(width: i32, height: i32) -> nwg::Window { let [total_width, total_height] = [nwg::Monitor::width(), nwg::Monitor::height()]; let mut window = nwg::Window::default(); let x = (total_width - width) / 2; let y = (total_height - height) / 2; nwg::Window::builder() .size((width, height)) .position((x, y)) .build(&mut window) .unwrap(); window } ``` ``` -------------------------------- ### Flexbox Layout Implementation Source: https://docs.rs/native-windows-gui/latest/src/flexbox_sub_layout/flexbox_sub_layout.rs.html A complete example showing the setup of a window, buttons, and nested flexbox layouts. Requires the 'flexbox' feature flag to be enabled. ```rust /*! A very simple application that show how to use a flexbox layout. Requires the following features: `cargo run --example flexbox --features "flexbox"` */ extern crate native_windows_gui as nwg; use nwg::NativeUi; #[derive(Default)] pub struct FlexBoxApp { window: nwg::Window, layout: nwg::FlexboxLayout, button1: nwg::Button, layout2: nwg::FlexboxLayout, button2: nwg::Button, button3: nwg::Button, } impl FlexBoxApp { fn exit(&self) { nwg::stop_thread_dispatch(); } } // // ALL of this stuff is handled by native-windows-derive // mod flexbox_app_ui { use native_windows_gui as nwg; use super::*; use std::rc::Rc; use std::cell::RefCell; use std::ops::Deref; pub struct FlexBoxAppUi { inner: Rc, default_handler: RefCell> } impl nwg::NativeUi for FlexBoxApp { fn build_ui(mut data: FlexBoxApp) -> Result { use nwg::Event as E; // Controls nwg::Window::builder() .size((500, 500)) .position((300, 300)) .title("Flexbox example") .build(&mut data.window)?; nwg::Button::builder() .text("Btn 1") .parent(&data.window) .focus(true) .build(&mut data.button1)?; nwg::Button::builder() .text("Btn 2") .parent(&data.window) .focus(true) .build(&mut data.button2)?; nwg::Button::builder() .text("Btn 3") .parent(&data.window) .focus(true) .build(&mut data.button3)?; // Wrap-up let ui = FlexBoxAppUi { inner: Rc::new(data), default_handler: Default::default(), }; // Events let evt_ui = Rc::downgrade(&ui.inner); let handle_events = move |evt, _evt_data, handle| { if let Some(evt_ui) = evt_ui.upgrade() { match evt { E::OnWindowClose => if &handle == &evt_ui.window { FlexBoxApp::exit(&evt_ui); }, _ => {} } } }; *ui.default_handler.borrow_mut() = Some(nwg::full_bind_event_handler(&ui.window.handle, handle_events)); // Layout use nwg::stretch::{geometry::Size, style::{Dimension as D, FlexDirection}}; nwg::FlexboxLayout::builder() .parent(&ui.window) .flex_direction(FlexDirection::Column) .child(&ui.button2) .child_size(Size { width: D::Auto, height: D::Points(200.0) }) .child(&ui.button3) .child_flex_grow(2.0) .child_size(Size { width: D::Auto, height: D::Auto }) .build_partial(&ui.layout2)?; nwg::FlexboxLayout::builder() .parent(&ui.window) .flex_direction(FlexDirection::Row) .child(&ui.button1) .child_flex_grow(2.0) .child_size(Size { width: D::Auto, height: D::Auto }) .child_layout(&ui.layout2) .child_size(Size { width: D::Points(300.0), height: D::Auto }) .build(&ui.layout)?; return Ok(ui); } } impl Drop for FlexBoxAppUi { /// To make sure that everything is freed without issues, the default handler must be unbound. fn drop(&mut self) { let handler = self.default_handler.borrow(); if handler.is_some() { nwg::unbind_event_handler(handler.as_ref().unwrap()); } } } impl Deref for FlexBoxAppUi { type Target = FlexBoxApp; fn deref(&self) -> &FlexBoxApp { &self.inner } } } fn main() { nwg::init().expect("Failed to init Native Windows GUI"); nwg::Font::set_global_family("Segoe UI").expect("Failed to set default font"); let _ui = FlexBoxApp::build_ui(Default::default()).expect("Failed to build UI"); nwg::dispatch_thread_events(); } ``` -------------------------------- ### IconBuilder Example: Using source_file Source: https://docs.rs/native-windows-gui/latest/native_windows_gui/struct.IconBuilder.html An example demonstrating how to use `IconBuilder` with the `source_file` method to load an icon from a .ico file. ```APIDOC ## POST /api/users ### Description This endpoint creates a new user in the system. It requires user details in the request body. ### Method POST ### Endpoint /api/users ### Parameters #### Request Body - **username** (string) - Required - The desired username for the new user. - **email** (string) - Required - The email address of the new user. - **password** (string) - Required - The password for the new user. ### Request Example ```json { "username": "johndoe", "email": "john.doe@example.com", "password": "securepassword123" } ``` ### Response #### Success Response (201 Created) - **id** (string) - The unique identifier for the newly created user. - **username** (string) - The username of the created user. - **email** (string) - The email address of the created user. #### Response Example ```json { "id": "a1b2c3d4-e5f6-7890-1234-567890abcdef", "username": "johndoe", "email": "john.doe@example.com" } ``` ``` -------------------------------- ### Basic Flexbox Layout Setup in Rust Source: https://docs.rs/native-windows-gui/latest/native_windows_gui/struct.FlexboxLayoutBuilder.html Illustrates the initial setup for a Flexbox layout in a Rust GUI application, including window and button creation. ```rust 44 fn build_ui(mut data: FlexBoxApp) -> Result { 45 use nwg::Event as E; 46 47 // Controls 48 nwg::Window::builder() 49 .size((500, 300)) 50 .position((300, 300)) 51 .title("Flexbox example") 52 .build(&mut data.window)?; 53 54 nwg::Button::builder() 55 .text("Btn 1") 56 .parent(&data.window) 57 .focus(true) 58 .build(&mut data.button1)?; 59 60 nwg::Button::builder() 61 .text("Btn 2") 62 .parent(&data.window) 63 .build(&mut data.button2)?; 64 65 nwg::Button::builder() 66 .text("Btn 3") 67 .parent(&data.window) 68 .build(&mut data.button3)?; 69 70 // Wrap-up 71 let ui =FlexBoxAppUi { 72 inner: Rc::new(data), 73 default_handler: Default::default(), 74 }; 75 76 // Events 77 let evt_ui = Rc::downgrade(&ui.inner); 78 let handle_events = move |evt, _evt_data, handle| { 79 if let Some(evt_ui) = evt_ui.upgrade() { 80 match evt { 81 E::OnWindowClose => 82 if &handle == &evt_ui.window { 83 FlexBoxApp::exit(&evt_ui); 84 }, 85 _ => {} 86 } 87 } 88 }; 89 90 *ui.default_handler.borrow_mut() = Some(nwg::full_bind_event_handler(&ui.window.handle, handle_events)); 91 92 93 // Layout 94 use nwg::stretch::{geometry::{Size, Rect}, style::{Dimension as D, FlexDirection, AlignSelf}}; 95 const FIFTY_PC: D = D::Percent(0.5); ``` -------------------------------- ### Call a library function in an example Source: https://docs.rs/native-windows-gui/latest/scrape-examples-help.html An example file demonstrating how to call a function defined in a crate. ```rust // examples/ex.rs fn main() { a_crate::a_func(); } ``` -------------------------------- ### Build an ImageFrame Source: https://docs.rs/native-windows-gui/latest/native_windows_gui/struct.ImageFrame.html Example demonstrating how to initialize an ImageFrame using the builder pattern. ```rust use native_windows_gui as nwg; fn build_frame(button: &mut nwg::ImageFrame, window: &nwg::Window, ico: &nwg::Icon) { nwg::ImageFrame::builder() .parent(window) .build(button); } ``` -------------------------------- ### ComboBox Builder Example Source: https://docs.rs/native-windows-gui/latest/native_windows_gui/struct.ComboBox.html Example of how to build and configure a ComboBox with a collection of string slices and a default selected index. ```APIDOC ## POST /api/combobox (Example) ### Description This example demonstrates how to initialize a ComboBox with a parent window, a collection of items, and a pre-selected item. ### Method POST ### Endpoint /api/combobox ### Parameters #### Request Body - **parent** (Window) - Required - The parent window for the combobox. - **size** (tuple) - Optional - The size of the combobox (width, height). - **collection** (Vec<&'static str>) - Required - The list of items to display in the combobox. - **selected_index** (Option) - Optional - The index of the initially selected item. ### Request Example ```json { "parent": "window_handle", "size": [200, 30], "collection": ["one", "two"], "selected_index": 0 } ``` ### Response #### Success Response (200) - **handle** (ControlHandle) - The handle to the created combobox. #### Response Example ```json { "handle": "combobox_handle" } ``` ``` -------------------------------- ### Build a Notice Control Source: https://docs.rs/native-windows-gui/latest/src/native_windows_gui/controls/notice.rs.html Example of how to build and associate a Notice control with a parent window. ```rust use native_windows_gui as nwg fn build_notice(notice: &mut nwg::Notice, window: &nwg::Window) { nwg::Notice::builder() .parent(window) .build(notice); } ``` -------------------------------- ### ControlBase Usage Example Source: https://docs.rs/native-windows-gui/latest/native_windows_gui/struct.ControlBase.html Example demonstrating the usage of ControlBase builder methods to create controls. ```APIDOC ```rust use native_windows_gui as nwg; fn basic_stuff(window: &nwg::Window) -> Result<(), nwg::NwgError> { // Build an HWND for a button nwg::ControlBase::build_hwnd() .class_name("BUTTON") .forced_flags(0) .flags(0) .size((100, 100)) .position((100, 100)) .text("HELLO") .parent(Some(window.handle)) .build()?; // Build an HMENU item if the 'menu' feature is enabled #[cfg(feature = "menu")] nwg::ControlBase::build_hmenu() .text("Item") .item(true) .parent(window.handle) .build()?; Ok(()) } ``` ``` -------------------------------- ### Build a TextBox Source: https://docs.rs/native-windows-gui/latest/native_windows_gui/struct.TextBox.html Example of initializing a TextBox using the builder pattern. ```rust use native_windows_gui as nwg; fn build_box(tbox: &mut nwg::TextBox, window: &nwg::Window, font: &nwg::Font) { nwg::TextBox::builder() .text("Hello") .font(Some(font)) .parent(window) .build(tbox); } ``` -------------------------------- ### Building a CheckBox Source: https://docs.rs/native-windows-gui/latest/native_windows_gui/struct.CheckBox.html Example demonstrating how to initialize a CheckBox using the builder pattern. ```rust use native_windows_gui as nwg; fn build_checkbox(button: &mut nwg::CheckBox, window: &nwg::Window, font: &nwg::Font) { nwg::CheckBox::builder() .text("Hello") .flags(nwg::CheckBoxFlags::VISIBLE) .font(Some(font)) .parent(window) .build(button); } ``` -------------------------------- ### Initialize and Use Locale Source: https://docs.rs/native-windows-gui/latest/native_windows_gui/struct.Locale.html Examples of creating Locale instances and retrieving locale-specific information. ```rust use native_windows_gui as nwg; let fr_locale = nwg::Locale::from_str("fr"); let en_us_locale = nwg::Locale::from_str("en-US"); let user_locale = nwg::Locale::user(); let locales: Vec = nwg::Locale::all(); user_locale.display_name(); ``` -------------------------------- ### Build a Timer Source: https://docs.rs/native-windows-gui/latest/native_windows_gui/struct.Timer.html Example of initializing and building a Timer using the builder pattern. ```rust use native_windows_gui as nwg; fn build_timer(parent: &nwg::Window) { let mut timer = Default::default(); nwg::Timer::builder() .parent(parent) .interval(100) .stopped(false) .build(&mut timer); } ``` -------------------------------- ### Get Pixel Format GUID Source: https://docs.rs/native-windows-gui/latest/src/native_windows_gui/resources/image_decoder.rs.html Retrieves the pixel format of the bitmap source as a GUID. Consult the Microsoft WIC documentation for a mapping of GUIDs to specific pixel formats. ```rust pub fn pixel_format(&self) -> WICPixelFormatGUID { let mut fmt = unsafe { mem::zeroed() }; unsafe { (&*self.frame).GetPixelFormat(&mut fmt) }; fmt } ``` -------------------------------- ### Build a TextInput control Source: https://docs.rs/native-windows-gui/latest/native_windows_gui/struct.TextInput.html Example demonstrating how to instantiate a TextInput using the builder pattern. ```rust use native_windows_gui as nwg; fn build_box(tbox: &mut nwg::TextInput, window: &nwg::Window, font: &nwg::Font) { nwg::TextInput::builder() .text("Hello") .font(Some(font)) .parent(window) .build(tbox); } ``` -------------------------------- ### GET /trackbar/selection_range_pos Source: https://docs.rs/native-windows-gui/latest/src/native_windows_gui/controls/track_bar.rs.html Retrieves the starting and ending position of the current selection range in a trackbar. Only works for trackbars with the `Range` flags. ```APIDOC ## GET /trackbar/selection_range_pos ### Description Retrieves the starting and ending position of the current selection range in a trackbar. This functionality is only available for trackbars that have been created with the `TrackBarFlags::RANGE` flag. ### Method GET ### Endpoint /trackbar/selection_range_pos ### Parameters None ### Response #### Success Response (200) - **start** (usize) - The starting position of the selected range. - **end** (usize) - The ending position of the selected range. #### Response Example ```json { "start": 20, "end": 80 } ``` ``` -------------------------------- ### Implement a basic layout Source: https://docs.rs/native-windows-gui/latest/native_windows_gui/struct.TextInputBuilder.html Demonstrates the initial setup of a window and controls within a build_ui function, focusing on the builder pattern for UI components. ```rust 47 fn build_ui(mut data: BasicApp) -> Result { 48 use nwg::Event as E; 49 50 // Controls 51 nwg::Window::builder() 52 .flags(nwg::WindowFlags::WINDOW | nwg::WindowFlags::VISIBLE) 53 .size((300, 115)) 54 .position((300, 300)) 55 .title("Basic example") 56 .build(&mut data.window)?; 57 58 nwg::TextInput::builder() 59 .text("Heisenberg") 60 .parent(&data.window) 61 .focus(true) 62 .build(&mut data.name_edit)?; 63 64 nwg::Button::builder() 65 .text("Say my name") 66 .parent(&data.window) 67 .build(&mut data.hello_button)?; ``` -------------------------------- ### Build a Label Component Source: https://docs.rs/native-windows-gui/latest/native_windows_gui/struct.Label.html Example demonstrating how to construct a Label using the builder pattern. ```rust use native_windows_gui as nwg; fn build_label(label: &mut nwg::Label, window: &nwg::Window, font: &nwg::Font) { nwg::Label::builder() .text("Hello") .font(Some(font)) .parent(window) .build(label); } ``` -------------------------------- ### Basic Window and Controls Setup in Rust Source: https://docs.rs/native-windows-gui/latest/native_windows_gui/fn.full_bind_event_handler.html Sets up a basic window, text input, and button. Requires `nwg` crate. Used for simple UI interactions. ```rust fn build_ui(mut data: BasicApp) -> Result { use nwg::Event as E; // Controls nwg::Window::builder() .flags(nwg::WindowFlags::WINDOW | nwg::WindowFlags::VISIBLE) .size((300, 115)) .position((300, 300)) .title("Basic example") .build(&mut data.window)?; nwg::TextInput::builder() .text("Heisenberg") .parent(&data.window) .focus(true) .build(&mut data.name_edit)?; nwg::Button::builder() .text("Say my name") .parent(&data.window) .build(&mut data.hello_button)?; // Wrap-up let ui = BasicAppUi { inner: Rc::new(data), default_handler: Default::default(), }; // Events let evt_ui = Rc::downgrade(&ui.inner); let handle_events = move |evt, _evt_data, handle| { if let Some(evt_ui) = evt_ui.upgrade() { match evt { E::OnButtonClick => if &handle == &evt_ui.hello_button { BasicApp::say_hello(&evt_ui); }, E::OnWindowClose => if &handle == &ui.window { BasicApp::say_goodbye(&evt_ui); }, _ => {} } } }; *ui.default_handler.borrow_mut() = Some(nwg::full_bind_event_handler(&ui.window.handle, handle_events)); // Layouts nwg::GridLayout::builder() .parent(&ui.window) .spacing(1) .child(0, 0, &ui.name_edit) .child_item(nwg::GridLayoutItem::new(&ui.hello_button, 0, 1, 1, 2)) .build(&ui.layout)?; return Ok(ui); } ``` -------------------------------- ### Get Winapi Base Flags Source: https://docs.rs/native-windows-gui/latest/src/native_windows_gui/controls/text_input.rs.html Returns the base WinAPI window style flags used during window creation. This example returns `WS_VISIBLE`. ```rust pub fn flags(&self) -> u32 { ::winapi::um::winuser::WS_VISIBLE } ``` -------------------------------- ### Create and center a window Source: https://docs.rs/native-windows-gui/latest/native_windows_gui/struct.Monitor.html Example demonstrating how to use Monitor methods to calculate window position for centering on the primary monitor. ```rust // Creating and centering a window in the main monitor use native_windows_gui as nwg; fn create_window(width: i32, height: i32) -> nwg::Window { let [total_width, total_height] = [nwg::Monitor::width(), nwg::Monitor::height()]; let mut window = nwg::Window::default(); let x = (total_width-width)/2; let y = (total_height-height)/2; nwg::Window::builder() .size((width, height)) .position((x, y)) .build(&mut window) .unwrap(); window } ``` -------------------------------- ### Get Selected Character Range Source: https://docs.rs/native-windows-gui/latest/src/native_windows_gui/controls/text_box.rs.html Retrieves the starting and ending positions of the currently selected text within the input control. The handle must be bound. ```rust use winapi::um::winuser::EM_GETSEL; if self.handle.blank() { panic!("{}", NOT_BOUND); } let handle = self.handle.hwnd().expect(BAD_HANDLE); let (mut out1, mut out2) = (0u32, 0u32); let (ptr1, ptr2) = (&mut out1 as *mut u32, &mut out2 as *mut u32); wh::send_message(handle, EM_GETSEL as u32, ptr1 as WPARAM, ptr2 as LPARAM); Range { start: out1 as u32, end: out2 as u32 } ``` -------------------------------- ### Basic Control Creation Example Source: https://docs.rs/native-windows-gui/latest/native_windows_gui/struct.ControlBase.html Demonstrates building a button (HWND) and a menu item (HMENU) using ControlBase. Requires the 'menu' feature flag for HMENU creation. ```rust use native_windows_gui as nwg; fn basic_stuff(window: &nwg::Window) -> Result<(), nwg::NwgError> { nwg::ControlBase::build_hwnd() .class_name("BUTTON") .forced_flags(0) .flags(0) .size((100, 100)) .position((100, 100)) .text("HELLO") .parent(Some(window.handle)) .build()?; #[cfg(feature = "menu")] nwg::ControlBase::build_hmenu() .text("Item") .item(true) .parent(window.handle) .build()?; Ok(()) } ``` -------------------------------- ### Get RichTextBox Selection Range Source: https://docs.rs/native-windows-gui/latest/native_windows_gui/struct.RichTextBox.html Returns the range of characters currently selected by the user within the RichTextBox. The range is represented as `start..end`. ```rust pub fn selection(&self) -> Range ``` -------------------------------- ### Basic Application UI Setup in Rust Source: https://docs.rs/native-windows-gui/latest/native_windows_gui/struct.WindowBuilder.html Sets up a basic window, text input, and button for a Rust application using the Native Windows GUI library. ```rust fn build_ui(mut data: BasicApp) -> Result { use nwg::Event as E; // Controls nwg::Window::builder() .flags(nwg::WindowFlags::WINDOW | nwg::WindowFlags::VISIBLE) .size((300, 135)) .position((300, 300)) .title("Basic example") .build(&mut data.window)?; nwg::TextInput::builder() .size((280, 35)) .position((10, 10)) .text("Heisenberg") .parent(&data.window) .focus(true) .build(&mut data.name_edit)?; nwg::Button::builder() .size((280, 70)) .position((10, 50)) .text("Say my name") .parent(&data.window) .build(&mut data.hello_button)?; // Wrap-up let ui = BasicAppUi { ``` -------------------------------- ### Basic Window with Controls and Event Handling Source: https://docs.rs/native-windows-gui/latest/native_windows_gui/struct.Button.html Demonstrates building a basic UI with a window, text input, and a button, along with setting up event handlers for button clicks and window closing. This example uses a struct to manage application data and UI state. ```rust fn build_ui(mut data: BasicApp) -> Result { use nwg::Event as E; // Controls nwg::Window::builder() .flags(nwg::WindowFlags::WINDOW | nwg::WindowFlags::VISIBLE) .size((300, 135)) .position((300, 300)) .title("Basic example") .build(&mut data.window)?; nwg::TextInput::builder() .size((280, 35)) .position((10, 10)) .text("Heisenberg") .parent(&data.window) .focus(true) .build(&mut data.name_edit)?; nwg::Button::builder() .size((280, 70)) .position((10, 50)) .text("Say my name") .parent(&data.window) .build(&mut data.hello_button)?; // Wrap-up let ui = BasicAppUi { inner: Rc::new(data), default_handler: Default::default(), }; // Events let evt_ui = Rc::downgrade(&ui.inner); let handle_events = move |evt, _evt_data, handle| { if let Some(ui) = evt_ui.upgrade() { match evt { E::OnButtonClick => if &handle == &ui.hello_button { BasicApp::say_hello(&ui); }, E::OnWindowClose => if &handle == &ui.window { BasicApp::say_goodbye(&ui); }, _ => {} } } }; *ui.default_handler.borrow_mut() = Some(nwg::full_bind_event_handler(&ui.window.handle, handle_events)); return Ok(ui); } ``` -------------------------------- ### Get Selected Character Range Source: https://docs.rs/native-windows-gui/latest/src/native_windows_gui/controls/rich_label.rs.html Returns the start and end indices of the currently selected text range in a text input control. Requires the `winapi` crate. ```rust pub fn selection(&self) -> Range { use winapi::um::winuser::EM_GETSEL; let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE); let (mut out1, mut out2) = (0u32, 0u32); let (ptr1, ptr2) = (&mut out1 as *mut u32, &mut out2 as *mut u32); wh::send_message(handle, EM_GETSEL as u32, ptr1 as _, ptr2 as _); Range { start: out1 as usize, end: out2 as usize } } ``` -------------------------------- ### GridLayout Setup in Rust Source: https://docs.rs/native-windows-gui/latest/native_windows_gui/struct.TextInputBuilder.html Configures a GridLayout for arranging UI elements within a window. This example shows how to define parent, spacing, and individual child element positions. ```rust nwg::GridLayout::builder() .parent(&ui.window) .spacing(1) .child(0, 0, &ui.name_edit) .child_item(nwg::GridLayoutItem::new(&ui.hello_button, 0, 1, 1, 2)) .build(&ui.layout)?; ``` -------------------------------- ### Get Locale Native Country Name in Rust Source: https://docs.rs/native-windows-gui/latest/src/native_windows_gui/winnls/locale.rs.html Retrieves the native language name of the country or region for the locale, for example, "Deutschland". This function calls `GetLocaleInfoEx` with the `LOCALE_SNATIVECOUNTRY` flag (0x00000008). ```rust self.get_locale_info_string(0x00000008) ``` -------------------------------- ### Build and Use Notice Component Source: https://docs.rs/native-windows-gui/latest/native_windows_gui/struct.Notice.html Examples demonstrating how to initialize a Notice component and how to trigger it from a background thread. ```rust use native_windows_gui as nwg; fn build_notice(notice: &mut nwg::Notice, window: &nwg::Window) { nwg::Notice::builder() .parent(window) .build(notice); } ``` ```rust use native_windows_gui as nwg; use std::thread; use std::time; fn notice(noticer: &nwg::Notice) { let sender = noticer.sender(); thread::spawn(move || { thread::sleep(time::Duration::new(5, 0)); sender.notice(); }); } ``` -------------------------------- ### Basic Barebone Window with Controls Source: https://docs.rs/native-windows-gui/latest/native_windows_gui/struct.Button.html Illustrates the fundamental setup for a Native Windows GUI application, including window creation, text input, a button, and a grid layout. Event handling for button clicks and window closing is also shown. ```rust fn main() { nwg::init().expect("Failed to init Native Windows GUI"); nwg::Font::set_global_family("Segoe UI").expect("Failed to set default font"); let mut window = Default::default(); let mut name_edit = Default::default(); let mut hello_button = Default::default(); let layout = Default::default(); nwg::Window::builder() .size((300, 115)) .position((300, 300)) .title("Basic example") .build(&mut window) .unwrap(); nwg::TextInput::builder() .text("Heisenberg") .focus(true) .parent(&window) .build(&mut name_edit) .unwrap(); nwg::Button::builder() .text("Say my name") .parent(&window) .build(&mut hello_button) .unwrap(); nwg::GridLayout::builder() .parent(&window) .spacing(1) .child(0, 0, &name_edit) .child_item(nwg::GridLayoutItem::new(&hello_button, 0, 1, 1, 2)) .build(&layout) .unwrap(); let window = Rc::new(window); let events_window = window.clone(); let handler = nwg::full_bind_event_handler(&window.handle, move |evt, _evt_data, handle| { use nwg::Event as E; match evt { E::OnWindowClose => if &handle == &events_window as &nwg::Window { nwg::modal_info_message(&events_window.handle, "Goodbye", &format!("Goodbye {}", name_edit.text())); nwg::stop_thread_dispatch(); }, E::OnButtonClick => if &handle == &hello_button { nwg::modal_info_message(&events_window.handle, "Hello", &format!("Hello {}", name_edit.text())); }, _ => {} } }); nwg::dispatch_thread_events(); nwg::unbind_event_handler(&handler); } ``` -------------------------------- ### Get Locale Dialing Code in Rust Source: https://docs.rs/native-windows-gui/latest/src/native_windows_gui/winnls/locale.rs.html Returns the country/region dialing code for the locale. For example, both "en-US" and "en-CA" return 1. This method uses `GetLocaleInfoEx` with the `LOCALE_IDIALINGCODE` flag (0x00000005). ```rust self.get_locale_info_int(0x00000005) ``` -------------------------------- ### Build Basic Window and Buttons in Rust Source: https://docs.rs/native-windows-gui/latest/native_windows_gui/struct.WindowBuilder.html This snippet shows the basic setup for creating a window and several buttons using nwg. It includes setting window properties and button texts. ```rust 49 nwg::Window::builder() 50 .size((500, 500)) 51 .position((300, 300)) 52 .title("Flexbox example") 53 .build(&mut data.window)?; 54 55 nwg::Button::builder() 56 .text("Btn 1") 57 .parent(&data.window) 58 .focus(true) 59 .build(&mut data.button1)?; 60 61 nwg::Button::builder() 62 .text("Btn 2") 63 .parent(&data.window) 64 .focus(true) 65 .build(&mut data.button2)?; 66 67 nwg::Button::builder() 68 .text("Btn 3") 69 .parent(&data.window) 70 .focus(true) 71 .build(&mut data.button3)?; ``` -------------------------------- ### Example Usage Source: https://docs.rs/native-windows-gui/latest/native_windows_gui/struct.Notice.html Illustrates how to build and use the Notice component, including sending signals between threads. ```APIDOC ### Example ```rust use native_windows_gui as nwg; fn build_notice(notice: &mut nwg::Notice, window: &nwg::Window) { nwg::Notice::builder() .parent(window) .build(notice); } ``` ### Example with Threading ```rust use native_windows_gui as nwg; use std::thread; use std::time; fn notice(noticer: &nwg::Notice) { let sender = noticer.sender(); thread::spawn(move || { thread::sleep(time::Duration::new(5, 0)); sender.notice(); }); } ``` ``` -------------------------------- ### Flexbox Layout Example Source: https://docs.rs/native-windows-gui/latest/native_windows_gui/struct.FlexboxLayoutBuilder.html An example demonstrating the usage of FlexboxLayout for arranging UI elements. ```APIDOC ## Example Usage of FlexboxLayout ### Description This example shows how to create a window with buttons arranged using a FlexboxLayout, configuring properties like direction, padding, margins, and child sizing. ### Method `nwg::FlexboxLayout::builder()` ### Endpoint N/A (This is a code example, not an API endpoint) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```rust use nwg::stretch::geometry::{Size, Rect}; use nwg::stretch::style::{Dimension as D, FlexDirection, AlignSelf}; const FIFTY_PC: D = D::Percent(0.5); const PT_10: D = D::Points(10.0); const PT_5: D = D::Points(5.0); const PADDING: Rect = Rect{ start: PT_10, end: PT_10, top: PT_10, bottom: PT_10 }; const MARGIN: Rect = Rect{ start: PT_5, end: PT_5, top: PT_5, bottom: PT_5 }; nwg::FlexboxLayout::builder() .parent(&ui.window) .flex_direction(FlexDirection::Row) .padding(PADDING) .child(&ui.button1) .child_margin(MARGIN) .child_max_size(Size { width: D::Points(200.0), height: D::Undefined }) .child_size(Size { width: FIFTY_PC, height: D::Auto }) .child(&ui.button2) .child_margin(MARGIN) .child_align_self(AlignSelf::FlexEnd) .child_size(Size { width: D::Percent(0.25), height: FIFTY_PC }) .child(&ui.button3) .child_margin(MARGIN) .child_flex_grow(2.0) .child_size(Size { width: D::Auto, height: D::Auto }) .build(&ui.layout)?; ``` ### Response #### Success Response (200) None #### Response Example None ``` -------------------------------- ### Build RichLabel Example Source: https://docs.rs/native-windows-gui/latest/native_windows_gui/struct.RichLabel.html Demonstrates how to build and initialize a RichLabel with specified text, font, and parent window. Ensure the 'rich-textbox' feature is enabled for RichLabel functionality. ```rust use native_windows_gui as nwg; fn build_label(label: &mut nwg::RichLabel, window: &nwg::Window, font: &nwg::Font) { nwg::RichLabel::builder() .text("Hello") .font(Some(font)) .parent(window) .build(label); } ``` -------------------------------- ### Initialize and Run Native Windows GUI Source: https://docs.rs/native-windows-gui/latest/src/system_tray/system_tray.rs.html Initializes the GUI framework, builds the system tray UI, and starts the event dispatch loop. ```rust nwg::init().expect("Failed to init Native Windows GUI"); let _ui = SystemTray::build_ui(Default::default()).expect("Failed to build UI"); nwg::dispatch_thread_events(); } ``` -------------------------------- ### POST /build Source: https://docs.rs/native-windows-gui/latest/native_windows_gui/struct.WindowBuilder.html Finalizes the configuration and instantiates the window control. ```APIDOC ## build ### Description Consumes the WindowBuilder and builds the window into the provided output reference. ### Method build ### Parameters - **out** (&mut Window) - Required - The mutable reference to the Window instance to be initialized. ### Response - **Result<(), NwgError>** - Returns Ok(()) on success, or an NwgError if the build fails. ``` -------------------------------- ### Initialize and Run NWG Application Source: https://docs.rs/native-windows-gui/latest/src/message_bank/message_bank.rs.html Initializes the GUI framework, sets the global font, and starts the event loop. ```rust fn main() { nwg::init().expect("Failed to init Native Windows GUI"); nwg::Font::set_global_family("Segoe UI").expect("Failed to set default font"); let _ui = MessageBank::build_ui(Default::default()).expect("Failed to build UI"); nwg::dispatch_thread_events(); } ``` -------------------------------- ### Build Flexbox UI with NWG Source: https://docs.rs/native-windows-gui/latest/native_windows_gui/struct.FlexboxLayoutBuilder.html This example demonstrates building a UI with NWG, including creating windows, buttons, and setting up event handling. It then configures a FlexboxLayout to arrange these buttons. ```rust fn build_ui(mut data: FlexBoxApp) -> Result { use nwg::Event as E; // Controls nwg::Window::builder() .size((500, 300)) .position((300, 300)) .title("Flexbox example") .build(&mut data.window)?; nwg::Button::builder() .text("Btn 1") .parent(&data.window) .focus(true) .build(&mut data.button1)?; nwg::Button::builder() .text("Btn 2") .parent(&data.window) .build(&mut data.button2)?; nwg::Button::builder() .text("Btn 3") .parent(&data.window) .build(&mut data.button3)?; // Wrap-up let ui =FlexBoxAppUi { inner: Rc::new(data), default_handler: Default::default(), }; // Events let evt_ui = Rc::downgrade(&ui.inner); let handle_events = move |evt, _evt_data, handle| { if let Some(evt_ui) = evt_ui.upgrade() { match evt { E::OnWindowClose => if &handle == &evt_ui.window { FlexBoxApp::exit(&evt_ui); }, _ => {} } } }; *ui.default_handler.borrow_mut() = Some(nwg::full_bind_event_handler(&ui.window.handle, handle_events)); // Layout use nwg::stretch::{geometry::{Size, Rect}, style::{Dimension as D, FlexDirection, AlignSelf}}; const FIFTY_PC: D = D::Percent(0.5); const PT_10: D = D::Points(10.0); const PT_5: D = D::Points(5.0); const PADDING: Rect = Rect{ start: PT_10, end: PT_10, top: PT_10, bottom: PT_10 }; const MARGIN: Rect = Rect{ start: PT_5, end: PT_5, top: PT_5, bottom: PT_5 }; nwg::FlexboxLayout::builder() .parent(&ui.window) .flex_direction(FlexDirection::Row) .padding(PADDING) .child(&ui.button1) .child_margin(MARGIN) .child_max_size(Size { width: D::Points(200.0), height: D::Undefined }) .child_size(Size { width: FIFTY_PC, height: D::Auto }) .child(&ui.button2) .child_margin(MARGIN) .child_align_self(AlignSelf::FlexEnd) .child_size(Size { width: D::Percent(0.25), height: FIFTY_PC }) .child(&ui.button3) .child_margin(MARGIN) .child_flex_grow(2.0) .child_size(Size { width: D::Auto, height: D::Auto }) .build(&ui.layout)?; return Ok(ui); } ``` -------------------------------- ### Get Window Long (32-bit) Source: https://docs.rs/native-windows-gui/latest/src/native_windows_gui/win32/window_helper.rs.html Platform-specific helper to get a window's long value on 32-bit systems. ```rust #[inline(always)] #[cfg(target_pointer_width = "32")] pub fn get_window_long(handle: HWND, index: c_int) -> LONG { unsafe { ::winapi::um::winuser::GetWindowLongW(handle, index) } } ``` -------------------------------- ### Build StatusBar with Text and Font Source: https://docs.rs/native-windows-gui/latest/native_windows_gui/struct.StatusBar.html Example of how to build and configure a StatusBar with initial text, a font, and a parent window. Ensure the 'status-bar' feature is enabled. ```rust use native_windows_gui as nwg; fn build_status(status: &mut nwg::StatusBar, window: &nwg::Window, font: &nwg::Font) { nwg::StatusBar::builder() .text("Hello") .font(Some(font)) .parent(window) .build(status); } ``` -------------------------------- ### AnimationTimer Start Method Source: https://docs.rs/native-windows-gui/latest/native_windows_gui/struct.AnimationTimer.html Starts the AnimationTimer. If the timer is already running, this action resets its lifetime and tick count. ```rust pub fn start(&self) ```