Sign in to follow this  
Maarten Baert

Window classes in C++

Recommended Posts

I am learning to create windows programs, and I have a question. Most tutorials I found use C code for everything, instead of object-oriented C++ code. These tutorials almost always tell me to create a separate window procedure for every kind of window, and a separate window class to use that window procedure. But I am using C++, and in my program every window already has its own C++ class derived from an abstract (callback) class, and I have only one window procedure function for all these classes. The window procedure uses SetWindowLong to store a pointer to the window class instances and just calls the virtual functions. My question is, do I have to create a separate window class for every C++ class if I am using the same window procedure?

Share this post


Link to post
Share on other sites
It really depends on what you need your windows to do. For example, if you need a menu in your window its name needs to be provided when filling out the window class description. Apart from that you can get different styles (overlapped, without border, without minimize etc) and sizes by specifying that in the CreateWindow[Ex] call.

In other words, if you don't need menus I think you will be fine using a single window class. I may have overlooked something though, not being an expert Windows programmer. :)

Share this post


Link to post
Share on other sites
The important point is that different *types* of windows should invoke different *implementations* of a window procedure function. Buttons, for example, should have a different window procedure than edit boxes. In C the way you would do this is with a different window class, which allows you to register a different window procedure. But in C++ you have virtual functions, which allows you to solve the same problem differently. Consider for example, this:


class BaseWindow
{
public:
virtual LRESULT WindowProc(UINT msg, WPARAM wParam, LPARAM lParam);
protected:
HWND m_hwnd;
private:
};

class EditBox : public BaseWindow
{
public:
virtual LRESULT WindowProc(UINT msg, WPARAM wParam, LPARAM lparam);
};

class Button : public BaseWindow
{
public:
virtual LRESULT WindowProc(UINT msg, WPARAM wParam, LPARAM lparam);
};



Using the method that you have already implemented with SetWindowLong(), you register a single global or static window proc which uses GetWindowLong() to obtain an instance to a BaseWindow* and then calls window->WindowProc(msg, wParam, lParam). Because it uses a virtual function, you have now invoked a different window procedure for each type of window, exactly the same thing that registering multiple classes achieves, but using a totally different method.

There are still valid reasons to register multiple types of window classes, but they're far less common than they used to be back in the C-days.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this