πŸ“œ ⬆️ ⬇️

We write screensaver on wxWidgets

Foreword


This material does not in any way encourage the reader to write screensavers, but is intended primarily to review some of the features of the wxWidgets library. Those who are not familiar with wxWidgets, but want to learn more, you can read articles on CodeProject.com ( this and this )

Brain is what we need


So, I have written many times about how to build wxWidgets and how to create an application using this library, so let's start right away with an example. To begin, we need a minimal application with one form. This will be the skeleton for creating a screensaver.
We will not invent the bicycle today either, but take as an illustration the simplest screensaver with running symbols Γ  la Matrix.

The main objects that is displayed on the screen, we have a symbol. Each character is characterized by the following parameters:

To store information about the symbol, create the following class:
class MatrixSymbol { wxPoint m_Position; wxChar m_Symbol; int m_Speed; wxColour m_Colour; public : MatrixSymbol() : m_Position(wxDefaultPosition), m_Symbol(wxT( '0' )), m_Speed(1), m_Colour(*wxGREEN) {} MatrixSymbol( const wxPoint & position, wxChar symbol, int speed, const wxColour & colour) : m_Position(position), m_Symbol(symbol), m_Speed(speed), m_Colour(colour) {} const wxPoint & GetPosition() { return m_Position;} void SetPosition( const wxPoint & value ) {m_Position = value ;} wxChar GetSymbol() { return m_Symbol;} void SetSymbol(wxChar value ) {m_Symbol = value ;} int GetSpeed() { return m_Speed;} void SetSpeed( int value ) {m_Speed = value ;} const wxColour & GetColour() { return m_Colour;} void SetColour( const wxColour & value ) {m_Colour = value ;} }; * This source code was highlighted with Source Code Highlighter .
  1. class MatrixSymbol { wxPoint m_Position; wxChar m_Symbol; int m_Speed; wxColour m_Colour; public : MatrixSymbol() : m_Position(wxDefaultPosition), m_Symbol(wxT( '0' )), m_Speed(1), m_Colour(*wxGREEN) {} MatrixSymbol( const wxPoint & position, wxChar symbol, int speed, const wxColour & colour) : m_Position(position), m_Symbol(symbol), m_Speed(speed), m_Colour(colour) {} const wxPoint & GetPosition() { return m_Position;} void SetPosition( const wxPoint & value ) {m_Position = value ;} wxChar GetSymbol() { return m_Symbol;} void SetSymbol(wxChar value ) {m_Symbol = value ;} int GetSpeed() { return m_Speed;} void SetSpeed( int value ) {m_Speed = value ;} const wxColour & GetColour() { return m_Colour;} void SetColour( const wxColour & value ) {m_Colour = value ;} }; * This source code was highlighted with Source Code Highlighter .
  2. class MatrixSymbol { wxPoint m_Position; wxChar m_Symbol; int m_Speed; wxColour m_Colour; public : MatrixSymbol() : m_Position(wxDefaultPosition), m_Symbol(wxT( '0' )), m_Speed(1), m_Colour(*wxGREEN) {} MatrixSymbol( const wxPoint & position, wxChar symbol, int speed, const wxColour & colour) : m_Position(position), m_Symbol(symbol), m_Speed(speed), m_Colour(colour) {} const wxPoint & GetPosition() { return m_Position;} void SetPosition( const wxPoint & value ) {m_Position = value ;} wxChar GetSymbol() { return m_Symbol;} void SetSymbol(wxChar value ) {m_Symbol = value ;} int GetSpeed() { return m_Speed;} void SetSpeed( int value ) {m_Speed = value ;} const wxColour & GetColour() { return m_Colour;} void SetColour( const wxColour & value ) {m_Colour = value ;} }; * This source code was highlighted with Source Code Highlighter .
  3. class MatrixSymbol { wxPoint m_Position; wxChar m_Symbol; int m_Speed; wxColour m_Colour; public : MatrixSymbol() : m_Position(wxDefaultPosition), m_Symbol(wxT( '0' )), m_Speed(1), m_Colour(*wxGREEN) {} MatrixSymbol( const wxPoint & position, wxChar symbol, int speed, const wxColour & colour) : m_Position(position), m_Symbol(symbol), m_Speed(speed), m_Colour(colour) {} const wxPoint & GetPosition() { return m_Position;} void SetPosition( const wxPoint & value ) {m_Position = value ;} wxChar GetSymbol() { return m_Symbol;} void SetSymbol(wxChar value ) {m_Symbol = value ;} int GetSpeed() { return m_Speed;} void SetSpeed( int value ) {m_Speed = value ;} const wxColour & GetColour() { return m_Colour;} void SetColour( const wxColour & value ) {m_Colour = value ;} }; * This source code was highlighted with Source Code Highlighter .
  4. class MatrixSymbol { wxPoint m_Position; wxChar m_Symbol; int m_Speed; wxColour m_Colour; public : MatrixSymbol() : m_Position(wxDefaultPosition), m_Symbol(wxT( '0' )), m_Speed(1), m_Colour(*wxGREEN) {} MatrixSymbol( const wxPoint & position, wxChar symbol, int speed, const wxColour & colour) : m_Position(position), m_Symbol(symbol), m_Speed(speed), m_Colour(colour) {} const wxPoint & GetPosition() { return m_Position;} void SetPosition( const wxPoint & value ) {m_Position = value ;} wxChar GetSymbol() { return m_Symbol;} void SetSymbol(wxChar value ) {m_Symbol = value ;} int GetSpeed() { return m_Speed;} void SetSpeed( int value ) {m_Speed = value ;} const wxColour & GetColour() { return m_Colour;} void SetColour( const wxColour & value ) {m_Colour = value ;} }; * This source code was highlighted with Source Code Highlighter .
  5. class MatrixSymbol { wxPoint m_Position; wxChar m_Symbol; int m_Speed; wxColour m_Colour; public : MatrixSymbol() : m_Position(wxDefaultPosition), m_Symbol(wxT( '0' )), m_Speed(1), m_Colour(*wxGREEN) {} MatrixSymbol( const wxPoint & position, wxChar symbol, int speed, const wxColour & colour) : m_Position(position), m_Symbol(symbol), m_Speed(speed), m_Colour(colour) {} const wxPoint & GetPosition() { return m_Position;} void SetPosition( const wxPoint & value ) {m_Position = value ;} wxChar GetSymbol() { return m_Symbol;} void SetSymbol(wxChar value ) {m_Symbol = value ;} int GetSpeed() { return m_Speed;} void SetSpeed( int value ) {m_Speed = value ;} const wxColour & GetColour() { return m_Colour;} void SetColour( const wxColour & value ) {m_Colour = value ;} }; * This source code was highlighted with Source Code Highlighter .
  6. class MatrixSymbol { wxPoint m_Position; wxChar m_Symbol; int m_Speed; wxColour m_Colour; public : MatrixSymbol() : m_Position(wxDefaultPosition), m_Symbol(wxT( '0' )), m_Speed(1), m_Colour(*wxGREEN) {} MatrixSymbol( const wxPoint & position, wxChar symbol, int speed, const wxColour & colour) : m_Position(position), m_Symbol(symbol), m_Speed(speed), m_Colour(colour) {} const wxPoint & GetPosition() { return m_Position;} void SetPosition( const wxPoint & value ) {m_Position = value ;} wxChar GetSymbol() { return m_Symbol;} void SetSymbol(wxChar value ) {m_Symbol = value ;} int GetSpeed() { return m_Speed;} void SetSpeed( int value ) {m_Speed = value ;} const wxColour & GetColour() { return m_Colour;} void SetColour( const wxColour & value ) {m_Colour = value ;} }; * This source code was highlighted with Source Code Highlighter .
  7. class MatrixSymbol { wxPoint m_Position; wxChar m_Symbol; int m_Speed; wxColour m_Colour; public : MatrixSymbol() : m_Position(wxDefaultPosition), m_Symbol(wxT( '0' )), m_Speed(1), m_Colour(*wxGREEN) {} MatrixSymbol( const wxPoint & position, wxChar symbol, int speed, const wxColour & colour) : m_Position(position), m_Symbol(symbol), m_Speed(speed), m_Colour(colour) {} const wxPoint & GetPosition() { return m_Position;} void SetPosition( const wxPoint & value ) {m_Position = value ;} wxChar GetSymbol() { return m_Symbol;} void SetSymbol(wxChar value ) {m_Symbol = value ;} int GetSpeed() { return m_Speed;} void SetSpeed( int value ) {m_Speed = value ;} const wxColour & GetColour() { return m_Colour;} void SetColour( const wxColour & value ) {m_Colour = value ;} }; * This source code was highlighted with Source Code Highlighter .
  8. class MatrixSymbol { wxPoint m_Position; wxChar m_Symbol; int m_Speed; wxColour m_Colour; public : MatrixSymbol() : m_Position(wxDefaultPosition), m_Symbol(wxT( '0' )), m_Speed(1), m_Colour(*wxGREEN) {} MatrixSymbol( const wxPoint & position, wxChar symbol, int speed, const wxColour & colour) : m_Position(position), m_Symbol(symbol), m_Speed(speed), m_Colour(colour) {} const wxPoint & GetPosition() { return m_Position;} void SetPosition( const wxPoint & value ) {m_Position = value ;} wxChar GetSymbol() { return m_Symbol;} void SetSymbol(wxChar value ) {m_Symbol = value ;} int GetSpeed() { return m_Speed;} void SetSpeed( int value ) {m_Speed = value ;} const wxColour & GetColour() { return m_Colour;} void SetColour( const wxColour & value ) {m_Colour = value ;} }; * This source code was highlighted with Source Code Highlighter .
  9. class MatrixSymbol { wxPoint m_Position; wxChar m_Symbol; int m_Speed; wxColour m_Colour; public : MatrixSymbol() : m_Position(wxDefaultPosition), m_Symbol(wxT( '0' )), m_Speed(1), m_Colour(*wxGREEN) {} MatrixSymbol( const wxPoint & position, wxChar symbol, int speed, const wxColour & colour) : m_Position(position), m_Symbol(symbol), m_Speed(speed), m_Colour(colour) {} const wxPoint & GetPosition() { return m_Position;} void SetPosition( const wxPoint & value ) {m_Position = value ;} wxChar GetSymbol() { return m_Symbol;} void SetSymbol(wxChar value ) {m_Symbol = value ;} int GetSpeed() { return m_Speed;} void SetSpeed( int value ) {m_Speed = value ;} const wxColour & GetColour() { return m_Colour;} void SetColour( const wxColour & value ) {m_Colour = value ;} }; * This source code was highlighted with Source Code Highlighter .
  10. class MatrixSymbol { wxPoint m_Position; wxChar m_Symbol; int m_Speed; wxColour m_Colour; public : MatrixSymbol() : m_Position(wxDefaultPosition), m_Symbol(wxT( '0' )), m_Speed(1), m_Colour(*wxGREEN) {} MatrixSymbol( const wxPoint & position, wxChar symbol, int speed, const wxColour & colour) : m_Position(position), m_Symbol(symbol), m_Speed(speed), m_Colour(colour) {} const wxPoint & GetPosition() { return m_Position;} void SetPosition( const wxPoint & value ) {m_Position = value ;} wxChar GetSymbol() { return m_Symbol;} void SetSymbol(wxChar value ) {m_Symbol = value ;} int GetSpeed() { return m_Speed;} void SetSpeed( int value ) {m_Speed = value ;} const wxColour & GetColour() { return m_Colour;} void SetColour( const wxColour & value ) {m_Colour = value ;} }; * This source code was highlighted with Source Code Highlighter .
  11. class MatrixSymbol { wxPoint m_Position; wxChar m_Symbol; int m_Speed; wxColour m_Colour; public : MatrixSymbol() : m_Position(wxDefaultPosition), m_Symbol(wxT( '0' )), m_Speed(1), m_Colour(*wxGREEN) {} MatrixSymbol( const wxPoint & position, wxChar symbol, int speed, const wxColour & colour) : m_Position(position), m_Symbol(symbol), m_Speed(speed), m_Colour(colour) {} const wxPoint & GetPosition() { return m_Position;} void SetPosition( const wxPoint & value ) {m_Position = value ;} wxChar GetSymbol() { return m_Symbol;} void SetSymbol(wxChar value ) {m_Symbol = value ;} int GetSpeed() { return m_Speed;} void SetSpeed( int value ) {m_Speed = value ;} const wxColour & GetColour() { return m_Colour;} void SetColour( const wxColour & value ) {m_Colour = value ;} }; * This source code was highlighted with Source Code Highlighter .
  12. class MatrixSymbol { wxPoint m_Position; wxChar m_Symbol; int m_Speed; wxColour m_Colour; public : MatrixSymbol() : m_Position(wxDefaultPosition), m_Symbol(wxT( '0' )), m_Speed(1), m_Colour(*wxGREEN) {} MatrixSymbol( const wxPoint & position, wxChar symbol, int speed, const wxColour & colour) : m_Position(position), m_Symbol(symbol), m_Speed(speed), m_Colour(colour) {} const wxPoint & GetPosition() { return m_Position;} void SetPosition( const wxPoint & value ) {m_Position = value ;} wxChar GetSymbol() { return m_Symbol;} void SetSymbol(wxChar value ) {m_Symbol = value ;} int GetSpeed() { return m_Speed;} void SetSpeed( int value ) {m_Speed = value ;} const wxColour & GetColour() { return m_Colour;} void SetColour( const wxColour & value ) {m_Colour = value ;} }; * This source code was highlighted with Source Code Highlighter .
  13. class MatrixSymbol { wxPoint m_Position; wxChar m_Symbol; int m_Speed; wxColour m_Colour; public : MatrixSymbol() : m_Position(wxDefaultPosition), m_Symbol(wxT( '0' )), m_Speed(1), m_Colour(*wxGREEN) {} MatrixSymbol( const wxPoint & position, wxChar symbol, int speed, const wxColour & colour) : m_Position(position), m_Symbol(symbol), m_Speed(speed), m_Colour(colour) {} const wxPoint & GetPosition() { return m_Position;} void SetPosition( const wxPoint & value ) {m_Position = value ;} wxChar GetSymbol() { return m_Symbol;} void SetSymbol(wxChar value ) {m_Symbol = value ;} int GetSpeed() { return m_Speed;} void SetSpeed( int value ) {m_Speed = value ;} const wxColour & GetColour() { return m_Colour;} void SetColour( const wxColour & value ) {m_Colour = value ;} }; * This source code was highlighted with Source Code Highlighter .
  14. class MatrixSymbol { wxPoint m_Position; wxChar m_Symbol; int m_Speed; wxColour m_Colour; public : MatrixSymbol() : m_Position(wxDefaultPosition), m_Symbol(wxT( '0' )), m_Speed(1), m_Colour(*wxGREEN) {} MatrixSymbol( const wxPoint & position, wxChar symbol, int speed, const wxColour & colour) : m_Position(position), m_Symbol(symbol), m_Speed(speed), m_Colour(colour) {} const wxPoint & GetPosition() { return m_Position;} void SetPosition( const wxPoint & value ) {m_Position = value ;} wxChar GetSymbol() { return m_Symbol;} void SetSymbol(wxChar value ) {m_Symbol = value ;} int GetSpeed() { return m_Speed;} void SetSpeed( int value ) {m_Speed = value ;} const wxColour & GetColour() { return m_Colour;} void SetColour( const wxColour & value ) {m_Colour = value ;} }; * This source code was highlighted with Source Code Highlighter .
  15. class MatrixSymbol { wxPoint m_Position; wxChar m_Symbol; int m_Speed; wxColour m_Colour; public : MatrixSymbol() : m_Position(wxDefaultPosition), m_Symbol(wxT( '0' )), m_Speed(1), m_Colour(*wxGREEN) {} MatrixSymbol( const wxPoint & position, wxChar symbol, int speed, const wxColour & colour) : m_Position(position), m_Symbol(symbol), m_Speed(speed), m_Colour(colour) {} const wxPoint & GetPosition() { return m_Position;} void SetPosition( const wxPoint & value ) {m_Position = value ;} wxChar GetSymbol() { return m_Symbol;} void SetSymbol(wxChar value ) {m_Symbol = value ;} int GetSpeed() { return m_Speed;} void SetSpeed( int value ) {m_Speed = value ;} const wxColour & GetColour() { return m_Colour;} void SetColour( const wxColour & value ) {m_Colour = value ;} }; * This source code was highlighted with Source Code Highlighter .
  16. class MatrixSymbol { wxPoint m_Position; wxChar m_Symbol; int m_Speed; wxColour m_Colour; public : MatrixSymbol() : m_Position(wxDefaultPosition), m_Symbol(wxT( '0' )), m_Speed(1), m_Colour(*wxGREEN) {} MatrixSymbol( const wxPoint & position, wxChar symbol, int speed, const wxColour & colour) : m_Position(position), m_Symbol(symbol), m_Speed(speed), m_Colour(colour) {} const wxPoint & GetPosition() { return m_Position;} void SetPosition( const wxPoint & value ) {m_Position = value ;} wxChar GetSymbol() { return m_Symbol;} void SetSymbol(wxChar value ) {m_Symbol = value ;} int GetSpeed() { return m_Speed;} void SetSpeed( int value ) {m_Speed = value ;} const wxColour & GetColour() { return m_Colour;} void SetColour( const wxColour & value ) {m_Colour = value ;} }; * This source code was highlighted with Source Code Highlighter .
  17. class MatrixSymbol { wxPoint m_Position; wxChar m_Symbol; int m_Speed; wxColour m_Colour; public : MatrixSymbol() : m_Position(wxDefaultPosition), m_Symbol(wxT( '0' )), m_Speed(1), m_Colour(*wxGREEN) {} MatrixSymbol( const wxPoint & position, wxChar symbol, int speed, const wxColour & colour) : m_Position(position), m_Symbol(symbol), m_Speed(speed), m_Colour(colour) {} const wxPoint & GetPosition() { return m_Position;} void SetPosition( const wxPoint & value ) {m_Position = value ;} wxChar GetSymbol() { return m_Symbol;} void SetSymbol(wxChar value ) {m_Symbol = value ;} int GetSpeed() { return m_Speed;} void SetSpeed( int value ) {m_Speed = value ;} const wxColour & GetColour() { return m_Colour;} void SetColour( const wxColour & value ) {m_Colour = value ;} }; * This source code was highlighted with Source Code Highlighter .
  18. class MatrixSymbol { wxPoint m_Position; wxChar m_Symbol; int m_Speed; wxColour m_Colour; public : MatrixSymbol() : m_Position(wxDefaultPosition), m_Symbol(wxT( '0' )), m_Speed(1), m_Colour(*wxGREEN) {} MatrixSymbol( const wxPoint & position, wxChar symbol, int speed, const wxColour & colour) : m_Position(position), m_Symbol(symbol), m_Speed(speed), m_Colour(colour) {} const wxPoint & GetPosition() { return m_Position;} void SetPosition( const wxPoint & value ) {m_Position = value ;} wxChar GetSymbol() { return m_Symbol;} void SetSymbol(wxChar value ) {m_Symbol = value ;} int GetSpeed() { return m_Speed;} void SetSpeed( int value ) {m_Speed = value ;} const wxColour & GetColour() { return m_Colour;} void SetColour( const wxColour & value ) {m_Colour = value ;} }; * This source code was highlighted with Source Code Highlighter .
  19. class MatrixSymbol { wxPoint m_Position; wxChar m_Symbol; int m_Speed; wxColour m_Colour; public : MatrixSymbol() : m_Position(wxDefaultPosition), m_Symbol(wxT( '0' )), m_Speed(1), m_Colour(*wxGREEN) {} MatrixSymbol( const wxPoint & position, wxChar symbol, int speed, const wxColour & colour) : m_Position(position), m_Symbol(symbol), m_Speed(speed), m_Colour(colour) {} const wxPoint & GetPosition() { return m_Position;} void SetPosition( const wxPoint & value ) {m_Position = value ;} wxChar GetSymbol() { return m_Symbol;} void SetSymbol(wxChar value ) {m_Symbol = value ;} int GetSpeed() { return m_Speed;} void SetSpeed( int value ) {m_Speed = value ;} const wxColour & GetColour() { return m_Colour;} void SetColour( const wxColour & value ) {m_Colour = value ;} }; * This source code was highlighted with Source Code Highlighter .
  20. class MatrixSymbol { wxPoint m_Position; wxChar m_Symbol; int m_Speed; wxColour m_Colour; public : MatrixSymbol() : m_Position(wxDefaultPosition), m_Symbol(wxT( '0' )), m_Speed(1), m_Colour(*wxGREEN) {} MatrixSymbol( const wxPoint & position, wxChar symbol, int speed, const wxColour & colour) : m_Position(position), m_Symbol(symbol), m_Speed(speed), m_Colour(colour) {} const wxPoint & GetPosition() { return m_Position;} void SetPosition( const wxPoint & value ) {m_Position = value ;} wxChar GetSymbol() { return m_Symbol;} void SetSymbol(wxChar value ) {m_Symbol = value ;} int GetSpeed() { return m_Speed;} void SetSpeed( int value ) {m_Speed = value ;} const wxColour & GetColour() { return m_Colour;} void SetColour( const wxColour & value ) {m_Colour = value ;} }; * This source code was highlighted with Source Code Highlighter .
  21. class MatrixSymbol { wxPoint m_Position; wxChar m_Symbol; int m_Speed; wxColour m_Colour; public : MatrixSymbol() : m_Position(wxDefaultPosition), m_Symbol(wxT( '0' )), m_Speed(1), m_Colour(*wxGREEN) {} MatrixSymbol( const wxPoint & position, wxChar symbol, int speed, const wxColour & colour) : m_Position(position), m_Symbol(symbol), m_Speed(speed), m_Colour(colour) {} const wxPoint & GetPosition() { return m_Position;} void SetPosition( const wxPoint & value ) {m_Position = value ;} wxChar GetSymbol() { return m_Symbol;} void SetSymbol(wxChar value ) {m_Symbol = value ;} int GetSpeed() { return m_Speed;} void SetSpeed( int value ) {m_Speed = value ;} const wxColour & GetColour() { return m_Colour;} void SetColour( const wxColour & value ) {m_Colour = value ;} }; * This source code was highlighted with Source Code Highlighter .
  22. class MatrixSymbol { wxPoint m_Position; wxChar m_Symbol; int m_Speed; wxColour m_Colour; public : MatrixSymbol() : m_Position(wxDefaultPosition), m_Symbol(wxT( '0' )), m_Speed(1), m_Colour(*wxGREEN) {} MatrixSymbol( const wxPoint & position, wxChar symbol, int speed, const wxColour & colour) : m_Position(position), m_Symbol(symbol), m_Speed(speed), m_Colour(colour) {} const wxPoint & GetPosition() { return m_Position;} void SetPosition( const wxPoint & value ) {m_Position = value ;} wxChar GetSymbol() { return m_Symbol;} void SetSymbol(wxChar value ) {m_Symbol = value ;} int GetSpeed() { return m_Speed;} void SetSpeed( int value ) {m_Speed = value ;} const wxColour & GetColour() { return m_Colour;} void SetColour( const wxColour & value ) {m_Colour = value ;} }; * This source code was highlighted with Source Code Highlighter .
  23. class MatrixSymbol { wxPoint m_Position; wxChar m_Symbol; int m_Speed; wxColour m_Colour; public : MatrixSymbol() : m_Position(wxDefaultPosition), m_Symbol(wxT( '0' )), m_Speed(1), m_Colour(*wxGREEN) {} MatrixSymbol( const wxPoint & position, wxChar symbol, int speed, const wxColour & colour) : m_Position(position), m_Symbol(symbol), m_Speed(speed), m_Colour(colour) {} const wxPoint & GetPosition() { return m_Position;} void SetPosition( const wxPoint & value ) {m_Position = value ;} wxChar GetSymbol() { return m_Symbol;} void SetSymbol(wxChar value ) {m_Symbol = value ;} int GetSpeed() { return m_Speed;} void SetSpeed( int value ) {m_Speed = value ;} const wxColour & GetColour() { return m_Colour;} void SetColour( const wxColour & value ) {m_Colour = value ;} }; * This source code was highlighted with Source Code Highlighter .
