Our first example is the famous bare "Hello World" sample. This first program will just open a window and draw the string "Hello World" inside.

Let's start creating our application window. Here is its declaration:

#include "LF.h"

class HelloWorld : public LF::Window

  virtual void OnDraw(LF::Graphics& g);

It's a good practice that we put each class declaration in its own header file (.h), and the implementation in its own .cpp file. So, please name this file "HelloWorld.h".

Notice the #include "LF.h". This is all that's needed to make lightforms classes available for your code.

In lightforms you design every window of your application deriving a new class from the Window base class. The Window base class hides all the platform dependent stuff and takes care of displaying your window and delivering clean, easy-to-use events.

You specify code for handling events by overriding virtual methods, for example the OnDraw() method. The OnDraw() method will be called whenever some area of your window must be (re)drawn. This is the event we will use to display our Hello World string, so let's implement our class:

#include "LF.h"
#include "HelloWorld.h"

using namespace LF;

  SetCaption(L"Hello World");


void HelloWorld::OnDraw(Graphics& g)
  g.DrawString(L"Hello light World!!!", 10, 10);

All lightforms classes live inside the LF namespace, so we bring that namespace to scope with "using namespace LF;". We just set the caption of our window to "Font Test" in our constructor. Notice that the string has an "L" prefixed, this means we are passing a string of wide chars (wchar_t).

In this example we have nothing to worry about in the destructor, so just let's leave it empty.

The code that really makes the thing work is the OnDraw() method. This method is called every time our window needs to be drawn. This happens when it gets visible, and when the window contents are "uncovered" by other windows in front. This method receives a reference to a Graphics object. This object is a graphics context, the way we can draw things on something, in this case the window client area.

We work with colors through two methods of the graphics context: SetColor() and SetTextColor(). One sets the color of everything you draw except the text, and the later sets the text color. We set the color for our string and then draw it. The DrawString() method receives the string to be drawn and its location (the x,y coordinates).

Now let's make our main() function to instantiate our class and turn the engine on:

int main()
  HelloWorld form;
  form.SetSize(600, 600);


  return 0;

Everything is supposed to be very intuitive. The Application::Run() method starts the platform event loop and waits delivering the events until the application quits.

We'll see the steps to compile this sample on Linux and Windows later.

Special thanks to Sri for the help and Thiago for creating code@web.

made using code@Web

Copyright © 2004 LightForms
Valid XHTML 1.0! Valid CSS!