Home

Awesome

WinLamb

A lightweight modern C++11 library for Win32 API, using lambdas to handle Windows messages.

  1. Overview
  2. Setup
  3. Example
  4. Classes summary
  5. License

1. Overview

As far as I can remember, around 2002 I started wrapping all my Win32 routines in classes, to make them reusable to myself, to save my time. Through all these years it took the form of a real library, a thin abstraction layer over raw Win32. People who saw it often commented that it was good, so in 2017 I decided to publish it on GitHub.

Then I wrote CodeProject - WinLamb: using C++11 lambdas to handle Win32 messages, a comprehensive article explaining WinLamb's message handling model, with dialogs and also ordinary windows. Actually, features from C++14 and C++17 are used as well, as much as my compiler (Visual C++) allows it.

Beyond dialog/window message handling, WinLamb also has wrappers for most native Windows controls (textbox, listview, etc.), along with other utility classes (strings, file I/O, COM wrappers, etc.) which play nice together. These controls and utilities, however, are not mandatory: you can use your own classes upon the basic dialog/window infrastructure.

WinLamb by no means covers the whole Win32 API, simply because it's too huge. It just wraps some things. New features are constantly being added, though.

There is a great WinLamb presentation from Utah C++ Programmers here: https://youtu.be/2K52YX-vHv4

2. Setup

WinLamb is a header-only library. You can clone the repository or simply download the files; once referenced in your source code, it should work right away.

It has been tested with Visual C++ 2017.

2.1. Windows 10 manifest file

There's an included win10.exe.manifest file, which you can add to your Visual Studio project. This manifest includes Common Controls and gives you Windows 10 support.

3. Example

This is a simple Win32 program written with WinLamb. Each window has a class, and messages are handled with C++11 lambdas using message crackers. There's no need to write a message loop or window registering.

Declaration: My_Window.h

#include "winlamb/window_main.h"

class My_Window : public wl::window_main {
public:
    My_Window();
};

Implementation: My_Window.cpp

#include "My_Window.h"

RUN(My_Window) // optional, generate WinMain call and instantiate My_Window

My_Window::My_Window()
{
    setup.wndClassEx.lpszClassName = L"SOME_CLASS_NAME"; // class name to be registered
    setup.title = L"This is my window";
    setup.style |= WS_MINIMIZEBOX;

    on_message(WM_CREATE, [this](wl::wm::create p) -> LRESULT
    {
        set_text(L"A new title for the window");
        return 0;
    });

    on_message(WM_LBUTTONDOWN, [](wl::wm::lbuttondown p) -> LRESULT
    {
        bool isCtrlDown = p.has_ctrl();
        long xPos = p.pos().x;
        return 0;
    });
}

3.1. Project examples

I've written the following examples showcasing different things:

More projects can be seen browsing winlamb topic.

4. Classes summary

Most files are named after the class they contain; for example, file "button.h" contains button class.

To create your windows, you inherit from these classes below. See the article and the examples to learn how to use them:

ClassDescription
dialog_controlInherit from this class to have a dialog to be used as a control within a parent window.
dialog_mainInherit from this class to have a dialog as the main window for your application.
dialog_modalInherit from this class to have a modal dialog popup.
dialog_modelessInherit from this class to have a dialog modeless popup.
window_controlInherit from this class to have an user-custom window control.
window_mainInherit from this class to have an ordinary main window for your application.

Wrappers and utilities:

ClassDescription
buttonWrapper to native button control.
checkboxWrapper to native checkbox control.
com::bstrWrapper to BSTR string, used with COM.
com::libSmart class to automate CoInitialize and CoUninitialize calls.
com::ptrWrapper to a COM pointer.
com::variantWrapper to VARIANT object, used with COM.
comboboxWrapper to native combobox control.
datetimeWrapper to SYSTEMTIME structure.
datetime_pickerWrapper to datetime picker control from Common Controls library.
gdi::dcWrapper to device context.
gdi::dc_painterWrapper to device context which calls BeginPaint/EndPaint automatically.
gdi::dc_painter_bufferedWrapper to device context which calls BeginPaint/EndPaint automatically with double-buffer.
downloadAutomates internet download operations.
executableExecutable-related utilities.
fileWrapper to a low-level HANDLE of a file.
file_iniWrapper to INI file.
file_mappedWrapper to a memory-mapped file.
fontWrapper to HFONT handle.
iconWrapper to HICON handle.
image_listWrapper to image list object from Common Controls library.
insert_order_mapVector-based associative container which keeps the insertion order.
labelWrapper to native static text control.
listviewWrapper to listview control from Common Controls library.
menuWrapper to HMENU handle.
pathUtilities to file path operations with std::wstring.
progress_taskbarAllows to show a progress bar in the taskbar button of the window, in green, yellow or red.
progressbarWrapper to progressbar control from Common Controls library.
radioWrapper to native radio button control.
radio_groupAutomates a group of native radio buttons.
resizerAllows the resizing of multiple controls when the parent window is resized.
scrollinfoAutomates SCROLLINFO operations.
statusbarWrapper to status control from Common Controls library.
strUtilities to std::wstring.
subclassManages window subclassing for a window.
sysdlgWrappers to system dialogs.
syspathRetrieves system paths.
textboxWrapper to native edit box control.
treeviewWrapper to treeview control from Common Controls library.
vecUtilities to std::vector.
versionParses version information from an EXE or DLL.
wndSimple HWND wrapper, base to all dialog and window classes.
xmlXML wrapper class to MSXML2 Windows library.
zipUtilities to work with zipped files.

5. License

Licensed under MIT license, see LICENSE.txt for details.