Guest User

Untitled

a guest
Sep 27th, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.30 KB | None | 0 0
  1. If you find the method from above. From the below BaseWindow.cpp
  2. I would i have commented out what i have problems with
  3.  
  4.  
  5. **Main.cpp**
  6.  
  7. [code]
  8.     #include "BaseWindow.h"
  9.     #include "ChildWindow.h"
  10.    
  11.     int APIENTRY WinMain( HINSTANCE h_instance, HINSTANCE h_prev_instance, LPSTR lp_cmd_line, int n_cmd_show ) {
  12.    
  13.         ChildWindow child_window( h_instance, TEXT( "Child Window" ) );
  14.         BaseWindow base_window( TEXT( "Base Window" ), child_window.ClassName() );
  15.    
  16.         while ( base_window.HandleMessages() );
  17.         return 0;
  18.     }
  19.  
  20. **AbstractWindow.h**
  21.  
  22.     #ifndef __ABSTRACTWINDOW_H__
  23.     #define __ABSTRACTWINDOW_H__
  24.     #pragma once
  25.    
  26.     #include <Windows.h>
  27.    
  28.     class AbstractWindow {
  29.    
  30.         #pragma region Methods
  31.             public:
  32.                 AbstractWindow();
  33.                 ~AbstractWindow();
  34.    
  35.                 virtual bool Create();
  36.                     static LRESULT CALLBACK MessageHandler( HWND, UINT, WPARAM, LPARAM );
  37.                 protected:
  38.                     virtual LRESULT CALLBACK WindowProcedure( HWND, UINT, WPARAM, LPARAM ) = 0;
  39.         #pragma endregion
  40.    
  41.         #pragma region Variables
  42.             protected:
  43.                 HWND hwnd_;
  44.                 DWORD style_ex_;
  45.                 LPCTSTR class_name_;
  46.                 LPCTSTR window_name_;
  47.                 DWORD style_;
  48.                 int x_;
  49.                 int y_;
  50.                 int width_;
  51.                 int height_;
  52.                 HWND parent_;
  53.                 HMENU menu_;
  54.                 HINSTANCE instance_;
  55.    
  56.         #pragma endregion
  57.    
  58.     };
  59.     #endif // !__ABSTRACTWINDOW_H__
  60.  
  61. **AbstractWindow.cpp**
  62.  
  63.     #include "AbstractWindow.h"
  64.    
  65.     AbstractWindow::AbstractWindow() {}
  66.    
  67.     AbstractWindow::~AbstractWindow() {}
  68.    
  69.     bool AbstractWindow::Create() {
  70.         // Default Create Method
  71.    
  72.         hwnd_ = CreateWindowEx(
  73.             style_ex_,
  74.             class_name_,
  75.             window_name_,
  76.             style_,
  77.             x_,
  78.             y_,
  79.             width_,
  80.             height_,
  81.             parent_,
  82.             menu_,
  83.             instance_,
  84.             this
  85.             );
  86.    
  87.         return ( hwnd_ ? true : false );
  88.    
  89.     }
  90.    
  91.     LRESULT CALLBACK AbstractWindow::MessageHandler( HWND hwnd, UINT message, WPARAM w_param, LPARAM l_param ) {
  92.    
  93.         AbstractWindow* abstract_window = 0;
  94.    
  95.         if ( message == WM_NCCREATE ) {
  96.             abstract_window = ( AbstractWindow* ) ( ( LPCREATESTRUCT( l_param ) )->lpCreateParams );
  97.             SetWindowLong( hwnd, GWL_USERDATA, long( abstract_window ) );
  98.         }
  99.    
  100.         abstract_window = ( AbstractWindow * ) ( GetWindowLong( hwnd, GWL_USERDATA ) );
  101.    
  102.         if ( abstract_window ) {
  103.             return abstract_window->WindowProcedure( hwnd, message, w_param, l_param );
  104.         }
  105.         else {
  106.             return DefWindowProc( hwnd, message, w_param, l_param );
  107.         }
  108.    
  109.     }
  110.  
  111. **BaseWindow.h**
  112.  
  113.     #ifndef __BASEWINDOW_H__
  114.     #define __BASEWINDOW_H__
  115.     #pragma once
  116.    
  117.     #include "AbstractWindow.h"
  118.     #include "ChildWindow.h"
  119.    
  120.     class BaseWindow : public AbstractWindow {
  121.    
  122.         #pragma region Test Methods
  123.             private:
  124.                 void Update();
  125.                 void Render();
  126.         #pragma endregion
  127.    
  128.         #pragma region Methods
  129.             public:
  130.                 BaseWindow();
  131.                 ~BaseWindow();
  132.    
  133.                 bool HandleMessages();
  134.                 BaseWindow( const TCHAR*, const TCHAR* );
  135.                 void Show();
  136.                 virtual LRESULT CALLBACK WindowProcedure( HWND, UINT, WPARAM, LPARAM );
  137.                 #pragma region Handles
  138.                     private:
  139.                         bool CreateBackBuffer( HWND );
  140.                         bool OnPaint( HWND );
  141.                         bool PaintManager();
  142.                         bool OnDestroy();
  143.                 #pragma endregion
  144.         #pragma endregion
  145.    
  146.         #pragma region Variables
  147.             private:
  148.                 RECT window_rect_;              // Structure for window width and height
  149.                 int client_width_;
  150.                 int client_height_;
  151.                 POINT mouse_pos_;
  152.                 #pragma region Back Buffer
  153.                         HDC hdc_;                   // Handle to Device Context
  154.                         HDC back_buffer_;           // Back Buffer
  155.                         HBITMAP bitmap_;                // Current bitmap
  156.                 #pragma endregion
  157.         #pragma endregion
  158.    
  159.     };
  160.     #endif // !__BASEWINDOW_H__
  161.  
  162.  
  163. **BaseWindow.cpp**
  164.  
  165.     #include "BaseWindow.h"
  166.    
  167.     BaseWindow::BaseWindow() {}
  168.    
  169.     BaseWindow::~BaseWindow() {}
  170.    
  171.     BaseWindow::BaseWindow( const TCHAR* window_name, const TCHAR* class_name ) {
  172.    
  173.         style_ex_ = NULL;
  174.         class_name_ = class_name;
  175.         window_name_ = window_name;
  176.         style_ = WS_OVERLAPPEDWINDOW | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
  177.         x_ = CW_USEDEFAULT;
  178.         y_ = CW_USEDEFAULT; CW_USEDEFAULT;
  179.         width_ = CW_USEDEFAULT;
  180.         height_ = CW_USEDEFAULT;
  181.         parent_ = NULL;
  182.         menu_ = NULL;
  183.         instance_ = GetModuleHandle( NULL );
  184.    
  185.         Create();
  186.         Show();
  187.    
  188.     }
  189.    
  190.     void BaseWindow::Show() {
  191.         ShowWindow( hwnd_, SW_SHOW );
  192.         UpdateWindow( hwnd_ );
  193.     }
  194.    
  195.     LRESULT CALLBACK BaseWindow::WindowProcedure( HWND hwnd, UINT message, WPARAM w_param, LPARAM l_param ) {
  196.    
  197.         switch ( message ) {
  198.             case WM_CREATE:
  199.                 CreateBackBuffer(hwnd);
  200.                 return true;
  201.             case WM_ERASEBKGND:
  202.                 return true;
  203.             case WM_DESTROY:
  204.                 return OnDestroy();
  205.             default:
  206.                 return DefWindowProc( hwnd, message, w_param, l_param );
  207.         }
  208.     }
  209.    
  210.     bool BaseWindow::HandleMessages() {
  211.    
  212.         // Counts Per Second
  213.         INT64 counts_per_sec = 0;
  214.         QueryPerformanceFrequency( ( LARGE_INTEGER* ) &counts_per_sec );
  215.         // Seconds Per Count
  216.         float sec_per_count = 1.0f / ( float ) counts_per_sec;
  217.         // Pervious Time
  218.         INT64 prev_time = 0;
  219.         QueryPerformanceCounter( ( LARGE_INTEGER* ) &prev_time );
  220.    
  221.         MSG message = { 0 };
  222.    
  223.         if ( PeekMessage( &message, NULL, 0, 0, PM_REMOVE ) ) {
  224.             TranslateMessage( &message );
  225.             DispatchMessage( &message );
  226.    
  227.             if ( message.message == WM_QUIT ) {
  228.                 OnDestroy();
  229.                 return false;
  230.             }
  231.         }
  232.         else {
  233.             // Get current count
  234.             INT64 current_time = 0;
  235.             QueryPerformanceCounter( ( LARGE_INTEGER* ) &current_time );
  236.             // DeltaTime
  237.             float delta_time = ( current_time - prev_time ) * sec_per_count;
  238.    
  239.             // Update
  240.    
  241.             // Render
  242.             // I need to call for OnPaint()
  243.             // I need to take in hwnd
  244.             // But how ?
  245.             // Or do i not take in hwnd ?
  246.             // But hoe ?
  247.    
  248.         }
  249.         return true;
  250.     }
  251.    
  252.     #pragma region Handles
  253.    
  254.     bool BaseWindow::CreateBackBuffer( HWND hwnd ) {
  255.    
  256.         GetClientRect( hwnd, &window_rect_ );
  257.         client_width_ = window_rect_.right;
  258.         client_height_ = window_rect_.bottom;
  259.    
  260.         back_buffer_ = CreateCompatibleDC( NULL );  // Create Back Buffer
  261.         hdc_ = GetDC( hwnd );   // Get the Device Context
  262.         bitmap_ = CreateCompatibleBitmap( hdc_, client_width_, client_height_ );    // Create Bitmap
  263.         SelectObject( back_buffer_, bitmap_ );  // Select Bitmap
  264.    
  265.         ReleaseDC( hwnd, hdc_ );    // Release
  266.    
  267.         return true;
  268.     }
  269.    
  270.     bool BaseWindow::OnPaint( HWND hwnd ) {
  271.    
  272.         PAINTSTRUCT paint_struct;
  273.    
  274.         hdc_ = BeginPaint( hwnd_, &paint_struct );      // Get the Device Context
  275.    
  276.         BitBlt( back_buffer_, 0, 0, client_width_, client_height_, NULL, NULL, NULL, WHITENESS );
  277.    
  278.         // Paint
  279.         PaintManager();
  280.    
  281.         BitBlt( hdc_, 0, 0, client_width_, client_height_, back_buffer_, 0, 0, SRCCOPY );       // Display the back buff
  282.         InvalidateRect( hwnd, NULL, true );     // Repaint the screen
  283.    
  284.         EndPaint( hwnd, &paint_struct );
  285.    
  286.         return true;
  287.     }
  288.    
  289.     bool BaseWindow::PaintManager() {
  290.    
  291.         HBRUSH brush = ( HBRUSH ) ( GetStockObject( WHITE_BRUSH ) );
  292.         SelectObject( back_buffer_, brush );        // Select Brush
  293.    
  294.         Rectangle( back_buffer_, 200, 200, 500, 500 );
  295.    
  296.         DeleteObject( brush );
  297.    
  298.         return true;
  299.    
  300.     }
  301.    
  302.     bool BaseWindow::OnDestroy() {
  303.         PostQuitMessage( 0 );
  304.         return true;
  305.     }
  306.    
  307.     #pragma endregion
  308.  
  309. **ChildWindow.h**
  310.  
  311.         #ifndef __CHILDWINDOW_H__
  312.     #define __CHILDWINDOW_H__
  313.     #pragma once
  314.    
  315.     #include "AbstractWindow.h"
  316.     #include "BaseWindow.h"
  317.    
  318.     class ChildWindow : protected WNDCLASSEX {
  319.    
  320.         #pragma region Methods
  321.             public:
  322.                 ChildWindow();
  323.                 ~ChildWindow();
  324.    
  325.                 ChildWindow( HINSTANCE, const TCHAR* );
  326.                 bool Register();
  327.                 const TCHAR* ClassName() const;
  328.         #pragma endregion
  329.    
  330.     };
  331.     #endif // !__CHILDWINDOW_H__
  332.    
  333.  
  334.     **ChildWindow.cpp**
  335.  
  336.     #include "ChildWindow.h"
  337.    
  338.     ChildWindow::ChildWindow() {}
  339.    
  340.     ChildWindow::~ChildWindow() {}
  341.    
  342.     ChildWindow::ChildWindow( HINSTANCE h_instance, const TCHAR* class_name ) {
  343.    
  344.         cbSize = sizeof( WNDCLASSEX );
  345.         style = NULL;
  346.         lpfnWndProc = AbstractWindow::MessageHandler;
  347.         cbClsExtra = NULL;
  348.         cbWndExtra = NULL;
  349.         hInstance = h_instance;
  350.         hIcon = LoadIcon( NULL, IDI_APPLICATION );
  351.         hCursor = LoadCursor( NULL, IDC_ARROW );
  352.         //hbrBackground = ( HBRUSH ) ( GetStockObject( DKGRAY_BRUSH ) );
  353.         hbrBackground = ( HBRUSH ) NULL;
  354.         lpszMenuName = NULL;
  355.         lpszClassName = class_name;
  356.         hIconSm = LoadIcon( NULL, IDI_APPLICATION );
  357.    
  358.         Register();
  359.        
  360.     }
  361.    
  362.     bool ChildWindow::Register() {
  363.         return ( ( RegisterClassEx( this ) ) ? true : false );
  364.     }
  365.    
  366.     const TCHAR* ChildWindow::ClassName() const {
  367.         return lpszClassName;
  368.     }
Advertisement
Add Comment
Please, Sign In to add comment