WinapiExtension
 All Classes Files Functions Variables Typedefs Pages
Creating a window

Subclassing Window

The window creation has seen some revision which ultimately lead to this super-compact solution.

class TutorialWindow : public Window
{
public:
void Create()
{
Window::Create(WindowParams().Style(WS_OVERLAPPEDWINDOW|WS_VISIBLE)
.InnerSize(640, 480)
.Text(L"I am a tutorial window"));
}
};

As shown, the standard way of creating a window is by subclassing Window. WindowParams is the only arguments, and is used to specify only the necessary parameters. By default, if you use a raw WindowParams, it will create a standard top level window.

Note
Win32 experts may notice that no WNDCLASS is explicitly created in this sample. That is because Window::Create() handles all this stuff internally. One can specfiy the properties of the WNDCLASS, e.g. using WindowParams::Cursor(), WindowParams::Icon(), WindowParams::Background(), but the framework will handle the rest.

Adding controls

If the window is not used for drawing, it should have some controls. These can be created using the global functions like CreateButton(). The controls can be assigned to Control wrappers like Edit or Button, which allow to call welldefined methods on the controls.

class TutorialWindow : public Window
{
void Create()
{
// create the window
Window::Create(WindowParams().Style(WS_OVERLAPPEDWINDOW|WS_VISIBLE)
.InnerSize(640, 480)
.Text(L"I am a tutorial window"));
// first row
CreateStatic(GetHandle(), L"Login", 5, 5, 90, 25);
editLogin = CreateEdit(GetHandle(), 0, 100, 5, 100, 25);
// second row
CreateStatic(GetHandle(), L"Password", 5, 35, 90, 25);
editPassword = CreateEdit(GetHandle(), 0, 100, 35, 100, 25, ES_AUTOHSCROLL|ES_PASSWORD);
// comfirmation button
Button buttonConnect = CreateButton(GetHandle(), L"Connect", 0, 100, 65, 100, 25);
editLogin.SetFocus();
editPassword.SetPasswordChar(L'X');
// bind message handlers
BindCommand(buttonConnect, &TutorialWindow::OnConnect, this)
}
void OnConnect(msgCommand& m)
{
tstring login = editLogin.GetText();
tstring password = editPassword.GetText();
// we leave the rest to the imagination
}
Edit editLogin;
Edit editPassword;
};

This sample creates two textfields for a login form. As you can see, the button does not have a window ID, it is set to null. The response function is bound to the button handle, thus creating an unambiguous connection.