-- dynamically displays the memory status of the system. Wolf/Jan/99

include win32lib.ew
without warning

constant
  MemWin  = create(Window,"Memory Info",0,0,0,235,95,
                     {WS_OVERLAPPEDWINDOW - WS_MAXIMIZEBOX}),
  MyTimer = 2,
  zGlobalMemoryStatus = registerw32Procedure(kernel32,"GlobalMemoryStatus",{C_POINTER})

sequence meminfo, string1
atom buffer
  buffer = allocate(32)
  poke4(buffer,32)

procedure GetMemory()
  w32Proc(zGlobalMemoryStatus,{buffer})
  meminfo = peek4u({buffer,8})
end procedure

--procedure onPaint_MemWin( integer x1, integer y1, integer x2, integer y2)
procedure onPaint_MemWin( integer self, integer event, sequence params )
	GetMemory()
	setPenPos(MemWin,2,2)
	string1= "bytes of memory = " & sprintf("%d",meminfo[3])
	wPuts(MemWin,string1)
	setPenPos(MemWin,2,20)
	string1= "percent of memory in use  = " & sprintf("%d",meminfo[2]) & "%"
	wPuts(MemWin,string1)
	setText(MemWin, sprintf("%d",meminfo[2]) & "% used")
	setPenPos(MemWin,2,38)
	string1= "free memory bytes = " & sprintf("%d",meminfo[4])
	wPuts(MemWin,string1)
end procedure

--procedure onTimer_MemWin( integer timerId )
procedure onTimer_MemWin( integer self, integer event, sequence params )
  repaintWindow( MemWin )
end procedure  

procedure onOpen_MemWin( integer self, integer event, sequence params )
  setTimer( MemWin, MyTimer, 2000 )
end procedure

procedure onClose_MemWin( integer self, integer event, sequence params )
  free(buffer)
end procedure

setHandler( MemWin, w32HOpen,  routine_id( "onOpen_MemWin" ) )
setHandler( MemWin, w32HTimer, routine_id( "onTimer_MemWin" ) )
setHandler( MemWin, w32HPaint, routine_id( "onPaint_MemWin" ) )
setHandler( MemWin, w32HDestroy, routine_id( "onClose_MemWin" ) )

WinMain(MemWin,Normal)
----
