pub struct Button(/* private fields */);
gui
only.Expand description
Native button control.
§Examples
Basic structure of a program with a main window and a button, both created programmatically:
use winsafe::{self as w, co, gui, prelude::*};
fn main() {
if let Err(err) = Main::create_and_run() {
w::HWND::NULL
.MessageBox(&err.to_string(), "Uncaught error", co::MB::ICONERROR)
.unwrap();
}
}
#[derive(Clone)]
struct Main {
wnd: gui::WindowMain,
btn: gui::Button,
}
impl Main {
#[must_use]
fn create_and_run() -> w::AnyResult<i32> {
let wnd = gui::WindowMain::new(gui::WindowMainOpts {
title: "Main window".to_owned(),
..Default::default()
});
let btn = gui::Button::new(
&wnd,
gui::ButtonOpts {
text: "&Click me".to_owned(),
position: gui::dpi(20, 20),
..Default::default()
},
);
let new_self = Self { wnd, btn };
new_self.events();
new_self.wnd.run_main(None)
}
fn events(&self) {
let self2 = self.clone();
self.btn.on().bn_clicked(move || {
self2.wnd.hwnd().SetWindowText("Button clicked")?;
Ok(())
});
}
}
Implementations§
Source§impl Button
impl Button
Sourcepub fn on_subclass(&self) -> &WindowEvents
pub fn on_subclass(&self) -> &WindowEvents
Exposes the subclass events. If at least one event exists, the control will be subclassed.
Note: Subclassing may impact performance, use with care.
§Panics
Panics if the control or the parent window are already created. Events must be set before control and parent window creation.
Sourcepub fn on(&self) -> &ButtonEvents
pub fn on(&self) -> &ButtonEvents
Exposes the specific control events.
§Panics
Panics if the control is already created. Events must be set before control creation.
Source§impl Button
impl Button
Sourcepub fn new(parent: &(impl GuiParent + 'static), opts: ButtonOpts) -> Self
pub fn new(parent: &(impl GuiParent + 'static), opts: ButtonOpts) -> Self
Instantiates a new Button
object, to be created on the parent window
with HWND::CreateWindowEx
.
§Panics
Panics if the parent window was already created – that is, you cannot
dynamically create a Button
in an event closure.
Sourcepub fn new_dlg(
parent: &(impl GuiParent + 'static),
ctrl_id: u16,
resize_behavior: (Horz, Vert),
) -> Self
pub fn new_dlg( parent: &(impl GuiParent + 'static), ctrl_id: u16, resize_behavior: (Horz, Vert), ) -> Self
Instantiates a new Button
object, to be loaded from a dialog resource
with HWND::GetDlgItem
.
§Panics
Panics if the parent dialog was already created – that is, you cannot
dynamically create a Button
in an event closure.
Sourcepub fn trigger_click(&self)
pub fn trigger_click(&self)
Fires the click event for the button by sending a
bm::Click
message.