Thread: About encapsulating a window class

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    50

    About encapsulating a window class

    I find the first parameter of the function WndProc is equal to the member variable of class MyWindow,
    I want to omit the parameter in the function WndProc,how to do?
    Look:
    Code:
    #include <windows.h>
    
    class MyWindow
    {
    public:
     HWND m_hWnd;
    public:
     MyWindow();
     BOOL CreateWin(LPCTSTR lpClassName,LPCTSTR lpWindowName,DWORD dwStyle,int x,int y,int nWidth,int nHeight,      
                       HWND hWndParent,HMENU hMenu,HINSTANCE hInstance,LPVOID lpParam );     
                    
        BOOL ShowWindow(int iCmdShow) ;
        BOOL UpdateWindow();
        static LRESULT CALLBACK WndProc (HWND hWnd,UINT message, WPARAM wParam, LPARAM lParam);
            
    };
    
    MyWindow::MyWindow()
    {
       m_hWnd=0;
    }
    
    BOOL MyWindow::CreateWin(LPCTSTR lpClassName, LPCTSTR lpWindowName,DWORD dwStyle,int x,int y,int nWidth,int nHeight,     
                             HWND hWndParent, HMENU hMenu,HINSTANCE hInstance, LPVOID lpParam)
    {
       m_hWnd = ::CreateWindow (lpClassName,lpWindowName,dwStyle,x,y,nWidth,nHeight,hWndParent,hMenu,hInstance,lpParam); 
       if(NULL==m_hWand)
        return FALSE;
       else
        return TRUE;
    }
    
    BOOL MyWindow::ShowWindow(int iCmdShow)
    {
        return ::ShowWindow(m_hWnd,iCmdShow);
    }
    
    BOOL MyWindow::UpdateWindow()
    {
     return ::UpdateWindow (m_hWnd);
    }
    
    LRESULT MyWindow::WndProc (HWND hWnd,UINT message, WPARAM wParam, LPARAM lParam)
    {
         HDC         hdc ;
         PAINTSTRUCT ps ;
         RECT        rect ;
        
         switch (message)
         {
         case WM_LBUTTONDOWN:
         MessageBox (hWnd,"WM_LBUTTONDOWN","Title",MB_OK);
              return 0 ;
        
        case WM_RBUTTONDOWN:
        MessageBox (hWnd,"WM_RBUTTONDOWN","Title",MB_OK);
              return 0 ;
       
         case WM_PAINT:
              hdc = BeginPaint (hWnd, &ps) ;
             
              GetClientRect (hWnd, &rect) ;
             
              DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect,
                        DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
             
              EndPaint (hWnd, &ps) ;
              return 0 ;
             
         case WM_DESTROY:
              PostQuitMessage (0) ;
              return 0 ;
         }
         return DefWindowProc (hWnd, message, wParam, lParam) ;
    }
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        PSTR szCmdLine, int iCmdShow)
    {
        static TCHAR szAppName[] = TEXT ("HelloWin") ;
        MSG  msg ;
        class MyWindow MyObj;
        WNDCLASS wndclass;
        wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
        wndclass.lpfnWndProc   =  MyWindow::WndProc;
        wndclass.cbClsExtra    = 0 ;
        wndclass.cbWndExtra    = 0 ;
        wndclass.hInstance     = hInstance ;
        wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
        wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
        wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
        wndclass.lpszMenuName  = NULL;
        wndclass.lpszClassName = szAppName;
    
        if (!RegisterClass (&wndclass))
        {
             MessageBox (NULL, TEXT ("This program requires Windows NT!"),
                         TEXT ("HelloWin"), MB_ICONERROR) ;
             return 0 ;
        }
       MyObj.CreateWin(szAppName,TEXT("The Hello Program"),
                        WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,CW_USEDEFAULT,
                        CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL, hInstance,NULL);
     
        MyObj.ShowWindow(SW_SHOWNORMAL); 
        MyObj.UpdateWindow();
       
        while (GetMessage (&msg, NULL, 0, 0))
        {
              TranslateMessage (&msg) ;
              DispatchMessage (&msg) ;
        }
        return msg.wParam ;
    }

  2. #2
    Registered User
    Join Date
    Sep 2009
    Posts
    48
    You might want to post questions specifically about Windows programming in the Windows programming section of the forum (Windows Programming), but I'll try to help.

    You can't pass a WndProc without the correct signature to the WNDCLASS struct, but you can do something like this:

    Create a regular WndProc class and give this one to the WNDCLASS(EX) struct, like so:

    Code:
    LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
         static MyWindow* wnd = NULL;
    
         switch(msg)
         {
         case WM_CREATE:
              wnd = (MyWindow*)((LPCREATESTRUCT)lParam)->lpCreateParams;
              return 0;
         default:
              if(wnd)
                   return wnd->WndProc(msg, wParam, lParam);
              else
                   return DefWindowProc(hWnd, msg, wParam, lParam);
         }
    }
    
    // ...
    WNDCLASSEX wcex;
    // ...
    wcex.WndProc = WndProc;
    // ...
    Then you can pass this as the last argument in the CreateWindow function within your class, like so:

    Code:
    hWnd = ::CreateWindow(lpClassName, lpWindowName, dwStyle, x, y, nWidth,
                                 nHeight, hWndParent, hMenu, hInstance, this);
    And change the window procedure within your class accordingly:

    Code:
    LRESULT WndProc(UINT msg, WPARAM wParam, LPARAM lParam)
    {
         // ...
         return DefWindowProc(hWnd, // The HWND member of your class
              msg, wParam, lParam);
    }
    You may also want to use the class to store/encapsulate more information about the window, like the class name, window title, x/y position, and width/height for example.

  3. #3

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encapsulating DlgProc
    By Joelito in forum C++ Programming
    Replies: 7
    Last Post: 04-01-2006, 07:47 AM
  2. Encapsulating cmd.exe
    By bennyandthejets in forum Windows Programming
    Replies: 4
    Last Post: 05-17-2005, 12:46 AM
  3. Window Class?
    By drdroid in forum Windows Programming
    Replies: 4
    Last Post: 10-16-2002, 12:06 AM
  4. Window Class Help
    By aresashura in forum Windows Programming
    Replies: 3
    Last Post: 12-09-2001, 10:17 PM
  5. Getting a Window's Class
    By minime6696 in forum Windows Programming
    Replies: 1
    Last Post: 09-04-2001, 06:26 AM