Advertisement
koronabora

Untitled

Apr 25th, 2020
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 KB | None | 0 0
  1. #pragma once
  2. #include <nana/gui.hpp>
  3. #include <nana/gui/widgets/menubar.hpp>
  4. #include <nana/gui/widgets/textbox.hpp>
  5. #include <nana/gui/place.hpp>
  6. #include <nana/gui/msgbox.hpp>
  7. #include <nana/gui/filebox.hpp>
  8. #include <nana/gui/widgets/button.hpp>
  9. #include <nana/gui/widgets/progress.hpp>
  10. #include <nana/threads/pool.hpp>
  11. #include <thread>
  12. #include <iostream>
  13. #include <atomic>
  14.  
  15. using namespace nana;
  16. class MainWindow : public form
  17. {
  18. public:
  19.     MainWindow();
  20.     ~MainWindow();
  21.     void wait(unsigned int wait = 0);
  22. private:
  23.     void mainLoop();
  24.  
  25.     place   place{ *this };
  26.     textbox textBox{ *this };
  27.     button startBtn{ *this };
  28.     progress progressBar{ *this };
  29.     threads::pool pool_;
  30.     std::atomic_bool flag = true;
  31.     std::atomic_bool started = false;
  32. };
  33.  
  34. #include "MainWindow.h"
  35.  
  36. MainWindow::MainWindow()
  37. {
  38.     caption("Simple Game");
  39.    
  40.     textBox.borderless(true);
  41.     textBox.editable(false);
  42.     API::effects_edge_nimbus(textBox, effects::edge_nimbus::none);
  43.     API::window_size(*this, nana::size(400, 450));
  44.     place.div("vert<textbox height=400><btn height=20><progress height=20>");
  45.     place["textbox"] << textBox;
  46.     place["btn"] << startBtn;
  47.     place["progress"] << progressBar;
  48.     place.collocate();
  49.  
  50.     startBtn.caption("Start");
  51.     startBtn.events().click(nana::threads::pool_push(pool_, *this, &MainWindow::mainLoop));
  52.  
  53.     auto window_handle = this->handle();
  54.     textBox.events().key_press([&](const nana::arg_keyboard&arg)
  55.     {
  56.         textBox.reset("");
  57.         textBox.append(std::wstring(L"Key pressed: ") + std::wstring(&arg.key) + L"\n", true);
  58.     });
  59. }
  60.  
  61. MainWindow::~MainWindow()
  62. {
  63.     flag = false;
  64.     pool_.wait_for_finished();
  65. }
  66.  
  67. void MainWindow::mainLoop()
  68. {
  69.     if (!started)
  70.     {
  71.         started = true;
  72.         startBtn.caption("Stop");
  73.         while (flag)
  74.         {
  75.             progressBar.inc();
  76.             if (progressBar.value() >= 100)
  77.                 progressBar.value(0);
  78.             wait(100);
  79.         }
  80.         startBtn.caption("Start");
  81.         flag = true;
  82.     }
  83.     else
  84.     {
  85.         started = false;
  86.         flag = false;
  87.     }
  88. }
  89.  
  90. void MainWindow::wait(unsigned int wait)
  91. {
  92.         std::this_thread::sleep_for(std::chrono::milliseconds{ wait });
  93. }
  94.  
  95. #include "MainWindow.h"
  96.  
  97. int main()
  98. {
  99.     MainWindow window;
  100.     window.show();
  101.  
  102.     exec();
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement