WinapiExtension
 All Classes Files Functions Variables Typedefs Pages
Introduction/Comparison

Table of Contents

The best start for a GUI framework may be to show what a minimal program using this framework would look like. This code will create an empty top level window with client size 400x400, that fits in nicely with how the rest of the OS looks.

I put in some examples from other frameworks as well, to make a comparison easier. As expected, higher level frameworks like wxWidgets and Qt excapsulate the entire initialization and therefore require less code.

Example with WinapiExtension

Creating the window is straightforward. The WinMain function is visible like in pure Winapi programs.

#include <windows.h>
class MainWindow : public Window
{
public:
MainWindow()
{
Bind(WM_DESTROY, &MainWindow::OnDestroy, this);
Create(WindowParams().InnerSize(400, 400).Title(L"Title"));
}
void OnDestroy(msgBase& m) { PostQuitMessage(0); }
};
int WINAPI WinMain(HINSTANCE instance, HINSTANCE prevInstance, LPSTR cmdLine, int cmdShow)
{
Font applicationFont;
applicationFont.CreateDefaultFont();
Window::SetDefaultFont(applicationFont);
MainWindow main;
main.Show(cmdShow);
MSG msg;
while(GetMessage(&msg, 0, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}

Example with MFC

In MFC we need 5 lines of code to set the client size of a window. This is just one point where MFC is very verbose.

#include <afxwin.h>
class MainWindow : public CFrameWnd
{
public:
MainWindow()
{
Create(AfxRegisterWndClass(0, LoadCursor(NULL, IDC_ARROW), GetSysColorBrush(COLOR_BTNFACE)),
L"Title",
WS_OVERLAPPEDWINDOW,
CRect());
CRect windowRect;
GetWindowRect(windowRect);
CRect adjRect(0, 0, 400, 400);
AdjustWindowRectEx(adjRect, GetStyle(), FALSE, GetExStyle());
MoveWindow(windowRect.left, windowRect.top, adjRect.Width(), adjRect.Height());
SendMessageToDescendants(WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT));
}
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(MainWindow, CFrameWnd)
END_MESSAGE_MAP()
class App : public CWinApp
{
public:
BOOL InitInstance()
{
m_pMainWnd = new MainWindow();
m_pMainWnd->ShowWindow(m_nCmdShow);
return TRUE;
}
};
App theApp;

Example with wxWidgets

#include <wx/wx.h>
class MainWindow : public wxFrame
{
MainWindow()
: wxFrame(0, 0, "Title")
{
SetClientSize(400, 400);
Show();
};
class App : public wxApp
{
bool OnInit()
{
SetTopWindow(new MainWindow());
return true;
}
};
IMPLEMENT_APP(App)

Example with Qt

#include <QMainWindow>
#include <QApplication>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow()
: QMainWindow(0)
{
resize(400, 400);
}
private slots:
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

Example with pure Winapi

This is how it looks without WinapiExtension. Very efficient code, but very hard to maintain.

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wp, LPARAM lp)
{
switch(message)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, message, wp, lp);
}
int WINAPI WinMain(HINSTANCE instance, HINSTANCE prevInstance, LPSTR cmdLine, int cmdShow)
{
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(wc));
wc.cbSize = sizeof(wc);
wc.style = CS_DBLCLKS;
wc.lpfnWndProc = WndProc;
wc.hInstance = instance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wc.hbrBackground = GetSysColorBrush(COLOR_BTNFACE);
wc.lpszClassName = L"MainWindow";
RegisterClassEx(&wc);
DWORD style = WS_OVERLAPPEDWINDOW;
RECT adjRect;
SetRect(&adjRect, 0, 0, 400, 400);
AdjustWindowRect(&adjRect, style, FALSE);
HWND main = CreateWindow(
wc.lpszClassName,
L"Title",
style,
CW_USEDEFAULT,
CW_USEDEFAULT,
adjRect.right - adjRect.left,
adjRect.bottom - adjRect.top,
0,
0,
instance,
0);
ShowWindow(main, cmdShow);
MSG msg;
while(GetMessage(&msg, 0, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}