class MatrixSymbol { wxPoint m_Position; wxChar m_Symbol; int m_Speed; wxColour m_Colour; public : MatrixSymbol() : m_Position(wxDefaultPosition), m_Symbol(wxT( '0' )), m_Speed(1), m_Colour(*wxGREEN) {} MatrixSymbol( const wxPoint & position, wxChar symbol, int speed, const wxColour & colour) : m_Position(position), m_Symbol(symbol), m_Speed(speed), m_Colour(colour) {} const wxPoint & GetPosition() { return m_Position;} void SetPosition( const wxPoint & value ) {m_Position = value ;} wxChar GetSymbol() { return m_Symbol;} void SetSymbol(wxChar value ) {m_Symbol = value ;} int GetSpeed() { return m_Speed;} void SetSpeed( int value ) {m_Speed = value ;} const wxColour & GetColour() { return m_Colour;} void SetColour( const wxColour & value ) {m_Colour = value ;} }; * This source code was highlighted with Source Code Highlighter .

We should have a lot of such symbols on the screen and, accordingly, we need an array to store all this stuff:
  1. #include <wx / dynarray.h>
  2. ...
  3. WX_DECLARE_OBJARRAY (MatrixSymbol, MatrixSymbolArray);
  4. ...
  5. #include <wx / arrimpl.cpp>
  6. WX_DEFINE_OBJARRAY (MatrixSymbolArray);
