HeaderPane
Main
Hide

SftButton/DLL 3.0 - Button Control

Share Link
Print

Simple Sample (C)

This sample illustrates basic SftButton creation in a dialog. It covers registering the button window class, placing a button from a dialog resource, configuring theme and dark-mode behavior, and handling BN_CLICKED notifications.

The source code is located at C:\Program Files (x86)\Softelvdm\SftButton DLL 3.0\Samples\C\Simple\Simple.c or C:\Program Files\Softelvdm\SftButton DLL 3.0\Samples\C\Simple\Simple.c (on 32-bit Windows versions).

/****************************************************************************/
/* SftButton/DLL 3.0 - Button Control for C/C++                            */
/* Copyright (C) 2026  Softel vdm, Inc. All Rights Reserved.               */
/****************************************************************************/

#include <windows.h>
#include <commctrl.h>

#include "SftButton.h"                  // SftButton/DLL Header File

#define SFTDARKMODE_IMPLEMENTATION
#include "SftDarkMode.h"                // Dark mode support

#include "resource.h"                   // application resources

/**********************************************************************/
/*                              Globals                               */
/**********************************************************************/

HINSTANCE g_hInst;                      // App Instance Handle

static LPVOID g_pImg1 = NULL;           // GDI+ image for Button2 Picture1
static LPVOID g_pImg2 = NULL;           // GDI+ image for Button2 Picture2

/**********************************************************************/
/*                          Main Dialog Proc                          */
/**********************************************************************/

