Your first SmartWin application II
From SmartWin Wiki
| Table of contents |
Creating Your First SmartWin Application
This is a small tutorial written to get you running on SmartWin++, the Template Based C++ Windows API GUI Abstraction Library! The tutorial assumes that you have Microsoft Visual Studio 7.1 or 8.0 in a version that contains the C++ compiler. (Note that this tutorial doesn't work with earlier releases.) If you're using the Bloodshed Dev-C++ IDE bundled with the MinGW/GCC compiler please check one of the "xxx.dev" files which can be found in any of the sub directories beneath the "SmartWinUnitTests" directory and skip to the next page afterwards
This tutorial demonstrates:
- The SmartWin++ Entry Point (equivalent to main or WinMain)
- Widgets
- Events and Event Handlers
- Creation of Widgets
Downloading the Library
First you should download (http://sourceforge.net/project/showfiles.php?group_id=92327) the library.
Installing the Library
Then you must add the paths to the "SmartWin/lib" folder and the "SmartWin" folder in the "VC++ Directories" of the Visual Studio IDE.
This is done by choosing the menu "Tools/Options/Projects/VC++ Directories" and then "Show Directories for:" "Library files". Press the yellow "Folder creation button" with the tool tip "New Line" or press CTRL + Insert and browse for the SmartWin/SmartWin/lib folder, if you installed SmartWin++ at e.g. the C drive (C:) you would have in this line "C:\SmartWin\SmartWin\lib".
Change the "Show Directories for:" to "Include files" and browse for the main inclusion folder (named include), and this is NOT at the root folder of the installation!
If you unzipped the .zip file in e.g. C: as SmartWin this path would be "C:\SmartWin\SmartWin\include"
Creating Your First Project
Create a new project (File/New/Project) of type "Visual C++ Projects/Win32 Project". Name the project "HelloWorld" and put it in whatever location you want. When you click OK afterwards you will be asked for some settings. Choose "Application Settings" and check the "Empty Project" box. Press "Finish." Now we should add a ".cpp" file. This can be done by right clicking the "HelloWorld" tree view node in your Solution Explorer. (If your Solution Explorer is invisible press the menu "View/Solution Explorer" or "Ctrl+Alt+L".) Choose "Add/Add New Item" and make sure the item is of type "C++ File (.cpp)". Give the file the name "Main.cpp". Your file should now be open in the Visual Studio IDE and it should have appeared inside the Source file folder of your project.
Then set the project options for your application. Right click the project in the Solution Explorer and then choose "Properties." A large dialog box with many options will appear. Choose the "C++" section in the tree view and then choose "Language" below. Set the option "Enable Run-Time Type Info" to "Yes (/GR)." Next tell the linker that we're going to build a SmartWin++ application. Choose the "Linker" tree view part and underneath this one the "Input" node. Verify that the "Additional Dependencies" says "SmartWin.lib." You can copy and paste from the line above.
Finally, verify the setting of "Treat wchar_t as Built-in Type." Choose the C/C++ | Language page and set "Treat wchar_t as Built-in Type" to Yes.
Something Interesting
At the top of your source code file include the SmartWin.h file by adding:
#include "SmartWin.h at the top of your Main.cpp file.
For our convenience we will bring in all the classes and functions in the SmartWin++ namespace by typing in:
using namespace SmartWin; below the #include "SmartWin.h line.
Now add our "SmartWinMain" function. All C++ programs have an "entry point function.
In SmartWin++ this function looks like:
int SmartWinMain( Application & app )
{
return app.run();
}
At this point you can actually Build and Run your SmartWin++ application. Press the menu Build/Build Solution and verify that you don't get any bugs. If you get any errors here you have not done everything right. Step back and verify that you have followed the instructions above. For your convenience, I've added all the code here so that you can copy and paste from the text field into your Main.cpp file.
#include "SmartWin.h"
using namespace SmartWin;
int SmartWinMain( Application & app )
{
return app.run();
}
In this function above the line "return app.run();" you can create your main widgets. But first we should create a class and derive it from WidgetFactory!
Subclassing the WidgetFactory Class
The WidgetFactory class is a "factory class" for creating Widgets Objects.
This means that it's normally responsible for creating all the control Widgets in your main Widget!
Create a new file of type "Header File (.h)" and name it "MyWidget.h" (See the first section of the tutorial for instructions.) Copy and paste from the block below into the newly-created MyWidget.h file.
#include "SmartWin.h"
using namespace SmartWin;
class MyWidget
: public WidgetFactory<WidgetWindow, MyWidget>
{
public:
void init()
{
createWindow();
}
};
Now we must instantiate our class or create an object of type MyWidget.
This is done right before the line return app.run(); in your Main.cpp file.
Open up the Main.cpp file and add the line #include "MyWidget.h" directly underneath the #include "SmartWin.h" line.
Then add this line MyWidget * widget = new MyWidget(); just before the return app.run(); line.
Now to "initialize" our widget we call the function "init();".
Add the line widget->init(); just underneath the last line you added.
And that's about it, we can now press F5 to compile and run the program and actually get a RESULT!
CONGRATULATIONS! You have just made your first SmartWin++ application!
For your convenience I've included everything here that should now be in your Main.cpp file.
#include "SmartWin.h"
#include "MyWidget.h"
using namespace SmartWin;
int SmartWinMain( Application & app )
{
MyWidget * widget = new MyWidget();
widget->init();
return app.run();
}
Of course it doesn't do anything useful so lets make some changes.
Doing Something Useful
Let's change the text of our main Widget.
Let's do something even MORE awesome: add a button and make the text of the main window change.
First add the line setText("Hello World"); inside the init() function of the MyWidget class.
Note: you must add it after the createWindow() function, or you will get a run time error.
Now you have changed the text of your main window.
Press F5 and run the application to verify that it compiles and to see your result.
Let's Create a Button!
Add the line WidgetButton * btn = createButton(); just after the setText line in your MyWidget class init function.
Then resize the button by adding the line btn->setBounds(100, 100, 100, 100); just after the createButton line. Press F5 and you've got a button.
This is a boring button; it doesn't do anything and it doesn't even have text inside of it.
Let's add an Event Handler and add some text to display inside the button!
Add the line btn->setText("My Button"); just after the setBounds line.
Then register an event handler by adding the line btn->onClicked( &MyWidget::clicked ); after the last line once again.
Now we have registered an event handler for the "Clicked Event," but we haven't added the actual event handler function. If you build your application now you will get a compilation error telling you that the function clicked is undefined. To fix this, add an event handler function with the signature:
void clicked( WidgetButton * btn )
{
}
at the end of your class.
Inside this function we can do all sorts of things, but for the purpose of this tutorial create a messagebox and display verifying that "Hello World Was Here."
Type in the line createMessageBox().show("Hello World Was Here"); inside the clicked function.
Press F5 and watch the beauty!
Congratulations, you have now built a fully functional SmartWin++ application! We have done it with ** 32 ** lines of code. If you were to use the Windows API for something like this you would have used about 150 lines of code and if you had used MFC you would have used roughly about twice as many lines! If you want to have more challenges look into the SmartWinUnitTests directory and try to figure out some of the projects there.
I've added the complete source to both files in two blocks below for your convenience. First the "MyWidget.h" file:
#include "SmartWin.h"
using namespace SmartWin;
class MyWidget
: public WidgetFactory<WidgetWindow, MyWidget>
{
public:
void clicked( WidgetButtonPtr btn )
{
createMessageBox().show("Hello World Was Here");
}
void init()
{
createWindow();
setText("Hello World");
WidgetButtonPtr btn = createButton();
btn->setText("My Button");
btn->setBounds(100, 100, 100, 100);
btn->onClicked(&MyWidget::clicked);
}
};
Then the "Main.cpp" file.
#include "SmartWin.h"
#include "MyWidget.h"
using namespace SmartWin;
int SmartWinMain( Application & app )
{
MyWidget * widget = new MyWidget();
widget->init();
return app.run();
}
Have Fun!