* This source code was highlighted with Source Code Highlighter .

Sculpt graphical interface


Ok, we have finished the preparatory stage, now we can begin to implement the graphical interface.
Create a new component inherited from wxWindow , and add in it as a member variable of the class an array of MatrixSymbol objects, as well as a method initializing this array with values:
  1. class MatrixCanvas: public wxWindow
  2. {
  3. ...
  4. void InitMatrix ();
  5. ...
  6. MatrixSymbolArray m_Symbols;
  7. ...
  8. };
  9. void MatrixCanvas :: InitMatrix ()
  10. {
  11. int width (0), height (0);
  12. int sw (0), sh (0);
  13. GetTextExtent (wxT ( "0" ), & sw, & sh);
  14. GetClientSize (& width, & height);
  15. m_Symbols.Clear ();
  16. for ( int x = 0; x <width; x + = sw + 2)
  17. {
  18. m_Symbols.Add (MatrixSymbol (
  19. wxPoint (x, 0),
  20. rand ()% 2? wxT ( '0' ): wxT ( '1' ),
  21. 3 + rand ()% 5,
  22. wxColour (0, rand ()% 200 + 56, 0)));
  23. }
  24. }
* This source code was highlighted with Source Code Highlighter .

What does the InitMatrix() method InitMatrix() ? Depending on the size of the client area of ​​the component, a number of MatrixSymbol objects are MatrixSymbol with random coordinates, displayed as β€œ0” or β€œ1”, and in different colors (random green gradation is selected).
Now we need to ensure the display of characters on the screen. To do this, create event handlers wxEVT_PAINT and wxEVT_ERASE_BACKGROUND .
  1. BEGIN_EVENT_TABLE (MatrixCanvas, wxWindow)
  2. EVT_PAINT (MatrixCanvas :: OnPaint)
  3. EVT_ERASE_BACKGROUND (MatrixCanvas :: OnEraseBackground)
  4. END_EVENT_TABLE ()
  5. void MatrixCanvas :: OnPaint (wxPaintEvent & event )
  6. {
  7. wxBufferedPaintDC dc ( this );
  8. dc.SetBackground (wxBrush (GetBackgroundColour ()));
  9. dc.Clear ();
  10. wxFont font = GetFont ();
  11. #if defined (__ WXWINCE__)
  12. int fontSize = 14;
  13. #else
  14. int fontSize = 48;
  15. #endif
  16. font.SetPointSize (fontSize);
  17. dc.SetFont (font);
  18. dc.SetTextForeground (wxColour (00, 20, 00));
  19. dc.DrawLabel (wxT ( "http://wxwidgets.info" ),
  20. wxRect (0, 0, dc.GetSize (). GetWidth (), dc.GetSize (). GetHeight ()),
  21. wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL);
  22. dc.SetFont (GetFont ());
  23. for (size_t i = 0; i <m_Symbols.Count (); i ++)
  24. {
  25. dc.SetTextForeground (m_Symbols [i] .GetColour ());
  26. dc.DrawText (wxString :: Format (wxT ( "% c" ), m_Symbols [i] .GetSymbol ()),
  27. m_Symbols [i] .GetPosition ());
  28. }
  29. }
  30. void MatrixCanvas :: OnEraseBackground (wxEraseEvent & event )
  31. {
  32. }