INT_PTR CALLBACK Main_DialogProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
    INT_PTR dmResult;

    switch (msg) {
    case WM_INITDIALOG: {
        HWND hwndButton1 = GetDlgItem(hwndDlg, IDC_SFTBUTTON1);
        HWND hwndButton2 = GetDlgItem(hwndDlg, IDC_SFTBUTTON2);
        HWND hwndButton3 = GetDlgItem(hwndDlg, IDC_SFTBUTTON3);

        // Set dialog icon
        SendMessage(hwndDlg, WM_SETICON, ICON_BIG, (LPARAM)LoadIcon(g_hInst, MAKEINTRESOURCE(IDI_ICON1)));
        SendMessage(hwndDlg, WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(g_hInst, MAKEINTRESOURCE(IDI_ICON1)));

        // Apply dark mode to dialog
        SftDarkMode_ApplyToDialog(hwndDlg);

        // Configure SftButton control 1
        {
            SFTBUTTON_CONTROL Ctl;
            Ctl.cbSize = sizeof(SFTBUTTON_CONTROL);
            SftButton_GetControlInfo(hwndButton1, &Ctl);
            Ctl.nDarkMode = SFTBUTTON_DARKMODE_AUTO;/* automatic dark mode support */
            SftButton_SetControlInfo(hwndButton1, &Ctl);
        }

        // Configure SftButton control 2
        {
            SFTBUTTON_CONTROL Ctl;
            Ctl.cbSize = sizeof(SFTBUTTON_CONTROL);
            SftButton_GetControlInfo(hwndButton2, &Ctl);
            Ctl.nDarkMode = SFTBUTTON_DARKMODE_AUTO;/* automatic dark mode support */
            Ctl.Text.align = SFT_PICALIGN_LEFT;/* text horizontal alignment */
            g_pImg1 = SftButton_LoadGDIPlusImageFromFile(TEXT("Res\\pic_world_48.png"));
            if (g_pImg1) Sft_SetPictureGDIPlusImage(&Ctl.Picture1, g_pImg1);
            Ctl.Picture1.align = SFT_PICALIGN_CENTER;
            Ctl.Picture1.valign = SFT_PICALIGN_VCENTER;
            g_pImg2 = SftButton_LoadGDIPlusImageFromFile(TEXT("Res\\pic_go_32.png"));
            if (g_pImg2) Sft_SetPictureGDIPlusImage(&Ctl.Picture2, g_pImg2);
            Ctl.Picture2.align = SFT_PICALIGN_LEFT;
            Ctl.Picture2.valign = SFT_PICALIGN_VCENTER;

            SftButton_SetControlInfo(hwndButton2, &Ctl);
        }

        // Configure SftButton control 3
        {
            SFTBUTTON_CONTROL Ctl;
            Ctl.cbSize = sizeof(SFTBUTTON_CONTROL);
            SftButton_GetControlInfo(hwndButton3, &Ctl);
            Ctl.fShowDropDown = TRUE;/* show drop-down arrow */
            Ctl.nDarkMode = SFTBUTTON_DARKMODE_AUTO;/* automatic dark mode support */
            SftButton_SetControlInfo(hwndButton3, &Ctl);
        }

        CheckDlgButton(hwndDlg, IDC_USE_THEMES, BST_CHECKED);
        CheckDlgButton(hwndDlg, IDC_DARK_MODE, SftDarkMode_IsActive() ? BST_CHECKED : BST_UNCHECKED);

        return TRUE;
    }

    case WM_COMMAND:
        switch (LOWORD(wParam)) {
        case IDCANCEL:
            EndDialog(hwndDlg, 0);
            return TRUE;
        case IDC_SFTBUTTON1:
            if (HIWORD(wParam) == BN_CLICKED)
                MessageBox(hwndDlg, TEXT("Button 1 clicked!"), TEXT("SftButton/DLL"), MB_OK);
            return TRUE;
        case IDC_SFTBUTTON2:
            if (HIWORD(wParam) == BN_CLICKED)
                MessageBox(hwndDlg, TEXT("Button 2 clicked!"), TEXT("SftButton/DLL"), MB_OK);
            return TRUE;
        case IDC_SFTBUTTON3:
            if (HIWORD(wParam) == BN_CLICKED)
                MessageBox(hwndDlg, TEXT("Button 3 clicked!"), TEXT("SftButton/DLL"), MB_OK);
            else if (HIWORD(wParam) == SFTBUTTONN_DROPDOWNCLICK)
                MessageBox(hwndDlg, TEXT("Button 3 drop-down clicked!"), TEXT("SftButton/DLL"), MB_OK);
            return TRUE;
        case IDC_USE_THEMES:
            if (HIWORD(wParam) == BN_CLICKED) {
                int useThemes = IsDlgButtonChecked(hwndDlg, IDC_USE_THEMES) == BST_CHECKED
                              ? SFTBUTTON_THEME_YES : SFTBUTTON_THEME_NO;
                int ids[] = { IDC_SFTBUTTON1, IDC_SFTBUTTON2, IDC_SFTBUTTON3 };
                for (int i = 0; i < (int)(sizeof(ids) / sizeof(ids[0])); ++i) {
                    HWND hwndBtn = GetDlgItem(hwndDlg, ids[i]);
                    SFTBUTTON_CONTROL Ctl;
                    Ctl.cbSize = sizeof(SFTBUTTON_CONTROL);
                    SftButton_GetControlInfo(hwndBtn, &Ctl);
                    Ctl.nUseThemes = useThemes;
                    SftButton_SetControlInfo(hwndBtn, &Ctl);
                }
            }
            return TRUE;
        case IDC_DARK_MODE:
            if (HIWORD(wParam) == BN_CLICKED) {
                BOOL fDark = IsDlgButtonChecked(hwndDlg, IDC_DARK_MODE) == BST_CHECKED;
                int btnMode = fDark ? SFTBUTTON_DARKMODE_ON : SFTBUTTON_DARKMODE_OFF;
                int ids[] = { IDC_SFTBUTTON1, IDC_SFTBUTTON2, IDC_SFTBUTTON3 };
                for (int i = 0; i < (int)(sizeof(ids) / sizeof(ids[0])); ++i)
                    SftButton_SetDarkMode(GetDlgItem(hwndDlg, ids[i]), btnMode);
                SftDarkMode_SetActive(fDark);
                SftDarkMode_ApplyToDialog(hwndDlg);
                InvalidateRect(hwndDlg, NULL, TRUE);
            }
            return TRUE;
        }
        break;

    case WM_DESTROY:
        // Free GDI+ images now that the control no longer references them.
        if (g_pImg1) { SftButton_FreeGDIPlusImageLoadedFromFile(g_pImg1); g_pImg1 = NULL; }
        if (g_pImg2) { SftButton_FreeGDIPlusImageLoadedFromFile(g_pImg2); g_pImg2 = NULL; }
        break;

    case WM_SETTINGCHANGE:
        // dark mode handling
        if (lParam && lstrcmpW((LPCWSTR)lParam, L"ImmersiveColorSet") == 0) {
            SftDarkMode_Init();
            SftDarkMode_ApplyToDialog(hwndDlg);
            // Forward to the SftButton controls so they can update (AUTO mode)
            SendMessage(GetDlgItem(hwndDlg, IDC_SFTBUTTON1), WM_SETTINGCHANGE, wParam, lParam);
            SendMessage(GetDlgItem(hwndDlg, IDC_SFTBUTTON2), WM_SETTINGCHANGE, wParam, lParam);
            SendMessage(GetDlgItem(hwndDlg, IDC_SFTBUTTON3), WM_SETTINGCHANGE, wParam, lParam);
            InvalidateRect(hwndDlg, NULL, TRUE);
        }
        return TRUE;
    }

    if (SftDarkMode_HandleDialogMessage(hwndDlg, msg, wParam, lParam, &dmResult))
        return dmResult;

    return FALSE;
}

/**********************************************************************/
/*                              WinMain                               */
/**********************************************************************/

int PASCAL WinMain(HINSTANCE hinst, HINSTANCE hinstPrev, LPSTR lpszCmdLine, int cmdShow)
{
    INITCOMMONCONTROLSEX icc;

    g_hInst = hinst;

    icc.dwSize = sizeof(icc);
    icc.dwICC = ICC_WIN95_CLASSES;
    InitCommonControlsEx(&icc);

    // Register with SftButton/DLL
    SftButton_RegisterApp(hinst);

    // Initialize dark mode support
    SftDarkMode_Init();

    DialogBox(hinst, MAKEINTRESOURCE(IDD_MAIN), NULL, Main_DialogProc);

    // Unregister from SftButton/DLL
    SftButton_UnregisterApp(hinst);

    return 0;
}

Last Updated 04/23/2026 - (email)
© 2026 Softel vdm, Inc.