Thread: Object Oriented WinAPI

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    10

    Object Oriented WinAPI

    I have a problem with following code:
    Everything is OK before that I want to use 'this' pointer from windowProcedure:
    Code:
    SetWindowText(o_hwnd, this->m_className);
    Any idea? I don't understand why it doesn't work - CWindowClass is accesible, but nothing more?

    Code:
    #ifndef MAIN_H_INCLUDED
    #define MAIN_H_INCLUDED
    
    #define body /* BODY */
    #define operands /* OPERANDS */
    #define initialize /* INITIALIZE */
    #define function /* FUNCTION */
    
    #include <windows.h>
    #include "error.h"
    
    // class declaration CMainWindow
    //_________________________________________________________________________________________________
    typedef class CMainWindow{
      private: const HINSTANCE mi_instance;
      private: const int mi_cmdShow;
    
      private: const char *m_className;
      private: char *m_windowCaption;
    
      private: WNDCLASSEX *m_windowClass;
      private: MSG        *m_messages;
      private: HWND       *m_hWindow;
    
      private: void initializeWndClass(void);
      private: void registerWndClass(void);
      private: void createWnd(void);
      private: static LRESULT __stdcall static_windowProcedure(HWND, UINT, WPARAM, LPARAM);
      private: LRESULT __stdcall windowProcedure(HWND, UINT, WPARAM, LPARAM);
    
      public: void Caption(char*);
      public: void Run(void);
      public: CMainWindow(const HINSTANCE, const int);
      public: ~CMainWindow(void);
    }* MainWindow;
    
    //_________________________________________________________________________________________________
    // ctor (HINSTANCE, HINSTANCE, LPSTR, int)
    CMainWindow::CMainWindow
    operands(
      const HINSTANCE hThisInstance,
      int nCmdShow
    )
    initialize:
      mi_instance(hThisInstance),
      mi_cmdShow(nCmdShow),
      m_className("CMainWindowClass"),
      m_windowCaption("ITHDTetris")
    body{
      // Initialize non-const members
      this->m_windowClass = new WNDCLASSEX;
      this->m_messages    = new MSG;
      this->m_hWindow     = new HWND;
    }
    
    //_________________________________________________________________________________________________
    // dtor (void)
    CMainWindow::~CMainWindow operands(void) body{
    
      delete m_className;
      delete m_windowClass;
      delete m_windowCaption;
      delete m_messages;
      delete m_hWindow;
    }
    
    //_________________________________________________________________________________________________
    // Caption : void (const char*)
    inline void
    CMainWindow::Caption operands(char* o_caption){
      m_windowCaption = o_caption;
    }
    
    //_________________________________________________________________________________________________
    // Run : void (void)
    void
    CMainWindow::Run operands(void) body{
      // Prepare window
      this->initializeWndClass();
      this->registerWndClass();
    
      // Make the window visible on the screen
      ShowWindow(*m_hWindow, mi_cmdShow);
    
      // Run the message loop. It will run until GetMessage() returns 0
      while(GetMessage (m_messages, NULL, 0, 0)){
        // Translate virtual-key messages into character messages
        TranslateMessage(m_messages);
        // Send message to WindowProcedure
        DispatchMessage(m_messages);
      }
    }
    
    //_________________________________________________________________________________________________
    // initializeWndClass : void (HINSTANCE) /* Fills WNDCLASSEX fields */
    void
    CMainWindow::initializeWndClass operands(void) body{
    
      // The window structure
      this->m_windowClass->hInstance = mi_instance;
      this->m_windowClass->lpszClassName = m_className;
      this->m_windowClass->lpfnWndProc = this->static_windowProcedure;
      this->m_windowClass->style = CS_DBLCLKS;
      this->m_windowClass->cbSize = sizeof(WNDCLASSEX);
    
      // Use default icon and mouse pointer
      this->m_windowClass->hIcon = LoadIcon (NULL, IDI_APPLICATION);
      this->m_windowClass->hIconSm = LoadIcon (NULL, IDI_APPLICATION);
      this->m_windowClass->hCursor = LoadCursor (NULL, IDC_ARROW);
      this->m_windowClass->lpszMenuName = NULL;                // No menu
      this->m_windowClass->cbClsExtra = 0;                     // No extra bytes after the window class
      this->m_windowClass->cbWndExtra = 0;                     // structure or the window instance
    
      // Use Windows's default colour as the background of the window
      this->m_windowClass->hbrBackground = (HBRUSH) COLOR_BACKGROUND;
    }
    
    //_________________________________________________________________________________________________
    // registerWndClass : void (void)
    void
    CMainWindow::registerWndClass operands(void) body{
      // Register the window class, and if it fails quit the program
      if(!RegisterClassEx(m_windowClass)) throw Error("Class registring fail");
      else this->createWnd();
    }
    
    //_________________________________________________________________________________________________
    // createWnd : void (void)
    void
    CMainWindow::createWnd operands(void) body{
      *this->m_hWindow = CreateWindowEx(
        0,                      // Extended possibilites for variation
        this->m_className,      // Classname
        this->m_windowCaption,  // Title Text
        WS_OVERLAPPEDWINDOW,    // Default window
        CW_USEDEFAULT,          // Windows decides the position
        CW_USEDEFAULT,          //   where the window ends up on the screen
        544,                    // The programs width
        375,                    //   and height in pixels
        HWND_DESKTOP,           // The window is a child-window to desktop
        NULL,                   // No menu
        mi_instance,            // Program Instance handler
        this                    // This instance
      );
    }
    
    //_________________________________________________________________________________________________
    LRESULT __stdcall
    CMainWindow::static_windowProcedure
    operands(
      HWND   o_hWnd,
      UINT   o_message,
      WPARAM o_wParam,
      LPARAM o_lParam
    )
    body{
      CMainWindow *lp_parrent = reinterpret_cast<CMainWindow*>(GetWindowLongPtr(o_hWnd, GWL_USERDATA));
      if(!lp_parrent){
        CREATESTRUCT *cs=(CREATESTRUCT*)o_lParam;
        lp_parrent=(CMainWindow*)cs->lpCreateParams;
        SetWindowLongPtr(o_hWnd, GWL_USERDATA, (LONG_PTR) lp_parrent);
        return lp_parrent->windowProcedure(o_hWnd, o_message, o_wParam, o_lParam);
      }
      return lp_parrent->windowProcedure(o_hWnd, o_message, o_wParam, o_lParam);
    }
    
    //_________________________________________________________________________________________________
    LRESULT __stdcall
    CMainWindow::windowProcedure operands(
      HWND   o_hwnd,
      UINT   o_message,
      WPARAM o_wParam,
      LPARAM o_lParam
    )
    body{
      switch (o_message){       // handle the messages
        case WM_DESTROY:
          PostQuitMessage (0);  // send a WM_QUIT to the message queue
          break;
        case WM_LBUTTONDOWN:
        case WM_LBUTTONDBLCLK:
          SetWindowText(o_hwnd, this->m_className);
          break;
        case WM_LBUTTONUP:
          SetWindowText(o_hwnd, "Push the left mouse button");
          break;
        default:                  // for messages that we don't deal with
          return DefWindowProc (o_hwnd, o_message, o_wParam, o_lParam);
      }
    }
    
    #endif // MAIN_H_INCLUDED

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    After the call to CreateWindowEx() add
    Code:
    SetWindowLong( m_hWindow, GWL_USERDATA, (long)(void*)this);
    and in the window procedure add
    Code:
    CMainWindow *This = (CMainWindow*)GetWindowLong (o_hWnd, GWL_USERDATA)
    Use This-> to call any member functions. It looks like you have something close to this already.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    10
    Thanks a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Object Oriented Programming
    By obaid in forum C++ Programming
    Replies: 5
    Last Post: 08-22-2007, 05:31 AM
  2. Object Oriented Programming
    By eric123 in forum C++ Programming
    Replies: 6
    Last Post: 11-30-2005, 04:56 AM
  3. Object Oriented Cube
    By Ti22 in forum Game Programming
    Replies: 3
    Last Post: 01-11-2005, 08:18 PM
  4. Object Oriented - Funny story
    By MethodMan in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 09-26-2002, 02:21 PM