'01:33 13/02/2025
'CP
'
'#include <windows.h>
% filename "t.exe"
'uses RTL64 'or uses RTL32
uses corewinW

'constexpr auto CLIENT_WIDTH   = 640;
'constexpr auto CLIENT_HEIGHT  = 480;
% CLIENT_WIDTH   = 640;
% CLIENT_HEIGHT  = 480;
typedef long lresult

struct PROP {
    sys        hMain;
    long        minTrackSizeW;
    long        minTrackSizeH;
};

static PROP gP;

long rWidth(RECT *r) {
    return r.right - r.left;
}

long rHeight(RECT *r) {
    return r.bottom - r.top;
}

int WndProc(sys hWnd, dword uMsg, sys wParam, sys lParam) callback  {

    typedef struct tagMINMAXINFO {
      POINT ptReserved;
      POINT ptMaxSize;
      POINT ptMaxPosition;
      POINT ptMinTrackSize;
      POINT ptMaxTrackSize;
    } MINMAXINFO, *PMINMAXINFO, *LPMINMAXINFO;

    int nRet = -1;
    MINMAXINFO* pMM;
    switch uMsg {

    case WM_CREATE:
        break;

    case 0X0024 'WM_GETMINMAXINFO:
        @pMM = lParam;
        pMM->ptMinTrackSize.x = gP.minTrackSizeW;
        pMM->ptMinTrackSize.y = gP.minTrackSizeH;
       break;

    case WM_SIZE:
        InvalidateRect(hWnd, NULL, TRUE);
        nRet = 0;
        break;

    case WM_PAINT:
            PAINTSTRUCT ps;
            sys hDC = BeginPaint(hWnd, ps);
            // Paint the window content here
            EndPaint(hWnd, ps);
            nRet = 0;
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        nRet = 0;
        break;
    }

    if (nRet == -1)
      nRet = DefWindowProc(hWnd, uMsg, wParam, lParam);
    endif
    return nRet;
}

int wWinMain(sys hInstance, sys hPrevInstance, sys lpCmdLine, int nCmdShow) {
    'UNREFERENCED_PARAMETER(hPrevInstance);
    'UNREFERENCED_PARAMETER(lpCmdLine);

    int nRet = 0;
    WCHAR szClass[] = L"MINI";
    // Detect process without using a Mutex
    sys hFound = FindWindow(szClass, NULL);
    if hFound {
        if IsIconic(hFound) { 
          ShowWindow(hFound, SW_RESTORE);
        }
        SetForegroundWindow(hFound);
        return nRet;
     }

    WNDCLASSEX wcx ' = { 0 };
    wcx.cbSize        = sizeof(wcx);
    long IsInitialized = GetClassInfoEx(hInstance, szClass, wcx);
    if not IsInitialized {
        wcx.style         = CS_HREDRAW | 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_BTNSHADOW;
        wcx.lpszMenuName  = 0;
        wcx.lpszClassName = strptr(szClass);
        wcx.hIconSm       = wcx.hIcon;
       if RegisterClassEx(wcx) then IsInitialized = -1;
    }

    if (IsInitialized) {
        DWORD dwExStyle = 0;
        DWORD dwStyle = WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
        RECT lpr '= { 0 };
        SetRect(lpr, 0, 0, CLIENT_WIDTH, CLIENT_HEIGHT);
        AdjustWindowRectEx(lpr, dwStyle, FALSE, dwExStyle);
        gP.minTrackSizeW = rWidth(lpr);
        gP.minTrackSizeH = rHeight(lpr);

        'long x = max((GetSystemMetrics(SM_CXSCREEN) - gP.minTrackSizeW) / 2, 0);
        'long y = max((GetSystemMetrics(SM_CYSCREEN) - gP.minTrackSizeH) / 2, 0);

        long x=(GetSystemMetrics(SM_CXSCREEN)-gP.minTrackSizeW)/2;
        long y=(GetSystemMetrics(SM_CYSCREEN)-gP.minTrackSizeH)/2;
        if (x<0) {x=0};
        if (y<0) {y=0};

        gP.hMain = CreateWindowEx(dwExStyle, szClass, szClass, dwStyle,
                   x, y, gP.minTrackSizeW, gP.minTrackSizeH, 0, 0, hInstance, NULL);
        if gP.hMain {
            ShowWindow(gP.hMain, nCmdShow);
            SetForegroundWindow(gP.hMain); // Slightly Higher Priority
            MSG mg '= { 0 };
            while GetMessage(mg, NULL, 0, 0) {
                TranslateMessage(mg);
                DispatchMessage(mg);
            }
            nRet = mg.wParam;
        }
       UnregisterClass(wcx.lpszClassName, hInstance);
    }
    return nRet;
}
wWinmain( GetModuleHandle(0),0,GetCommandLine(),SW_SHOWDEFAULT);

