Click here to Skip to main content
15,868,016 members
Articles / Desktop Programming / MFC
Article

Hello World with the VCF part 2

Rate me:
Please Sign up or sign in to vote.
2.14/5 (6 votes)
4 Jun 20023 min read 45.6K   201   11   6
A Hello World example to introduce the Visual Component Framework, part 2.

Image 1

About the Visual Component Framework

The Visual Component Framework was inspired by the ease of use of environments like NeXTStep's Interface Builder, Java IDEs like JBuilder, Visual J++, and Borland's Delphi and C++ Builder. I wanted a generic C++ class framework I could use to build apps quickly and visually (when designing GUIs), as well as have the core of the framework be as cross platform as possible. The Visual Component Framework is an Open Source project, so feel free to grab and use it if you think it might be useful. If you're really adventuresome, you can volunteer to help develop it, making it even better, especially in tying it into the VC++ environment as an add-in. For more information on either the project, helping out, or just browsing the doxygen generated documentation, please go to the VCF project on Source Forge here, or the project website. The code is available from CVS (follow the how-to here for setting up CVS on Windows), or as a tar.gz file in files section of the project.

Introduction

This article builds on our work in the first article "Hello World with the VCF". This will demonstrate customizing the VCF::Application class and positioning the window where we want it. We will add our own code by overriding the application's initialization method, and learning the correct place to put application start code when using a VCF::Application based app.

The example finished by looking something like this:

int main(int argc, char *argv[])
{
    Application app;

    Window* mainWindow = new Window();

    app.setMainWindow( mainWindow );

    mainWindow->setCaption( "Hello World" );

    mainWindow->show();

    Application::appMain( argc, argv );
}

We are going to change things a bit by adding our own application class like so:

class HelloWorld2Application : public Application {
public:
    HelloWorld2Application () {}

    virtual ~HelloWorld2Application () {}

    virtual bool initRunningApplication(){
        bool result = Application::initRunningApplication();

        return result;
    }
};

Similar in purpose to MFC's CWinApp, we can easily make our own VCF::Application derived app class. We override one method: the virtual initRunningApplication(), which is called to initialize the app (thus the name initRunningApplication). In this method, we return a boolean value to indicate whether or not the application should continue to run. If we return true, then the app continues to proceed to the application loop. If we return false, then the application will gracefully close.

In our case, we are going to place the start up code that was earlier in the main() function, and place it in our application's initRunningApplication() method as follows:

class HelloWorld2Application : public Application {
public:
    HelloWorld2Application () {}

    virtual ~HelloWorld2Application () {}

    virtual bool initRunningApplication(){
        bool result = Application::initRunningApplication();

        Window* mainWindow = new Window();

        setMainWindow( mainWindow );

        Rect mainWindowBounds( 100.0, 100.0, 500.0, 500.0 );

        mainWindow->setBounds( &mainWindowBounds );

        mainWindow->show();

        return result;
    }
};

So, as before, we create our main window instance on the heap, and then set the application's main window through the setMainWindow() method. This ensures that the application registers itself as a listener to the window's events, and gets properly notified when the window closes. Next, we create a VCF::Rect and pass in the bounds we want for our new window. These are specified in left, top, right, and bottom. The bounds are set when we call the setBounds() method on the window, passing in a pointer to the Rect we just initialized. Finally, we show the window with a call to the show() method.

Now that we have successfully set up our application class, we can adjust our main() function to simplify things a bit.

int main(int argc, char *argv[])
{
    HelloWorld2Application app;

    Application::appMain( argc, argv );

    return 0;
}

That's it! All we have to do is create an instance of our HelloWorld2Application class on the stack and call the Application::appMain() method. The rest is taken care of for us, and our initRunningApplication() method will automatically be called by the internals of the Application::appMain functionality.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United States United States
Currently working on the Visual Component Framework, a really cool C++ framework. Currently the VCF has millions upon millions upon billions of Users. If I make anymore money from it I'll have to buy my own country.

Comments and Discussions

 
GeneralMy vote of 1 Pin
aljodav13-Dec-09 17:57
aljodav13-Dec-09 17:57 
Too much blah-blah-blah, confusing, poorly formatted, etc..., etc..., and etc...
GeneralMy Vote of '' 1 '' Pin
aljodav13-Dec-09 17:57
aljodav13-Dec-09 17:57 
Questionmmmm? Pin
brutuscat4-Nov-08 7:27
brutuscat4-Nov-08 7:27 
AnswerRe: mmmm? Pin
Jim Crafton4-Nov-08 7:47
Jim Crafton4-Nov-08 7:47 
GeneralRe: mmmm? Pin
brutuscat4-Nov-08 8:15
brutuscat4-Nov-08 8:15 
GeneralRe: mmmm? Pin
Jim Crafton4-Nov-08 8:32
Jim Crafton4-Nov-08 8:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.