Native Windows GUI: Dynamic events
Just like controls, binding new events handler at runtime isn't very hard.
Dynamic binding
Binding event handler at runtime is done using
bind_event_handler
.
full_bind_event_handler
can also be used, but it's overkill as it will bind the handler on all children too.
full_bind_event_handler
can still be used to by the events
of multiple new controls in one take.
let new_button_handle = new_button.handle;
nwg::bind_event_handler(&new_button.handle, &parent_window.handle, move |evt, _evt_data, handle| {
match evt {
nwg::Event::OnButtonClick => {
if handle == new_button_handle {
nwg::simple_message(&title, &content);
}
},
_ => {}
}
});
As mentionned in the
Events section,
bind_event_handler
also takes the parent control handle as argument as most control sends their
events to the parent.
Another thing to keep in mind, the handler callback is not aware of the controls in the application, as such it can only send the handle.
In order to correctly dispatch the events, the handle of the controls must be moved to the handler callback and then the handle must be compared
to the one sent in the callback.
if handle == new_button_handle { ... }
Freeing events handler
Unlike events freeing, which is done automatically when the controls go out of scope, events handler must be manually freed by calling the
nwg::unbind_event_handler(&handler)
method.
Example