Win32 C++ class wrapper

tom_mai78101

The Helper Connoisseur / Ex-MineCraft Host
Staff member
Reaction score
1,694
I found this while I am cleaning out a few folders in my laptop. I don't know if I had ever posted this here and share it with you guys, as it contains a nice C++ class wrapper for Win32 applications that deserves to be seen.
Code:
//
// Header
//
 
class Window
{
    public:
    //...
    protected:
        static LRESULT CALLBACK StaticWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); 
        LRESULT WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam);
    protected: 
        HWND  m_hWnd;
};
//
// Source
//
// When you register the window class, pass CWindow::StaticWndProc as the window message procedure pointer
// When you create the window, pass "this" as the extra parameter
 
m_hWnd = CreateWindowEx(0,"Classname","Title",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,320,200,NULL,NULL,hInstance,this);
 
// Now for the real code:
 
LRESULT CALLBACK CWindow::StaticWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    CWindow* pParent;
   
    // Get pointer to window 
    if(uMsg == WM_CREATE) 
    {     
        pParent = (CWindow*)((LPCREATESTRUCT)lParam)->lpCreateParams;     
        SetWindowLongPtr(hWnd,GWL_USERDATA,(LONG_PTR)pParent); 
    } 
    else 
    {     
        pParent = (CWindow*)GetWindowLongPtr(hWnd,GWL_USERDATA);     
        if(!pParent)
            return DefWindowProc(hWnd,uMsg,wParam,lParam); 
    } 
    pParent->m_hWnd = hWnd; 
    return pParent->WndProc(uMsg,wParam,lParam);
}
 
LRESULT CWindow::WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
{ 
    switch(uMsg) 
    {     
        // Normal code here 
    } 
 
    // Call default window proc if we haven't handled the message 
    return DefWindowProc(m_hWnd,uMsg,wParam,lParam);
}
 
 
 
 
 
 
 
 
 
--OR--
 
 
http://web.archive.org/web/20051125022758/www.rpi.edu/~pudeyo/articles/wndproc/
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      • Ghan
        Administrator - Servers are fun

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top