* This source code was highlighted with Source Code Highlighter .

The wxEVT_ERASE_BACKGROUND event wxEVT_ERASE_BACKGROUND empty (without calling event.Skip() ). This will provide us with a redraw component without flicker.
In the wxEVT_PAINT event wxEVT_PAINT , we create a device context, set the background color to the background color of our component, then clean up (this is equivalent to filling with color). After that, an inscription is drawn in the center of the component, and then in the loop, all symbols from the array are drawn.
Next, we need to add the event handler wxEVT_SIZE so that when the component is resized, the characters are displayed across the entire width. In the handler, we will simply call the InitMatrix() method, which fills the array with characters.
  1. BEGIN_EVENT_TABLE (MatrixCanvas, wxWindow)
  2. ...
  3. EVT_SIZE (MatrixCanvas :: OnSize)
  4. END_EVENT_TABLE ()
  5. ...
  6. void MatrixCanvas :: OnSize (wxSizeEvent & event )
  7. {
  8. InitMatrix ();
  9. Refresh ();
  10. }
* This source code was highlighted with Source Code Highlighter .

So, the display of characters we have achieved, but we still need to make the characters move around the screen. The code that moves the characters around the screen is best executed in the event handler for the timer event.
  1. class MatrixCanvas: public wxWindow
  2. {
  3. ...
  4. wxTimer * m_MovementTimer;
  5. };
  6. MatrixCanvas :: MatrixCanvas (wxWindow * parent, wxWindowID id, const wxPoint & pos, const wxSize & size, long style)
  7. {
  8. Init ();
  9. Create (parent, id, pos, size, style);
  10. }
  11. bool MatrixCanvas :: Create (wxWindow * parent, wxWindowID id, const wxPoint & pos, const wxSize & size, long style)
  12. {
  13. wxWindow :: Create (parent, id, pos, size, style);
  14. CreateControls ();
  15. return true ;
  16. }
  17. MatrixCanvas :: ~ MatrixCanvas ()
  18. {
  19. wxDELETE (m_MovementTimer);
  20. }
  21. void MatrixCanvas :: Init ()
  22. {
  23. m_PreviewMode = false ;
  24. }
  25. void MatrixCanvas :: CreateControls ()
  26. {
  27. this -> SetForegroundColour (wxColour (0, 255, 0));
  28. this -> SetBackgroundColour (wxColour (0, 0, 0));
  29. int timerID = wxNewId ();
  30. m_MovementTimer = new wxTimer ( this , timerID);
  31. Connect (timerID, wxEVT_TIMER,
  32. wxTimerEventHandler (MatrixCanvas :: OnMovementTimer));
  33. InitMatrix ();
  34. Refresh ();
  35. m_MovementTimer-> Start (30);
  36. }
  37. void MatrixCanvas :: OnMovementTimer (wxTimerEvent & event )
  38. {
  39. for (size_t i = 0; i <m_Symbols.Count (); i ++)
  40. {
  41. int y = m_Symbols [i] .GetPosition (). y + m_Symbols [i] .GetSpeed ​​();
  42. if (y> GetClientSize (). GetHeight ())
  43. {
  44. y = -20;
  45. m_Symbols [i] .SetSpeed ​​(3 + rand ()% 5);
  46. m_Symbols [i] .SetColour (wxColour (0, rand ()% 200 + 56, 0));
  47. m_Symbols [i] .SetSymbol (rand ()% 2? wxT ( '0' ): wxT ( '1' ));
  48. }
  49. m_Symbols [i] .SetPosition (wxPoint (
  50. m_Symbols [i] .GetPosition (). x, y));
  51. }
  52. Refresh ();
  53. }
