'02:52 13/02/2025
'CP
'
'#Include "windows.bi"
% filename "t.exe"
'uses RTL64 'or uses RTL32
uses corewinW


Const CLIENT_WIDTH as long  = 640
Const CLIENT_HEIGHT as long = 480

Type PROP
    hMain As sys 'HWND
    minTrackSizeW As Long
    minTrackSizeH As Long
End Type

alias int as LRESULT

Dim gP As PROP

Function rWidth(ByRef r As RECT) As Long
    Return r.right - r.left
End Function

Function rHeight(ByRef r As RECT) As Long
    Return r.bottom - r.top
End Function

Function WndProc(ByVal hWnd As sys, ByVal uMsg As UINT, ByVal wParam As sys, ByVal lParam As sys) callback As LRESULT
    Dim nRet As LRESULT = -1
    Dim pMM As MINMAXINFO Ptr
   
    Select Case uMsg
    Case WM_CREATE
        ' Initialization can go here if needed.

    Case 0x0024 'WM_GETMINMAXINFO
        @pMM = lParam
        pMM->ptMinTrackSize.x = gP.minTrackSizeW
        pMM->ptMinTrackSize.y = gP.minTrackSizeH

    Case WM_SIZE
        InvalidateRect(hWnd, NULL, TRUE)
        nRet = 0

    Case WM_PAINT
        Dim ps As PAINTSTRUCT
        Dim hDC As sys = BeginPaint(hWnd, @ps)
        ' Paint window content here
        EndPaint(hWnd, @ps)
        nRet = 0

    Case WM_DESTROY
        PostQuitMessage(0)
        nRet = 0
    End Select

    If nRet = -1 Then nRet = DefWindowProc(hWnd, uMsg, wParam, lParam)
    Return nRet
End Function

Function WinMain(ByVal hInstance As sys, ByVal hPrevInstance As sys, ByVal lpCmdLine As sys, ByVal nCmdShow As Integer) As Integer
    Dim nRet As Integer = 0
    Dim szClass[10] As wchar ="MINI" 'zString * 10 = "MINI"

    ' Detect process without using a Mutex
    Dim hFound As sys = FindWindow(szClass, NULL)
    If hFound <> NULL Then
        If IsIconic(hFound) Then ShowWindow(hFound, SW_RESTORE)
        SetForegroundWindow(hFound)
        Return nRet
    End If

    Dim wcx As WNDCLASSEX
    wcx.cbSize = sizeof(wcx) 'not len
    Dim IsInitialized As Long = GetClassInfoEx(hInstance, szClass, @wcx)
    If IsInitialized = 0 Then
        wcx.style         = CS_HREDRAW Or CS_VREDRAW
        wcx.lpfnWndProc   = @WndProc
        wcx.cbClsExtra    = 0
        wcx.cbWndExtra    = 0
        wcx.hInstance     = hInstance
        wcx.hIcon         = LoadIcon(NULL, IDI_APPLICATION)
        wcx.hCursor       = LoadCursor(NULL, IDC_ARROW)
        wcx.hbrBackground = 16 'COLOR_BTNFACE + 1) ' Fix: Windows expects a valid brush
        wcx.lpszMenuName  = NULL
        wcx.lpszClassName = strptr(szClass)
        wcx.hIconSm       = wcx.hIcon
       
        If RegisterClassEx(@wcx) <> 0 Then IsInitialized = -1
    End If
    If IsInitialized Then
        Dim dwExStyle As DWORD = 0
        Dim dwStyle As DWORD = WS_OVERLAPPEDWINDOW Or WS_CLIPSIBLINGS Or WS_CLIPCHILDREN
        Dim lpr As RECT
        SetRect(@lpr, 0, 0, CLIENT_WIDTH, CLIENT_HEIGHT)
        AdjustWindowRectEx(@lpr, dwStyle, FALSE, dwExStyle)

        gP.minTrackSizeW = rWidth(lpr)
        gP.minTrackSizeH = rHeight(lpr)

        'Dim x As Long = Max((GetSystemMetrics(SM_CXSCREEN) - gP.minTrackSizeW) \ 2, 0)
        'Dim y As Long = Max((GetSystemMetrics(SM_CYSCREEN) - gP.minTrackSizeH) \ 2, 0)

        dim long x=(GetSystemMetrics(SM_CXSCREEN)-gP.minTrackSizeW)/2
        dim long y=(GetSystemMetrics(SM_CYSCREEN)-gP.minTrackSizeH)/2
        if x<0 then x=0
        if y<0 then y=0


        gP.hMain = CreateWindowEx(dwExStyle, szClass, strptr(szClass), dwStyle, x, y, gP.minTrackSizeW, gP.minTrackSizeH, NULL, NULL, hInstance, NULL)

        If gP.hMain <> NULL Then
            ShowWindow(gP.hMain, nCmdShow)
            SetForegroundWindow(gP.hMain) ' Slightly Higher Priority

            Dim mg As MSG
            While GetMessage(@mg, NULL, 0, 0)
                TranslateMessage(@mg)
                DispatchMessage(@mg)
            Wend
            nRet = mg.wParam
        End If

        UnregisterClass(szClass, hInstance)
    End If

    Return nRet
End Function

WinMain(GetModuleHandle(NULL), NULL, NULL, SW_SHOWDEFAULT)