Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For the "some stuff" about GUI, which you describe as "Drawing, refreshing, event-based... ", i.e. basic concepts, consider the Windows API.</p> <p>It's easy, it's <em>concrete</em>, and it allows your students to progress to wrapping it all in an OO way, which is very educational.</p> <p>Example that does "Drawing, refreshing, event-based", drawing a dynamically sized ellipse (you need to define the three headers included at the top):</p> <pre><code>#include &lt;winapi/wrapper/windows_h.h&gt; #include &lt;cppSupport/error_handling.h&gt; // cppSupport::throwX, cppSupport::ExitCode #include &lt;cppSupport/anti_warnings.h&gt; // cppSupport::suppressUnusedWarningFor #include &lt;iostream&gt; #include &lt;string&gt; // std::wstring using cppSupport::throwX; using cppSupport::ExitCode; using cppSupport::suppressUnusedWarningFor; RECT clientRectOf( HWND window ) { RECT result; GetClientRect( window, &amp;result ); return result; } void drawEllipse( HDC dc, RECT const&amp; boundingRect ) { RECT const&amp; r = boundingRect; Ellipse( dc, r.left, r.top, r.right, r.bottom ); } namespace mainWindow { namespace detail { void paint( HWND window, HDC dc ) { drawEllipse( dc, clientRectOf( window ) ); } void onWmDestroy( HWND window ) { suppressUnusedWarningFor( window ); PostQuitMessage( ExitCode::success() ); } void onWmPaint( HWND window ) { PAINTSTRUCT info; HDC const deviceContext = BeginPaint( window, &amp;info ); paint( window, deviceContext ); EndPaint( window, &amp;info ); } LRESULT CALLBACK messageHandler( HWND window, UINT messageId, WPARAM wParam, LPARAM lParam ) { switch( messageId ) { case WM_DESTROY: return HANDLE_WM_DESTROY( window, wParam, lParam, onWmDestroy ); case WM_PAINT: return HANDLE_WM_PAINT( window, wParam, lParam, onWmPaint ); default: return DefWindowProc( window, messageId, wParam, lParam ); } } ATOM registerClass() { WNDCLASS const info = { CS_HREDRAW | CS_VREDRAW, // UINT style; &amp;messageHandler, // WNDPROC lpfnWndProc; 0, // int cbClsExtra; 0, // int cbWndExtra; GetModuleHandle( 0 ), // HINSTANCE hInstance; 0, // HICON hIcon; LoadCursor( 0, IDC_ARROW ), // HCURSOR hCursor; reinterpret_cast&lt;HBRUSH&gt;( COLOR_WINDOW + 1 ), // HBRUSH hbrBackground; 0, // LPCTSTR lpszMenuName; L"MainWindowClass" // LPCTSTR lpszClassName; }; ATOM const result = RegisterClass( &amp;info ); (result != 0) || throwX( "registerWindowClass: RegisterClass failed" ); return result; } ATOM classAtom() { static ATOM const theClassAtom = registerClass(); return theClassAtom; } } // namespace mainWindow::detail HWND create( std::wstring const&amp; title ) { HWND const window = CreateWindow( MAKEINTATOM( detail::classAtom() ), // LPCTSTR lpClassName, title.c_str(), // LPCTSTR lpWindowName, WS_OVERLAPPEDWINDOW, // DWORD dwStyle, CW_USEDEFAULT, // int x, CW_USEDEFAULT, // int y, CW_USEDEFAULT, // int nWidth, CW_USEDEFAULT, // int nHeight, 0, // HWND hWndParent, 0, // HMENU hMenu, GetModuleHandle( 0 ), // HINSTANCE hInstance, 0 // LPVOID lpParam ); (window != 0) || throwX( "createMainWindow: CreateWindow failed" ); return window; } } // namespace mainWindow bool getMessage( MSG&amp; message, HWND window = 0 ) { int const result = GetMessage( &amp;message, window, 0, 0 ); (result != -1) || throwX( "getMessage: GetMessage failed" ); return (result != 0); } ExitCode dispatchWindowMessages() { MSG message; while( getMessage( message ) ) { TranslateMessage( &amp;message ); DispatchMessage( &amp;message ); } assert( message.message == WM_QUIT ); return ExitCode( message.wParam ); } ExitCode cppMain() { HWND const window = mainWindow::create( L"My main window" ); ShowWindow( window, SW_SHOWDEFAULT ); return dispatchWindowMessages(); } int main() { try { return cppMain(); } catch( std::exception const&amp; x ) { std::cerr &lt;&lt; "!" &lt;&lt; x.what() &lt;&lt; std::endl; } return ExitCode::failure(); } </code></pre> <p><strong>EDIT</strong>: OK, perhaps I should best post those three headers. It's not a good (complete) answer without them. So.</p> <p><strong>[winapi/wrapper/windows_h.h]</strong>:</p> <pre><code>// Copyright (c) 2010 Alf P. Steinbach #ifndef WINAPI_WRAPPER_WINDOWSH_H #define WINAPI_WRAPPER_WINDOWSH_H //#include &lt;progrock/cppx/devsupport/better_experience.h&gt; #ifdef _MBCS # error _MBCS was defined, only Unicode is supported. #endif #undef UNICODE #undef _UNICODE #undef STRICT #undef NOMINMAX #define UNICODE #define _UNICODE #define STRICT #define NOMINMAX #ifdef _WIN32_WINNT # if _WIN32_WINNT &lt; 0x5000 # error _WIN32_WINNT &lt; 0x5000, pre-Windows 2000 is not supported. # endif #else # define _WIN32_WINNT 0x5000 #endif #ifdef _WIN32_IE # if _WIN32_IE &lt; 0x5000 # error _WIN32_IE &lt; 0x5000, that old browser / Windows shell is not supported. # endif #else # define _WIN32_IE 0x5000 #endif #include &lt;windows.h&gt; #include &lt;windowsx.h&gt; //------------------------------------------------- g++ fixups: #ifndef BS_TYPEMASK # define BS_TYPEMASK 0x0000000F #endif #ifndef BS_PUSHBOX # define BS_PUSHBOX 0x0000000AL #endif #ifndef EN_ALIGN_LTR_EC # define EN_ALIGN_LTR_EC 0x0700 # define EN_ALIGN_RTL_EC 0x0701 #endif #ifndef LBS_COMBOBOX # define LBS_COMBOBOX 0x8000L #endif #endif </code></pre> <p><strong>[cppsupport/error_handling.h]</strong>:</p> <pre><code>#ifndef CPPSUPPORT_ERROR_HANDLING_H #define CPPSUPPORT_ERROR_HANDLING_H //-------------------------------- Dependencies: #include &lt;assert.h&gt; // assert #include &lt;stdexcept&gt; // std::runtime_error, std::exception #include &lt;stdlib.h&gt; // EXIT_SUCCESS, EXIT_FAILURE #include &lt;string&gt; // std::string, std::wstring //-------------------------------- Interface: namespace cppSupport { inline bool throwX( std::string const&amp; s ) { throw std::runtime_error( s ); } struct ExitCode { int value; explicit ExitCode( int v ): value( v ) {} operator int() const { return value; } static ExitCode success() { return ExitCode( EXIT_SUCCESS ); } static ExitCode failure() { return ExitCode( EXIT_FAILURE ); } }; } // namespace cppSupport #endif </code></pre> <p><strong>[cppsupport/anti_warnings.h]</strong>:</p> <pre><code>#ifndef CPPSUPPORT_ANTI_WARNINGS_H #define CPPSUPPORT_ANTI_WARNINGS_H //-------------------------------- Dependencies: // -- None. //-------------------------------- Interface: namespace cppSupport { template&lt; class Type &gt; void suppressUnusedWarningFor( Type const&amp; ) {} } // namespace cppSupport #endif </code></pre> <p>Cheers &amp; hth.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload