Create Windows Gui Using Forth

I will show you how to create a Windows GUI using the forth programming language (SwiftForth). We will use the win32 api and some functions to draw text to the screen.

We will also track our mouse x and y coordinates in realtime to troubleshoot a real-world problem

Full source code below:

0 VALUE hAPP

CREATE CLASSNAME ,Z" Counting"

: (BUILD-STRING) ( – addr n )
mousepos drop (.) HERE PLACE
S" :x y: " HERE APPEND
mousepos swap drop (.) HERE APPEND
HERE COUNT ;

: SHOW-STRING ( hdc – )
(BUILD-STRING)
HWND PAD GetClientRect DROP PAD
DT_SINGLELINE DT_CENTER OR DT_VCENTER OR
DrawText DROP ;

: REFRESH ( – )
HWND 0 TRUE InvalidateRect DROP ;

: REPAINT ( – res )
HWND PAD BeginPaint ( hdc)
HWND HERE GetClientRect DROP
( hdc) SHOW-STRING
HWND PAD EndPaint DROP 0 ;

[SWITCH MESSAGE-HANDLER DEFWINPROC ( – res )
WM_TIMER RUN: REFRESH 0 ;
WM_LBUTTONDOWN RUN: HWND 1002 10 0 SetTimer 0 ;
WM_RBUTTONDOWN RUN: HWND 1002 KillTimer 0 ;
WM_KEYDOWN RUN: HWND 1002 KillTimer 0 ;
SWITCH]

[+SWITCH MESSAGE-HANDLER ( – res )
WM_PAINT RUNS REPAINT
SWITCH]

:NONAME ( – res )
MSG LOWORD MESSAGE-HANDLER ; 4 CALLBACK: APPLICATION-CALLBACK

: REGISTER-CLASS ( – )
CLASSNAME APPLICATION-CALLBACK DefaultClass DROP ;

: CREATE-WINDOW ( – handle )
0
CLASSNAME
Z" Mouse position helper"
WS_OVERLAPPEDWINDOW
10 10 320 200
0
0
HINST
0
CreateWindowEx ;

: START ( – flag )
REGISTER-CLASS CREATE-WINDOW DUP IF
DUP SW_NORMAL ShowWindow DROP
DUP UpdateWindow DROP
DUP TO hAPP
THEN ;