Create a new project







Your project tree should be looking like this:





Select C++ File(.cpp), enter the name MainFrame.cpp in the name field, and click Add.



The new file should now appear under Source Files in the project tree.



Double-click on MainFrame.cpp to open it for editing.


Every SolidWidgets project must include one class that represents the main window of the application. This class must be derived from swFrame. swFrame represents the starting point of every SolidWidgets application.Every swFrame-derived class must implement the following 6 functions:


swString getApplicationTitle()

Must return the licensed application title.


void windowOpening()

Called by the framework to allow your application to initialize before the

application main window is displayed.


void windowOpened()

Called by the framework after the main window has been displayed.


BOOL windowClosing()

Called by the framework before the application is about to be shutdown. Return

FALSE if you want to prevent the application from shutting down.


void windowClosed()

Called after the application main window has been destroyed.


void actionPerformed(long sourceId,long eventId,const swString& eventName)

Receives all action events that are fired from menus, buttons, and other components.


Let's now add some code to MainFrame.cpp so we can show a window:


#include <swFrame.h>

class MainFrame: public swFrame

{

public:

// You must set the SolidWidgets license file in the constructor of your frame class

MainFrame()

{

setLicenseFile(L”c:\\program files\\SolidWidgets\\SolidWidgetsLicense.swl”,L””);

}

// Your frame class must implement the function getApplicationTitle, which must

// return the title of your application. This title must match the title that you licensed

// with SolidWidgets. If you’re using the trial version, return “SolidWidgets UI demo”.

swString getApplicationTitle()

{

return L”SolidWidgets UI demo”;

}


void windowOpening()

{

}


void windowOpened()

{

}


BOOL windowClosing()

{

if(showPromptMessage(L”Close Window?”,L”Confirm Close”,MB_YESNO) == IDYES)

return TRUE;

return FALSE;

}


void windowClosed()

{

}


void actionPerformed(long sourceId,long eventid, const swString& eventName)

{

}

};


int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPWSTR lpCmdLine, int nCmdShow)

{

MainFrame f;

return f.show(hInstance,0,0,800,600,TRUE);

}


The code is ready to be compiled and run, but we must first change a couple of the project properties so that our application can find and link with the SolidWidgets library. To do that, right-click the project node inside the Solution Explorer view and select Properties from the context menu.







Now that our project has been configured to properly link with the SolidWidgets library, we have one more step to do before we can compile and run our application. We must configure the visual c++ directories so it can find the SolidWidgets include and library files.


To do that under Visual Studio 2005 and 2008,








To do the same things under Visual Studio 2020,




We are now ready to compile and run our application.






Let’s now go back to the code to make some changes, so we can add some components to our main window.


Let’s first add a menu bar to our application.



void windowOpening()

{

// Add a menu bar to the frame window

if(addMenuBar())

{

swMenu *fileMenu = appendChildMenu(L"File");

if(fileMenu!= NULL)

{

fileMenu->appendChildItem(L"New",1);

fileMenu->appendChildItem(L"Open",2);

fileMenu->appendChildItem(L"Exit",3);

}

swMenu *editMenu = appendChildMenu(L"Edit");

if(editMenu!= NULL)

{

editMenu->appendChildItem(L"Copy",11);

editMenu->appendChildItem(L"Paste",12);

}

}

}




Before adding components to the window, I will take a little time to explain the layout manager used in SolidWidgets. SolidWidgets uses the grid layout concept. A grid layout allows the developer to layout components within a grid, specifying exactly how many rows and columns each component occupies within its parent. Components are always added to a panel, and the panel is always configured as a grid. Before adding components to a panel, we must first tell the panel how many rows and columns it should contain. We also tell the panel the width and height of each column and row respectively. There are 2 methods in the panel class that allows us to do that: addRow and addColumn.


A column can be configured to be of fixed width, or a filler column. A fixed width column will always occupy exactly the number of pixels that you configured it to be. A filler column will be allocated space based on the remaining horizontal pixels.


A row can be configured to be of fixed height, or a filler row. A fixed height row will always occupy exactly the number of pixels that you configured it to be. A filler row will be allocated space based on the remaining vertical pixels.


You can add a component, or a panel to a panel. This allows you to embed panels in a panel, and each child panel can have a completely different layout than its siblings.


Every frame and dialog has a root panel. In order to layout components in a dialog or a frame, you must first obtain a handle to the root panel by calling getContentPane(), and then you can add panels or components to it. The following is a typical method of laying out components in a frame or a dialog:


#include <swFrame.h>

#include <swLabel.h>

#include <swTextField.h>

#include <swTextArea.h>

#include <swButton.h>


class MainFrame: public swFrame

