// Standard Button and Color (Owner Drawn) Button

$ filename "Std_and_ClrBtn.exe"
'uses rtl32
'uses rtl64

$ EscapeKeyEnd
uses WinUtil

% EDGE_RAISED=5
% EDGE_SUNKEN=10
% BF_RECT=15
% WM_DRAWITEM=43
% ETO_OPAQUE=2
% ETO_CLIPPED=4
% ODS_SELECTED=1
% GWL_HINSTANCE -6

type SIZE
  long cx
  long cy
end type

type DRAWITEMSTRUCT 
    UINT CtlType
    UINT CtlID
    UINT itemID
    UINT itemAction
    UINT itemState
    sys  hwndItem
    sys  hDC
    RECT rcItem
    sys  itemData        'ulong_ptr
end type


% IDB_BUTTON  100
sys hWndStdButton

% IDC_OWNERDRAWN  1000
sys hWndClrButton

MainWindow 240, 120 , WS_OVERLAPPEDWINDOW

function WndProc(sys hwnd, uint MainMsg, sys wParam, lParam) as sys callback
    select case MainMsg

        case WM_CREATE
            SetWindowText(hwnd, "Buttons")

            sys hInstance = GetWindowLongPtr(hWnd, GWL_HINSTANCE)

            hWndStdButton = CreateWindowEx(
            0,
            "BUTTON",
            "Standard Button",
            WS_VISIBLE or WS_CHILD,
            10,  10,
            120, 24,
            hWnd,
            IDB_BUTTON,
            hInstance,
            NULL)

            hWndClrButton = CreateWindowEx(
            0,
            "BUTTON",
            NULL,
            WS_CHILD or BS_OWNERDRAW,
            10,  40,
            120, 24,
            hWnd,
            IDC_OWNERDRAWN,
            GetWindowLongPtr(hWnd, GWL_HINSTANCE),
            NULL)

            if not hWndClrButton then
                MessageBox(NULL, "Button Creation Failed.", "Error", MB_OK or MB_ICONERROR)
                return 0
            end if

            ShowWindow(hWndClrButton, SW_SHOW)

        case WM_DRAWITEM
            select case wParam
                case IDC_OWNERDRAWN

                    'get the pointer to the item-drawing info
                    DRAWITEMSTRUCT *ptDrawItem
                    &ptDrawItem=lParam
                   
                    ' alternative
                    'DRAWITEMSTRUCT ptDrawItem at lParam                   

                    SIZE siz
                    string text
                    text = "Color Button"
                    GetTextExtentPoint32(ptDrawItem.hDC, text, len(text), &siz)
                    SetTextColor(ptDrawItem.hDC, WHITE)
                    SetBkColor(ptDrawItem.hDC, RED)

                    ExtTextOut(ptDrawItem.hDC,
                    ((ptDrawItem.rcItem.right - ptDrawItem.rcItem.left) - siz.cx) \ 2,
                    ((ptDrawItem.rcItem.bottom - ptDrawItem.rcItem.top) - siz.cy) \ 2,
                    ETO_OPAQUE or ETO_CLIPPED, &ptDrawItem.rcItem, text, len(text), NULL)

                    uint edge
                    if ptDrawItem.itemState and ODS_SELECTED then
                      edge = EDGE_SUNKEN
                    else
                      edge = EDGE_RAISED
                    end if   
                    DrawEdge(ptDrawItem.hDC, &ptDrawItem.rcItem,
                    edge, BF_RECT)               
            end select


        case WM_COMMAND
            select case loword(wParam)
                case IDB_BUTTON
                    select case hiword(wParam)
                        case BN_CLICKED
                            MessageBox(NULL, "Selected Standard Button", "Standard Button", MB_OK or MB_ICONINFORMATION)
                    end select
                case IDC_OWNERDRAWN
                    select case hiword(wParam)
                        case BN_CLICKED
                            MessageBox(NULL, "Selected Color Button", "Color Button", MB_OK or MB_ICONINFORMATION)
                    end select
            end select

        case WM_CLOSE
            DestroyWindow(hWnd)

        case WM_DESTROY
            PostQuitMessage(0)

        case else
            return DefWindowProc(hWnd, MainMsg, wParam, lParam)
    end select

    return 0
end function