Thread: Creating a Window Class

  1. #1
    Darkness Prevails Dark_Phoenix's Avatar
    Join Date
    Oct 2006
    Location
    Houston, Texas
    Posts
    174

    Creating a Window Class

    So I just about have my basic window class finished. I have it down to 1 unresolved external error. I cannot for the life of me trace it down. Here is what I have so far.
    MyWindowClass.h
    Code:
    #ifndef MYWINDOWCLASS_H_INCLUDED
    #define MYWINDOWCLASS_H_INCLUDED
     
    class CBaseWindow;
    typedef std::map<HWND, CBaseWindow*> hWndMap;
     
    class CBaseWindow
    {
    public:
    CBaseWindow() { }
    virtual ~CBaseWindow() { }
    static LRESULT CALLBACK GlobalWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
    virtual LRESULT CALLBACK InstanceWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
    void Attach(HWND h, CBaseWindow *pClass) { m_ThisMap[h] = pClass; }
    static hWndMap m_ThisMap;
    };
     
    class CWindow : public CBaseWindow
    {
    public:
    CWindow();
    ~CWindow() { }
    bool Create(HINSTANCE hInstance, int nCmdShow, LPCSTR szClassName, LPCSTR szWindowName);
    int Run();
    LRESULT CALLBACK InstanceWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
    BOOL NavPaneDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
    BOOL ViewPaneDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
     
    private:
    WNDCLASSEX m_wc;
    HWND m_hwnd;
    MSG m_Msg;
    HINSTANCE m_hInstance;
    LPCSTR m_ClassName;
    LPCSTR m_WindowName;
    HWND m_hNavPane;
    HWND m_hViewPane;
    };
     
    #endif // MYWINDOWCLASS_H_INCLUDED
    MyWindowClass.cpp
    Code:
    #include "stdafx.h"
     
    hWndMap CBaseWindow::m_ThisMap;
     
    LRESULT CBaseWindow::GlobalWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        hWndMap::iterator it = m_ThisMap.find(hWnd);
        if (it != m_ThisMap.end() )
        {
            CWindow *pClass = (CWindow*)it->second;
            return pClass->InstanceWndProc(hWnd, msg, wParam, lParam);
        }
        return DefWindowProc(hWnd, msg, wParam, lParam);
    }
     
    LRESULT CWindow::InstanceWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch(msg)
        {
             // Menu Bar Commands
            case WM_COMMAND:
            {
                switch(LOWORD(wParam) )
                {
                    // Select Menubar-File-Exit
                    case ID_FILE_EXIT:
                    {
                        PostMessage(hwnd, WM_CLOSE, 0, 0);
                    }
                    break;
                }
            }
            case WM_CLOSE:
            {
                DestroyWindow(hwnd);
            }
            break;
            case WM_DESTROY:
            {
                PostQuitMessage(0);
            }
            break;
            default:
            {
                return DefWindowProc(hwnd, msg, wParam, lParam);
            }
        }
        return 0;
    }
     
    CWindow::CWindow()
    {
        m_hwnd = NULL;
        m_hNavPane = NULL;
        m_hViewPane = NULL;
    }
     
    bool CWindow::Create(HINSTANCE hInstance, int nCmdShow, LPCSTR szClassName, LPCSTR szWindowName)
    {
        m_hInstance  = hInstance;
        m_ClassName  = (LPCSTR)szClassName;
        m_WindowName = (LPCSTR)szWindowName;
     
        //  Register the Window Class
        m_wc.cbSize        = sizeof(WNDCLASSEX);
        m_wc.style         = 0;
        m_wc.lpfnWndProc   = GlobalWndProc;
        m_wc.cbClsExtra    = 0;
        m_wc.cbWndExtra    = 0;
        m_wc.hInstance     = hInstance;
        m_wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
        m_wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
        m_wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
        m_wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MYMENU);
        m_wc.lpszClassName = m_ClassName;
        m_wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
     
        if(!RegisterClassEx(&m_wc))
        {
            SHOWERROR("Window Registration Failed!");
            return false;
        }
     
        // Create the window
        m_hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,
                                m_ClassName,
                                m_WindowName,
                                WS_MAXIMIZE | WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL,
                                0,0,1280,1020,
                                NULL,
                                NULL,
                                m_hInstance,
                                NULL);
        if(m_hwnd == NULL)
        {
            SHOWERROR("Window Creation Failed!");
            return false;
        }
        Attach(m_hwnd, this);
        ShowWindow(m_hwnd, nCmdShow);
        UpdateWindow(m_hwnd);
        return true;
    }
     
    int CWindow::Run()
    {
        while(GetMessage(&m_Msg, NULL, 0, 0) > 0)
        {
           TranslateMessage(&m_Msg);
           DispatchMessage(&m_Msg);
        }
        return m_Msg.wParam;
    }
    main.cpp
    Code:
    #include "stdafx.h"
     
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                       LPSTR lpCmdLine, int nCmdShow)
    {
        CWindow *pWindow = new CWindow();
        // Create the Window
        int msg;
        if (pWindow->Create(hInstance, nCmdShow, "MainWindowClass", "My Window") )
        {
            // Run the application (start the message loop)
            msg = pWindow->Run();
        }
        // Clean up resources
        if(pWindow)
        {
            delete pWindow;
            pWindow = NULL;
        }
        return msg;
    }
    and the error I'm getting is
    Code:
    unresolved external symbol "public: virtual long __stdcall CBaseWindow::InstanceWndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?InstanceWndProc@CBaseWindow@@UAGJPAUHWND__@@IIJ@Z)
    OK, so what am I missing?
    Using Code::Blocks and Windows XP

    In every hero, there COULD be a villain!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't see CBaseWindow::InstanceWndProc implemented anywhere...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    You should delete the

    virtual LRESULT CALLBACK InstanceWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);


    from your CBaseWindow because you didn't implement it.

    You implemented it in CWindow instead.

    If you need more help, try the C++ Win32 API Wrapper
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. C++ confusion
    By Eirik in forum Windows Programming
    Replies: 14
    Last Post: 04-29-2009, 01:54 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  5. dont want to use all params
    By stallion in forum Windows Programming
    Replies: 2
    Last Post: 02-18-2003, 08:10 AM