Sourceforge.net - The VCF's Project Host
   The VCF Website Home   |   Online Discussion Forums   |   Sourceforge.net Project Page   

Chapter 2. Getting Started

The quickest way to start using the VCF is to look at the example code that comes with it. These tutorials are fully documented within the VCF online documentation here(insert url) and the source code itself is fully commented. Each tutorial tries to explain one particular aspect of programming with the VCF.

But before you can get the tutorials you first have to get the VCF installed on your system. For that, please download the latest installer (for Win32 systems) or .tgz file for your operating system.

Once you've installed the VCF try out the examples. The examples can be found in your vcf/examples directory.

2.1. A very simple VCF Application

This will explain the very basics of creating a "Hello World" application with the VCF.

This simple application will demonstrate creating a VCF::Application instance and displaying a main window, with "Hello World" in it's caption bar.

The simplest full VCF application, in other words, one that uses the Application class as it's starting point, as opposed to just using certain pieces of it, needs only a few lines of code in a main() function, like the following:

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

	Application::main();
}
			

Those two lines of code are all that is needed to create your app instance on the stack, and then start the app running, by calling the static Application::main() function. If you are using the VCF Application class features, like we are here, then there can only be one instance of a Application derived class for the running process, much like the restrictions that apply to MFC's CWinApp. Once you have this instance it is neccessary to start the app up by calling Application::main() function, passing in the number of arguments and the argument array of strings. This is processed internally and calls the applications initRunningApplication() and then the run loop, which starts the message pump up. From there on in you are processing windows messages.

Well this by itself is not that interesting, so lets add a window.

int main(int argc, char *argv[])
{
	Application* app = new Application(argc,argv);
	
	Window* mainWindow = new Window();

	Application::main();
}
			

This creates our new window on the heap. This is the preferred way of creating most objects in the VCF, with a few exceptions. Next we need to set the application's main window. In doing this the application is able to register itself as a listener to the windows window events (via a call to addWindowListener()), and will be notified when the window closes itself. At this point the application will in turn terminate it self cleanly and shut down. Failure to do this will prevent the Application of being notified to close correctly, and it will not be able to free up the heap based memory for the Window object.

int main(int argc, char *argv[])
{
	Application* app = new Application(argc, argv);
	
	Window* mainWindow = new Window();
	
	app->setMainWindow( mainWindow );

	Application::main();
}
			

Well this is still pretty boring so lets set the windows caption and then display it.

int main(int argc, char *argv[])
{
	Application* app = new Application(argc, argv);
	
	Window* mainWindow = new Window();
	
	app->setMainWindow( mainWindow );

	mainWindow->setCaption( "Hello World" );

	mainWindow->show();

	Application::main();
}
			

And there you have it: a window will magically display itself, with a caption that reads "Hello World". The caption is set via the setCaption() method, and the windows is made visible via the show() method. Alternatively, we could have made the window visible by calling the setVisible() with a true bool value.

Most of this was pretty easy, with the only odd thing the missing WinMain(). This is circumvented by setting some custom linker settings: in the output section of the project setting's Link tab, you specify mainCRTStartup as the Entry-point symbol, and make sure your /subsystem flag is set to /subsystem:windows not /subsystem:console. With this set you can go about your merry way and still use main() just like normal.

   Comments or Suggestions?    License Information