Sign in to follow this  

Need advice for building a window/button class.

This topic is 4686 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am in the process of building a window and button class. Should I just make one button class and have the RightClick() and LeftClick() process a function pointer data member, or should I have all of my buttons I plan to use derived off of the basic button class and use virtual functions? I’d appreciate any advice on designing these classes.

Share this post


Link to post
Share on other sites
Well, I assume you're trying to build a GUI system yourself, and that you need a button class.

I would not go for any of the solutions you're giving. The first one is a bit messy, and you'll be quickly lost in your function calls. The second may look nicer, but you would need to create as many classes as you have buttons, which is too much.

So, just copy what exists and work well!

Windows uses an event system. This means that when you click on the button, an event is generated, which is added to an event queue. These events are then dispatched, and can call functions.

If your buttons are all in the same screen (like a configuration screen, for example), you can have a general event queue at the screen level. When you create a button, you give them an ID, and a reference to the event queue. When the user clicks on the button, the button class adds the event "clicked" to the queue. Your screen class will have a method update, that you call from your general main loop. You extract the events, and make actions depending on it, for example changing a number on your Configuration screen.

The nice thing about events is that you can very easely add controls: just create a new control class, add the corresponding event codes, and process them into your main screen.

Share this post


Link to post
Share on other sites

This topic is 4686 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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