{

swLabel m_nameLabel;

swTextField m_nameField;

swLabel m_emailLabel;

swTextField m_emailField;

swLabel m_noteLabel;

swTextArea m_noteField;

swButton m_okButton,m_cancelButton;

public:

void windowOpening()

{

swPanel *panel = getContentPane();

if(panel!=NULL)

{

// Configure the panel layout

panel->setMargins(10,10,10,10);

panel->addRow(25);// fixed row of height 25-pixels

panel->addRow(25);// fixed row

panel->addRow(25);// fixed row

panel->addRow();// filler row

panel->addRow(25);// fixed row

panel->addColumn(180);

panel->addColumn(150);

panel->addColumn();

panel->addColumn(90);// OK button

panel->addColumn(90);// Cancel Button

// Add the child components

panel->addChild(&m_nameLabel,0,0,1,1);

panel->addChild(&m_nameField,1,0,1,1);

panel->addChild(&m_emailLabel,0,1,1,1);

panel->addChild(&m_emailField,1,1,4,1);// occupies 4 columns

panel->addChild(&m_noteLabel,0,2,1,1);

panel->addChild(&m_noteField,1,2,4,2);// 4 columns and 2 rows

panel->addChild(&m_okButton,3,4,1,1);

panel->addChild(&m_cancelButton,4,4,1,1);

}

m_nameLabel.setText(L”Name:”);

m_emailLabel.setText(L”Email:”);

m_noteLabel.setText(L”Note:”);

m_okButton.setText(L”OK”);

m_okButton.setActionListener(this); // make this class listen to the button events

m_cancelButton.setText(L”Cancel”);

m_cancelButton.setActionListener(this); // make this class listen to the button events

}

};

So now if we combine the code above with the code from the previous sample above it, we get the following. Replace the content of MainFrame.cpp with the code below, compile and run it, you should get a window looking like this:


#include <swFrame.h>

#include <swLabel.h>

#include <swTextField.h>

#include <swTextArea.h>

#include <swButton.h>


class MainFrame: public swFrame

{

swLabel m_nameLabel;

swTextField m_nameField;

swLabel m_emailLabel;

swTextField m_emailField;

swLabel m_noteLabel;

swTextArea m_noteField;

swButton m_okButton,m_cancelButton;

public:

// You must set the SolidWidgets license file in the constructor of your frame class

MainFrame()

{

setLicenseFile(L”c:\\program files\\SolidWidgets\\SolidWidgetsLicense.swl”,L””);

}

// Your frame class must implement the function getApplicationTitle, which must

// return the title of your application. This title must match the title that you licensed

// with SolidWidgets. If you’re using the trial version, return “SolidWidgets UI demo”.

swString getApplicationTitle()

{

return L”SolidWidgets UI demo”;

}

void windowOpening()

{

// Add a menu bar to the frame window

if(addMenuBar())

{

swMenu *fileMenu = appendChildMenu(L"File");

if(fileMenu!= NULL)

{

fileMenu->appendChildItem(L"New",1);

fileMenu->appendChildItem(L"Open",2);

fileMenu->appendChildItem(L"Exit",3);

}

swMenu *editMenu = appendChildMenu(L"Edit");

if(editMenu!= NULL)

{

editMenu->appendChildItem(L"Copy",11);

editMenu->appendChildItem(L"Paste",12);

}

}


swPanel *panel = getContentPane();

if(panel!=NULL)

{

// Configure the panel layout

panel->setMargins(10,10,10,10);

panel->addRow(25);// fixed row of height 25-pixels

panel->addRow(25);// fixed row

panel->addRow(25);// fixed row

panel->addRow();// filler row

panel->addRow(25);// fixed row

panel->addColumn(180);

panel->addColumn(150);

panel->addColumn();

panel->addColumn(90);// OK button

panel->addColumn(90);// Cancel Button

// Add the child components

panel->addChild(&m_nameLabel,0,0,1,1);

panel->addChild(&m_nameField,1,0,1,1);

panel->addChild(&m_emailLabel,0,1,1,1);

panel->addChild(&m_emailField,1,1,4,1);// occupies 4 columns

panel->addChild(&m_noteLabel,0,2,1,1);

panel->addChild(&m_noteField,1,2,4,2);// 4 columns and 2 rows

panel->addChild(&m_okButton,3,4,1,1);

panel->addChild(&m_cancelButton,4,4,1,1);

}

m_nameLabel.setText(L”Name:”);

m_emailLabel.setText(L”Email:”);

m_noteLabel.setText(L”Note:”);

m_okButton.setText(L”OK”);

m_okButton.setActionListener(this); // make this class listen to the button events

m_cancelButton.setText(L”Cancel”);

m_cancelButton.setActionListener(this); // make this class listen to the button events

}


void windowOpened()

{

}


BOOL windowClosing()

{

if(showPromptMessage(L”Close Window?”,L”Confirm Close”,MB_YESNO) == IDYES)

return TRUE;

return FALSE;

}


void windowClosed()

{

}


void actionPerformed(long sourceId,long eventid, const swString& eventName)

{

}

};


int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPWSTR lpCmdLine, int nCmdShow)

{

MainFrame f;

return f.show(hInstance,0,0,800,600,TRUE);

}



To make out window close when the OK or cancel buttons are clicked, add the following code in actionPerformed:


void actionPerformed(long sourceId, long eventid, const swString& eventName)

{

if(sourceId == m_okButton.getID())

dispose(1);

else if(sourceId == m_cancelButton.getID())

dispose(0);

}