Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. /**
  2. Simple example on how to use the nwg template system.
  3. */
  4.  
  5. #[macro_use] extern crate native_windows_gui as nwg;
  6.  
  7. use nwg::{Event, Ui, simple_message, fatal_message, dispatch_events};
  8.  
  9. /// Custom enums are the preferred way to define ui ids. It's clearer and more extensible than any other types (such as &'str).
  10. #[derive(Debug, Clone, Hash)]
  11. pub enum AppId {
  12. // Controls
  13. MainWindow,
  14. NameInput,
  15. HelloButton,
  16. Label(u8), // Ids for static controls that won't be referenced in the Ui logic can be shortened this way.
  17.  
  18. // Events
  19. SayHello,
  20.  
  21. // Resources
  22. MainFont,
  23. TextFont
  24. }
  25.  
  26. use AppId::*; // Shortcut
  27.  
  28. nwg_template!(
  29. head: setup_ui<AppId>,
  30. controls: [
  31. (MainWindow, nwg_window!( title="Template Example"; size=(280, 105) )),
  32. (Label(0), nwg_label!(
  33. parent=MainWindow;
  34.  
  35. // Как изменить text= в процессе выполнения программы?
  36. // Или хотя бы вначале, присвоить значение переменной, которая будет найдена в fn main(){}?
  37. text="Your Name: ";
  38. position=(5,15); size=(80, 25);
  39. font=Some(TextFont) )),
  40.  
  41. (NameInput, nwg_textinput!(
  42. parent=MainWindow;
  43. position=(85,13); size=(185,22);
  44. font=Some(TextFont) )),
  45.  
  46. (HelloButton, nwg_button!(
  47. parent=MainWindow;
  48. text="Hello World!";
  49. position=(5, 45); size=(270, 50);
  50. font=Some(MainFont) ))
  51. ];
  52. events: [
  53. (HelloButton, SayHello, Event::Click, |ui,_,_,_| {
  54. let your_name = nwg_get!(ui; (NameInput, nwg::TextInput));
  55. simple_message("Hello", &format!("Hello {}!", your_name.get_text()) );
  56. })
  57. ];
  58. resources: [
  59. (MainFont, nwg_font!(family="Arial"; size=27)),
  60. (TextFont, nwg_font!(family="Arial"; size=17))
  61. ];
  62. values: []
  63. );
  64.  
  65. fn main() {
  66. let app: Ui<AppId>;
  67.  
  68. match Ui::new() {
  69. Ok(_app) => { app = _app; },
  70. Err(e) => { fatal_message("Fatal Error", &format!("{:?}", e) ); }
  71. }
  72.  
  73. if let Err(e) = setup_ui(&app) {
  74. fatal_message("Fatal Error", &format!("{:?}", e));
  75. }
  76.  
  77. dispatch_events();
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement