'https://www.codeproject.com/Articles/559385/Custom-Controls-in-Win32-API-The-Basics

' application doing actually nothing but creating a main window and
' the custom control as its only child
' MWind.o2bas

$ filename "MWind.exe"

'uses rtl32
uses rtl64
uses WinUtil
! SetClassLongPtr lib "user32.dll" (sys hWnd, int nIndex , sys dwNewLong) as sys


#autodim off

% GCL_HBRBACKGROUND = -10
% COLOR_BTNFACE=15
% DT_CENTER=1
% DT_VCENTER=4
% DT_SINGLELINE=32
% SWP_NOZORDER=4
% SIZE_RESTORED=0
% SIZE_MAXIMIZED=2

function RGB(int r, g, b) as int
    return (r + g*256 + b*65536)
end Function



'Window class
string CUSTOM_WC= "CustomControl"

'Register/Unregister the window class
declare sub CustomRegister()
declare sub CustomUnregister()


sys hInstance=inst
sys hwndCustom

% CUSTOM_ID = 100
% MARGIN    =   7


MainWindow 350,250,WS_OVERLAPPEDWINDOW


function WndProc(sys hwnd, Msg, wParam, lParam) as sys callback

    select Msg
       
        case WM_CREATE
            SetWindowText(hwnd, "App Name")
            'Replaces the window-class style bits: color background   
            SetClassLongPtr(hwnd, GCL_HBRBACKGROUND, COLOR_BTNFACE + 1)           

            CustomRegister()
            hwndCustom = CreateWindowEx(0, CUSTOM_WC, null, WS_CHILD | WS_VISIBLE,
                                        0,0,0,0, hwnd, CUSTOM_ID, hInstance, null)

        case WM_CLOSE
            DestroyWindow(hwnd)

        case WM_SIZE
            if wParam = SIZE_MAXIMIZED or wParam = SIZE_RESTORED then
                word cx = LOWORD(lParam)
                word cy = HIWORD(lParam)
                SetWindowPos(hwndCustom, null, MARGIN, MARGIN,
                             cx-2*MARGIN, cy-2*MARGIN, SWP_NOZORDER)
            end if
           
        case WM_DESTROY
            CustomUnregister()
            PostQuitMessage(0)
                   
        case else
            return DefWindowProc(hwnd, Msg, wParam, lParam)

    end select
   
    return 0
end function

=======================================


sub CustomPaint(sys hwnd)
    PAINTSTRUCT ps
    sys hdc
    RECT rect

    GetClientRect(hwnd, &rect)

    hdc = BeginPaint(hwnd, &ps)
    SetTextColor(hdc, RGB(0,0,0))
    SetBkMode(hdc, TRANSPARENT)
    DrawText(hdc, "Hello World!", -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER)
    EndPaint(hwnd, &ps)
end sub


function CustomProc(sys hwnd, uint uMsg, sys wParam, lParam) as sys callback
    select uMsg
        case WM_PAINT
            CustomPaint(hwnd)
            return 0
    end select
    return DefWindowProc(hwnd, uMsg, wParam, lParam)
end function


sub CustomRegister()
    WNDCLASS wc

    wc.style = CS_GLOBALCLASS | CS_HREDRAW | CS_VREDRAW
    wc.lpfnWndProc = @CustomProc
    wc.hCursor = LoadCursor(null, IDC_ARROW)
    wc.lpszClassName = strptr CUSTOM_WC
    if not RegisterClass(&wc) then
      mbox "Cannot register Custom Control"
    end if
end sub

sub CustomUnregister()
    UnregisterClass(CUSTOM_WC, null)
end sub