* This source code was highlighted with Source Code Highlighter .

As you can see, a timer is created in the CreateControls() method and an event handler is assigned to it using the Connect() method. In the destructor, the timer is deleted.
Fine. The screensaver can work in normal mode and in preview mode. In normal mode, we need to provide a response to the user's actions, namely, keystrokes, as well as a mouse click. To do this, we will create a member variable of the bool m_PreviewMode class and, depending on its value, in the event handlers for keystrokes and mouse clicks, we will close the main form of the application.
  1. void MatrixCanvas :: OnMouse (wxMouseEvent & event )
  2. {
  3. if ( event .LeftDown () || event .MiddleDown () || event .RightDown ())
  4. {
  5. if (! m_PreviewMode)
  6. {
  7. wxFrame * frame = wxDynamicCast (wxTheApp-> GetTopWindow (), wxFrame);
  8. if (frame) frame-> Close ();
  9. }
  10. }
  11. }
  12. void MatrixCanvas :: OnChar (wxKeyEvent & event )
  13. {
  14. if (! m_PreviewMode)
  15. {
  16. wxFrame * frame = wxDynamicCast (wxTheApp-> GetTopWindow (), wxFrame);
  17. if (frame) frame-> Close ();
  18. }
  19. }
* This source code was highlighted with Source Code Highlighter .

Actually, on this work on the component is completed. Now you need to put it on the main form:
  1. void MatrixEffectMainFrame :: CreateControls ()
  2. {
  3. MatrixEffectMainFrame * itemFrame1 = this ;
  4. wxBoxSizer * itemBoxSizer2 = new wxBoxSizer (wxVERTICAL);
  5. itemFrame1-> SetSizer (itemBoxSizer2);
  6. m_Canvas = new MatrixCanvas (itemFrame1, ID_CANVAS, wxDefaultPosition, wxSize (100, 100), wxNO_BORDER);
  7. itemBoxSizer2-> Add (m_Canvas, 1, wxGROW, 0);
  8. }
* This source code was highlighted with Source Code Highlighter .

Generally super. On this work on the graphical interface, we will finish.


Processing command line parameters


So now we have the most interesting part of the work, namely the processing of command line parameters.
Windows screensaver can run with three different command line arguments:

Source: https://habr.com/ru/post/48046/


All Articles