Welcome to Software Laboratory of Antillia.com.
Future Windows and Linux Programming
|
What is the entry point of a Windows program?
How to initialize the context of a Windows program?
What is the event loop for a Windows program?
How to set parameters to a window?
How to create a window?
How to create a top-level window of SDI(Single Document Interface)?
How to register an event-handler method to handle a Windows message?
How to create a top-level window of MDI(Multiple Document Interface)?
How to create a MDI-child window?
How to create a window of Windows Controls?
How to register a callback method to handle WM_COMMAND message for Windows Controls?
How to create a window of Windows Common Controls?
How to register a callback method to handle WM_NOTIFY message for Windows Controls?
How to add a menu to a top-level window and handle a menu-selection event?
How to create a popup window and popup it?
How to create a modal/modeless dialog and popup it?
How to create an about-dialog and popup it?
How to create a common dialog?
How to handle WM_SIZE message in the subclass of Composite or ApplicationView?
How to use a DefaultLayoutManager in a subclass of ApplicationView?
How to use a FlowLayout in a subclass of Composite or ApplicationView?
How to use a GridLayout in a subclass of Composite or ApplicationView?
How to use a BorderLayout in a subclass of Composite or ApplicationView?
How to debug a program in SOL9?
How to create a list and sort it?
How to create a hash table and use it?
How to sort an array of int, char* or Object* ?
How to create a thread and use it?
Q:What is the entry point of a Windows program?
A:It is Main(int argc, TCHAR** argv) function.
#include <sol\Application.h>
void Main(int argc, TCHAR** argv)
{
// Do something.
}
Q:How to initialize the context of a Windows program?
A:Use the class Application.
#include <sol\Application.h>
void Main(int argc, TCHAR** argv)
{
const TCHAR* appClass = _T("Sample");
Application applet(appClass, argc, argv);
// Do something.
}
Q:What is the event loop for a Windows program?
A:Call run method of the class Application.
#include <sol\Application.h>
void Main(int argc, TCHAR** argv)
{
const TCHAR* appClass = _T("Sample");
Application applet(appClass, argc, argv);
// Enter an infinite loop of repeating
// GetMessage,TranslateMessage and DispatchMessage.
applet.run();
}
Q:How to set parameters to a window?
A:Use the class Args and call set method.
Args args;
args.set(XmNx, 100);
args.set(XmNy, 10);
args.set(XmNwidth, 200);
args.set(XmNheight, 400);
// Pass the variable args to a Constructor
ApplicationView view(applet, _T(""), args);
Q:How to create a window?
A:Call a constructor of the SOL++ window class.
Args args;
args.set(XmNwidth, 200);
args.set(XmNheight, 400);
// Create a window of class ApplicationView
ApplicationView view(applet, _T("Sample"), args);
Q:How to create a top-level window of SDI(Single Document Interface)?
A:Define a subclass of the ApplicationView class and call a constructor.
#include <sol\ApplicationView.h>
namespace SOL {
class AppFrame :public ApplicationView {
public:
// Constructor
AppFrame(Application& applet, const TCHAR* caption, Args& args)
:ApplicationView(applet, caption, args)
{
//
}
};
}
void Main(int argc, TCHAR** argv)
{
const TCHAR* appClass = _T("Sample");
Application applet(appClass, argc, argv);
Args args;
AppFrame frame(applet, appClass, args);
frame.realize(); // Show the window.
applet.run(); // Enter an event loop.
}
Q:How to register an event-handler method to handle a Windows message?
A:Use the method addEventHandler in the class View.
#include <sol\ApplicationView.h>
#include <sol\PaintDC.h>
namespace SOL {
class AppFrame :public ApplicationView {
long paint(Event& event) {
// This is called automatically
// whenever WM_PAINT message yields in this AppFrame.
// Never forget to create an instance of PaintDC.
PaintDC pdc(this);
const TCHAR* text = _T("Hello world");
pdc.textOut(100, 100, text, strlen(text));
return 0L;
}
public:
// Constructor
AppFrame(Application& applet, const TCHAR* caption, Args& args)
:ApplicationView(applet, caption, args)
{
// Register paint method of this class to this window
// so that paint is called whenever WM_PAINT message yields.
addEventHandler(WM_PAINT, this,
(Handler)&AppFrame::paint, null);
}
};
}
void Main(int argc, TCHAR** argv)
{
const TCHAR* appClass = _T("Sample");
Application applet(appClass, argc, argv);
Args args;
AppFrame frame(applet, appClass, args);
frame.realize(); // Show the window.
applet.run(); // Enter an event loop.
}
Q:How to create a top-level window of MDI(Multiple Document Interface)?
A:Define a subclass of the MdiFrame class and call a constructor.
#include <sol\MdiFrame.h>
namespace SOL {
class MdiMainFrame :public MdiFrame {
public:
// Constructor
MdiMainFrame(Application& applet, const TCHAR* caption, Args& args)
:MdiFrame(applet, caption, args)
{
//
}
};
}
void Main(int argc, TCHAR** argv)
{
const TCHAR* appClass = _T("Sample");
Application applet(appClass, argc, argv);
Args args;
MdiMainFrame frame(applet, appClass, args);
frame.realize(); // Show the window.
applet.run(); // Enter an event loop.
}
Q:How to create a MDI-child window?
A:Define a subclass of the MdiChild class and call a constructor.
#include <sol\MdiChild.h>
namespace SOL {
class MdiChildWindow :public MdiChild {
public:
// Constructor
MdiChildWindow(MdiClient* mdiClient, const TCHAR* caption, Args& args)
:MdiChild(mdiClient, caption, args)
{
//
}
};
}
Q:How to create a window of Windows Controls?
A:Use the class PushButton, ComboBox, GroupBox, ListBox, RadioButton, ScrollBar,
ScrolledText, Static, ScrolledText, TextField, ToggleButton.
#include <sol\ApplicationView.h>
#include <sol\PushButton.h>
namespace SOL {
class AppFrame :public ApplicationView {
PushButton* ok;
public:
// Constructor
AppFrame(Application& applet, const TCHAR* caption, Args& args)
:ApplicationView(applet, caption, args)
{
Args ar;
ar.set(XmNx, 100);
ar.set(XMNy, 10);
// Create an instance of PushButton.
ok = new PushButton(this, _T("OK"), ar);
}
};
}
Q:How to register a callback method to handle WM_COMMAND message for Windows Controls?
A:Use the method addCallback in the class View.
#include <sol\ApplicationView.h>
#include <sol\PushButton.h>
namespace SOL {
class AppFrame :public ApplicationView {
PushButton* ok;
void okPressed(Action& action) {
// This is called automatically
// whenever ok is activated.
}
public:
// Constructor
AppFrame(Application& applet, const TCHAR* caption, Args& args)
:ApplicationView(applet, caption, args)
{
Args ar;
ok = new PushButton(this, _T("OK"), ar);
// Register okPressed method of this class to this ok button
// so that okPressed is called whenever ok is activated.
ok -> addCallback(XmNactivateCallback, this,
(Callback)&AppFrame::okPressed, null);
}
};
}
Q:How to create a window of Windows Common Controls?
A:Use the class Animator, Header, ListView, StatusBar, Tab,
ToolBar, TrackBar, TreeView, Updown.
#include <sol\ApplicationView.h>
#include <sol\ListView.h>
namespace SOL {
class AppFrame :public ApplicationView {
ListView* listView;
public:
// Constructor
AppFrame(Application& applet, const TCHAR* caption, Args& args)
:ApplicationView(applet, caption, args)
{
Args ar;
ar.set(XmNx, 10);
ar.set(XMNy, 10);
ar.set(XmNwidth, 300);
ar.set(XmNheight, 300);
// Create an instance of ListView.
listView = new ListView(this, _T(""), ar);
}
};
}
Q:How to register a callback method to handle WM_NOTIFY message for Windows Controls?
A:Use the method addCallback in the class View.
#include <sol\ApplicationView.h>
#include <sol\ListView.h>
namespace SOL {
class AppFrame :public ApplicationView {
ListView* listView;
void itemChanged(Action& action) {
// This is called automatically
// whenever an item is changed.
}
public:
// Constructor
AppFrame(Application& applet, const TCHAR* caption, Args& args)
:ApplicationView(applet, caption, args)
{
Args ar;
ar.set(XmNwidth, 300);
ar.set(XmNheight, 300);
listView = new ListView(this, _T(""), ar);
// Register itemChanged method of this class to this listView
// so that itemChanged is called whenever a selection is changed.
listView -> addCallback(XmNitemChangedCallback, this,
(Callback)&AppFrame::itemChanged, null);
}
};
}
Q:How to add a menu to a top-level window and handle a menu-selection event?
A:Specify a menu-name in a resource file with the XmNmenuName creating a window
and use addCallback method to handle a menu-selection event.
#include <sol\ApplicationView.h>
namespace SOL {
class AppFrame :public ApplicationView {
void itemSelected(Action& action) {
// This is called automatically whenever a menu-item is selected.
Event& e = action.getEvent();
if (e.getMenuId() == IDM_EXIT) {
exit(action);
}
}
public:
// Constructor
AppFrame(Application& applet, const TCHAR* caption, Args& args)
:ApplicationView(applet, caption, args)
{
// Register itemSelected method of this class to this window
// so that itemSelected is called whenever a menu-item is selected.
addCallback(XmNmenuCallback, IDM_OPEN, this,
(Callback)&AppFrame::itemSelected, null);
//
addCallback(XmNmenuCallback, IDM_EXIT, this,
(Callback)&AppFrame::itemSelected, null);
}
};
}
void Main(int argc, TCHAR** argv)
{
const TCHAR* appClass = _T("Sample");
Application applet(appClass, argc, argv);
Args args;
// Specify the menuname in a resource file.
args.set(XmNmenuName, _T("SampleMenu"));
AppFrame frame(applet, appClass, args);
frame.realize(); // Show the window.
applet.run(); // Enter an event loop.
}
Q:How to create a popup window and popup it?
A:Use the class PopupView and call a popup method.
#include <sol\ApplicationView.h>
#include <sol\PopupView.h>
#include <sol\PushButton.h>
namespace SOL {
class AppFrame :public ApplicationView {
PopupView* popupView;
PushButton* ok;
public:
// Constructor
AppFrame(Application& applet, const TCHAR* caption, Args& args)
:ApplicationView(applet, caption, args)
{
Args ar;
ok = new PushButton(this, _T("OK"), ar);
ar.reset();
ar.set(XmNwidth, 300);
ar.set(XmNheight, 300);
// Create an instanace of PopupView.
popupView = new PopupView(this, _T(""), ar);
// Add the popup method of PopupView to ok button
// so that popupView is popped up whenever ok is activated.
ok -> addCallback(XmNactivateCallback, popupView,
(Callback)&PopupView::popup, null);
}
};
}
Q:How to create a modal/modeless dialog and popup it?
A:Use the class ModalDialog/ModelessDialog and call a popup method.
#include <sol\ApplicationView.h>
#include <sol\ModelessDialog.h>
#include <sol\PushButton.h>
namespace SOL {
class AppFrame :public ApplicationView {
ModelessDialog* modelessDialog;
PushButton* ok;
public:
// Constructor
AppFrame(Application& applet, const TCHAR* caption, Args& args)
:ApplicationView(applet, caption, args)
{
Args ar;
ok = new PushButton(this, _T("OK"), ar);
ar.reset();
ar.set(XmNwidth, 300);
ar.set(XmNheight, 300);
// Create an instanace of ModelessDialog.
modelessDialog = new ModelessDialog(this, _T(""), ar);
// Add the popup method of ModelessDialog to ok button
// so that modelessDialog is popped up whenever ok is activated.
ok -> addCallback(XmNactivateCallback, modelessDialog,
(Callback)&ModelessDialog::popup, null);
}
};
}
Q:How to create an about-dialog and popup it?
A:Use the class AboutDialog and call a popup method.
#include <sol\ApplicationView.h>
#include <sol\AboutDialog.h>
#include <sol\PushButton.h>
namespace SOL {
class AppFrame :public ApplicationView {
AboutDialog* aboutDialog;
PushButton* ok;
public:
// Constructor
AppFrame(Application& applet, const TCHAR* caption, Args& args)
:ApplicationView(applet, caption, args)
{
Args ar;
ok = new PushButton(this, _T("OK"), ar);
ar.reset();
ar.set(XmNwidth, 300);
ar.set(XmNheight, 300);
ar.set(XmNtemplate, _T("VersionDialog"));
// Create an instanace of AboutDialog.
aboutDialog = new AboutDialog(this, "", ar);
// Add the popup method of AboutDialog to ok button
// so that AboutDialog is popped up whenever ok is activated.
ok -> addCallback(XmNactivateCallback, aboutDialog,
(Callback)&AboutDialog::popup, null);
}
};
}
Q:How to create a common dialog?
A:Use the class FileDialog, ColorDialog, FindDialog, ReplaceDialog,
FontDialog, PrintDialog and PageSetupDialog.
#include <sol\ApplicationView.h>
#include <sol\FileDialog.h>
#include <sol\PushButton.h>
namespace SOL {
class AppFrame :public ApplicationView {
FileDialog* fileDialog;
PushButton* ok;
public:
// Constructor
AppFrame(Application& applet, const TCHAR* caption, Args& args)
:ApplicationView(applet, caption, args)
{
Args ar;
ok = new PushButton(this, _T("Open"), ar);
ar.reset();
// Create an instanace of FileDialog with OPEN mode.
fileDialog = new FileDialog(this, _T(""), ar);
// Add the popup method of FileDialog to ok button
// so that FileDialog is popped up whenever ok is activated.
ok -> addCallback(XmNactivateCallback, fileDialog,
(Callback)&FileDialog::popup, null);
}
};
}
Q:How to handle WM_SIZE message in the subclass of Composite or ApplicationView?
A:Define a method size in each subclass to override the size method in Composite or ApplicationView.
See also: How to use a DefaultLayoutManager in a subclass of ApplicationView?
#include <sol\ApplicationView.h>
#include <sol\ScrolledRichText.h>
namespace SOL {
class AppFrame :public ApplicationView {
ScrolledRichText* richText;
// Override size method in the super class ApplicationView
long size(Event& event) {
int w, h;
event.getSize(w, h);
// Reshape the richText window to fit in the client region of
// this AppFrame window.
richText -> reshape(0, 0, w, h);
return 0L;
}
public:
// Constructor
AppFrame(Application& applet, const TCHAR* caption, Args& args)
:ApplicationView(applet, caption, args)
{
Args ar;
richText = new ScrolledRichText(this, _T(""), ar);
}
};
}
Q:How to use a DefaultLayoutManager in a subclass of ApplicationView?
A:Call add method to let the DefaultLayoutManager manage the size of an internal window.
The DefaultLayoutManger reshapes an internal child window to fit in the client region of
the parent Window.
#include <sol\ApplicationView.h>
#include <sol\ScrolledRichText.h>
namespace SOL {
class AppFrame :public ApplicationView {
ScrolledRichText* richText;
public:
// Constructor
AppFrame(Application& applet, const TCHAR* caption, Args& args)
:ApplicationView(applet, caption, args)
{
Args ar;
richText = new ScrolledRichText(this, _T(""), ar);
// Simply call add method of ApplicationView to add this richText window to
// a managing list of DefaultLayoutManager in ApplicationView.
add(richText);
}
};
}
Q:How to use a FlowLayout in a subclass of Composite or ApplicationView?
A:Create an instanace of FlowLayout and call setLayout method to set it to the parent window.
To add a child window to the instance of FlowLayout, call add method of Composite.
#include <sol\ApplicationView.h>
#include <sol\PushButton.h>
#include <sol\FlowLayout.h>
namespace SOL {
class AppView :public ApplicationView {
PushButton* buttons[5];
FlowLayout* flowLayout;
public:
AppView(Application& applet, const TCHAR* label, Args& args)
:ApplicationView(applet, label, args)
{
Args ar;
// Create an instance of FlowLayout.
flowLayout = new FlowLayout();
// Call setLayout method to set the flowLayout.
setLayout(flowLayout);
for(int i = 0; i<5; i++) {
TCHAR string[128];
_stprintf(string, _T("Hello Button %d"), i);
ar.reset();
buttons[i] = new PushButton(this, string, ar);
// Add each button to the flowLayout.
add(buttons[i]);
}
}
};
}
Q:How to use a GridLayout in a subclass of Composite or ApplicationView?
A:Create an instanace of GridLayout and call setLayout method to set it to the parent window.
To add a child window to the instance of FlowLayout, call add method of Composite.
#include <sol\ApplicationView.h>
#include <sol\PushButton.h>
#include <sol\GridLayout.h>
namespace SOL {
class AppView :public ApplicationView {
PushButton* buttons[5];
GridLayout* gridLayout;
public:
AppView(Application& applet, const TCHAR* label, Args& args)
:ApplicationView(applet, label, args)
{
Args ar;
// Create an instance of GridLayout.
gridLayout = new GridLayout(3,2);
// Call setLayout method to set the gridLayout.
setLayout(gridLayout);
for(int i = 0; i<5; i++) {
TCHAR string[128];
_stprintf(string, _T("Button %d"), i);
ar.reset();
buttons[i] = new PushButton(this, string, ar);
// Add each button to the gridLayout.
add(buttons[i]);
}
}
};
}
Q:How to use a BorderLayout in a subclass of Composite or ApplicationView?
A:Create an instanace of BorderLayout and call setLayout method to set it to the parent window.
To add a child window to the instance of BorderLayout, call add method of Composite.
#include <sol\ApplicationView.h>
#include <sol\PushButton.h>
#include <sol\BorderLayout.h>
#include <sol\ScrolledText.h>
namespace SOL {
class AppView :public ApplicationView {
BorderLayout borderLayout;
PushButton* north;
PushButton* south;
PushButton* east;
PushButton* west;
ScrolledText* center;
public:
AppView(Application& applet, const TCHAR* label, Args& args)
:ApplicationView(applet, label, args)
{
Args ar;
// Set borderLayout.
setLayout(&borderLayout);
north = new PushButton(this, _T("north"), ar);
add(north, BorderLayout::NORTH);
ar.reset();
south = new PushButton(this, _T("south"), ar);
add(south, BorderLayout::SOUTH);
ar.reset();
west = new PushButton(this, _T("west"), ar);
add(west, BorderLayout::WEST);
ar.reset();
east = new PushButton(this, _T("east"), ar);
add(east, BorderLayout::EAST);
ar.reset();
center = new ScrolledText(this, _T("center"), ar);
add(center, BorderLayout::CENTER);
pack();
}
};
}
Q:How to debug a program in SOL9?
A:
Step1: Run Console program included in SOL9 samples.
Step2: Insert a Printf function of SOL9 into your program.
Step3: Compile and run your program.
Y
The Printf function takes variable number of arguments like an ordinay
printf function of C stdio library:
void Printf(const TCHAR* format,...);
But, you have to use the string "\r\n" for a new-line, not "\n".
// Include Stdio.h of SOL9 to use Printf function.
#include <sol\ApplicationView.h>
#include <sol\Stdio.h>
namespace SOL {
class AppFrame :public ApplicationView {
public:
AppFrame(Application& applet, const TCHAR* label, Args& args)
:ApplicationView(applet, label, args)
{
Printf(_T("AppFrame::AppFrame() - constructor\r\n"));
}
~AppFrame() {
Printf(_T("AppFrame::~AppFrame() - destructor\r\n"));
}
};
}
void Main(int argc, TCHAR** argv)
{
const TCHAR* appClass = _T("Sample");
Application applet(appClass, argc, argv);
Args args;
AppFrame frame(applet, appClass, args);
frame.realize();
applet.run();
}
Q:How to create a list and sort it?
A:Create an instanace of LinkedList or DoublyLinkedList and call sort method of thoses classes.
To add an instance of Object or its subclasses to the list, call add method of thoses classes.
#include <sol\DoublyLinkedList.h>
namespace SOL {
class ListSample {
public:
ListSample() {
DoublyLinkedList list;
// Adding some String objects to the list
list.add(new String(_T("orange")));
list.add(new String(_T("apple")));
list.add(new String(_T("melon")));
list.add(new String(_T("banana")));
// Reverse the order the elements of ths list.
list.reverse();
// Sort the list in descending order.
list.sort(Sortable::DESCENDING);
// Sort the list in acending order.
list.sort(Sortable::ASCENDING);
}
};
}
Q:How to create a hash table and use it?
A:Create an instanace of HashTabble and call add, lookup, remove methods of the classes.
#include <sol\HashTable.h>
#include <sol\String.h>
namespace SOL {
class HashTableSample {
public:
HashTableSample() {
HashTable* table = new HashTable(1731);
// Adding some String objects to the list by using key of const TCHAR* type.
table -> add(_T("apple"), new String(_T"apple")));
table -> add(_T("melon"), new String(_T"melon")));
table -> add(_T("orange"), new String(_T"orange")));
table -> add(_T("banana"), new String(_T"banana")));
// Call lookup method.
String* object = (String*) table -> lookup(_T("orange"));
// Remove the object.
table -> remove(object);
}
};
}
Q:How to sort an array of int, char* or Object* ?
A:Create an instanace of QuickSorter thread and call sort method. After sorting,
the QuickSorter thread will terminate. Usually, you have to call wait method to confirm the
termination of the thread.
#include <sol\QuickSorter.h>
#include <sol\String.h>
namespace SOL {
class QuickSorterSample {
public:
QuickSorterSample() {
static int integers[] = {
1, 200, 3, 11, 0,
500, 20, 600, 30
};
static char* strings[] = {
"orange", "banana", "apple", "melon",
"2000", "1", "2001", "1999"
};
// Create an instance of QuickSorter thread.
QuickSorter qsorter1;
// Sort an array of integers.
qsorter1.sort(integers, XtNumber(integers));
qsorter1.wait();
// Create an instance of QuickSorter thread.
QuickSorter qsorter2;
// Sort an array of char*.
qsorter2.sort(strings, XtNumber(strings));
qsorter2.wait();
static Object* objects[10];
int num = 0;
objects[num++] = new String(_T("dog"));
objects[num++] = new String(_T("sheep"));
objects[num++] = new String(_T("pig"));
objects[num++] = new String(_T("cat"));
objects[num++] = new String(_T("hen"));
objects[num++] = new String(_T("ox"));
// Create an instance of QuickSorter thread.
QuickSorter qsorter3;
// Sort an array of instances of String.
qsorter3.sort(objects, num);
qsorter3.wait();
}
};
}
Q:How to create a thread and use it?
A:
Step1: Define a subclass of Thread class, and implement run method in the subclass.
Step2: Create an instance of the subclass by calling a constructor.
Step3: Call start method of Thread class to start the thread.
Step4: Call wait method to wait the termination of the thread.
Note: A thread instance created by a constructor is in suspended state, so you have
to call start method to start it.
#include <sol\Application.h>
#include <sol\Thread.h>
#include <sol\Stdio.h>
namespace SOL {
class StopWatch :public Thread {
int seconds;
public:
StopWatch(int interval) {
seconds = interval;
}
void run() {
int n = 0;
while (n < seconds) {
sleep(1000);
Printf(_T("StopWatch %d\r\n"), n++);
}
}
};
}
void Main(int argc, char** argv)
{
const char* appClass = _T("StopWatch");
Application applet(appClass, argc, argv);
StopWatch stopWatch(10); // Create an instance of StopWatch
stopWatch.start(); // Start the thread.
stopWatch.wait(); // Wait the termination.
MessageBox(NULL, _T("Thread terminated"), _T("StopWatch"), MB_OK);
}
|
Last modified: 5 May 2016
Copyright (c) 2000-2016 Antillia.com ALL RIGHTS RESERVED.