GUI help

Started by
11 comments, last by BradDaBug 22 years, 4 months ago
I''m workin on my GUI, and its working great, but now I have a problem. Each type of control is a class derived from a base class. All the things like LeftMouseButtonDown() and junk like that (messages, i guess you could say) are virtual. Now, how do I make it so that each button can do something different? Like, when I click on a QUIT button it quits, but a different button would do something different? Would I have some other .cpp file containing all the different functions each button would have, then have some kind of function pointer telling each button which function to call once its pressed or whatever? How would I keep track of all those function pointers?
I like the DARK layout!
Advertisement
I''m not sure exactly what you''re trying to do, but you could use a function pointer that you pass to the button object during its creation (or some other time) and then your GUI''s user could define the button''s action.

Invader X
Invader''s Realm
I do what Invader X has already said, using a function pointer to handle messages. Just like Windows... you can pass in the ID of the event, and use a switch to handle it.

Z.
______________"Evil is Loud"
But wouldn''t that involve creating a message queu? I don''t wanna do that. What I have now is it calls the function, like wm_click() and wm_mousemove() and stuff like that.

Would I have to give it a function pointer for each function if I do it like that?

I''m loosly following Mason McCuskey''s "Creating a GUI using C++ and DirectX" tutorial.
I like the DARK layout!
Well, since all your base class methods are virtual, all you have to do is create the same methods in your derived class and they will be used instead of the base class. ie:

    class GUI{  virtual void LeftMouseButtonDown();  ..};class Button : public GUI{  void LeftMouseButtonDown();  ..};// then in .cppvoid GUI::LeftMouseButtonDown(){  // Base class implementation}void Button::LeftMouseButtonDown(){  // Derived class implementation}// then later in main() or WinMain() or whateverGUI* b;b = new Button();b->LeftMouseButtonDown();  // this will call the Button method    


And that's all you have to do!

Edited by - lshadow on December 18, 2001 11:32:30 AM
Working on: DoP
Here are some past threads on this topic, which may help you as well...

Small Thread

Larger Thread

Good luck.

"Don''t be afraid to dream, for out of such fragile things come miracles."
Thanks for all the replies, but I''m still not getting my answer (or maybe I''m just missing the obvious.)

I know how to derive a button class from a base class, and get buttons to do diffferent stuff than progress bars and whatever by using polymorphism, but this is what I''m trying to do...

I have two buttons, Button A and Button B. Both are instances of class CButton. I want Button A to do this and this and this, and I want Button B to do that and that and that. Two totally different actions for two totally different buttons that are instances of the same class. How?
I like the DARK layout!
Maybe if you derive yet another class for each type of button and then they can have specialized functions for what you want them to do. Or, you could also just define ALL the functions that you would need a button to perform in your CButton class (I think that is less flexible). One more thing too is that you could have an action variable like mybutton.type = CB_QUIT so that your class could decide what action to perform.

My best guess

Koz
-Koz
Maybe if you derive yet another class for each type of button and then they can have specialized functions for what you want them to do. Or, you could also just define ALL the functions that you would need a button to perform in your CButton class (I think that is less flexible). One more thing too is that you could have an action variable like mybutton.type = CB_QUIT so that your class could decide what action to perform.

My best guess

Koz
-Koz
BradDaBug: your question have already been answered by IShadow..
You said yourself that u got messages ala wm_leftmousebutton() (methods in ya baseclass) if these functions are virtual you just create a new button by deriving a new button class from you baseclass and make a Wm_leftmousebutton() method for that class.. like this:

class BaseButtonClass
{
virtual wm_leftmousebutton()
};

BaseButtonClass::wm_leftmousebutton()
{
// default handler code here
}

your button:

class MyButton : BaseButtonClass
{
virtual wm_leftmousebutton() // this method will override the one in the base class
};

MyButton::wm_leftmousebutton()
{
// this code is executed when button is clicked
}


the really smart thing is that a BaseButtonClass pointer can also refer to a MyButton object and the compiler figures out which method to call.. long live polymorphism :-)

This topic is closed to new replies.

Advertisement