-- originally by Brian K. Broker.
-- this modified program will re-size the
-- main window when the taskbar is moved.
-- Wolf Oct 2001

include win32lib.ew
without warning

constant
zSystemParameters = registerw32Function(user32, "SystemParametersInfoA",
                 {C_UINT, C_UINT, C_POINTER, C_UINT}, C_LONG),
zSPI_SETWORKAREA = 47,
zSPI_GETWORKAREA = 48,
zWM_SETTINGCHANGE = 26,

-- more new DLL calls for Win98 functionality.
zAttachThreadInput = registerw32Function(user32,
    "AttachThreadInput",{C_INT, C_INT, C_INT}, C_INT ),
zGetWindowThreadProcessId = registerw32Function(user32,
    "GetWindowThreadProcessId",{C_INT, C_POINTER}, C_INT),
zGetForegroundWindow = registerw32Function(user32,
    "GetForegroundWindow", {}, C_INT)

--

function getWorkarea()
-- Returns: sequence containing the coordinates of the work area,
-- expressed in virtual screen coordinates {x1,y1, x2,y2}
atom workarea
sequence wa_out

-- get pointer for RECT structure to receive coordinates
workarea = acquire_mem( 0,SIZEOF_RECT )

-- call C function from user32.dll
if not w32Func( zSystemParameters, {zSPI_GETWORKAREA,
               0, workarea, 0} ) then
  warnErr( "getWorkarea:SystemParametersInfo failed" )
end if

-- set return sequence
wa_out = { fetch( workarea, rectLeft ),  fetch( workarea, rectTop ),
           fetch( workarea, rectRight ), fetch( workarea, rectBottom ) }

-- free pointer
release_mem( workarea )

return wa_out
end function

-- now, for our actual 'window' creation

sequence area
atom ok, x, y, cx, cy


-- get the size and location allowing for the taskbar
area=getWorkarea()
x = area[1]
y = area[2]
cx = area[3]-area[1]
cy = area[4] - area[2]

constant
Win=create(Window," ", 0, x, y, cx, cy,0),
TheText=create(LText,"...move or re-size the taskbar.",
               Win, 10, 10, 200, 25, 0)

setText(Win,"total 'working' area = " & sprintf("%d",cx)
          & " by " & sprintf("%d",cy) & " pixels")

-- re-size/re-locate the window if the taskbar has been moved !
procedure resize()
atom this_win, id_1, id_2, id_3, pid_ptr

this_win = getHandle(Win) -- get our main window's handle.

area=getWorkarea()
x = area[1]
y = area[2]
cx = area[3]-area[1]
cy = area[4]-area[2]
setText(Win,"total 'working' area = " & sprintf("%d",cx )
        & " by " & sprintf("%d",cy ) & " pixels")
ok = w32Func( xMoveWindow, {this_win, x, y, cx, cy, 1} )

-- out new code to accomodate Win98.
pid_ptr = acquire_mem(0,1)

id_1 = w32Func(zGetForegroundWindow,{})
id_2 = w32Func(zGetWindowThreadProcessId,{id_1, pid_ptr })
id_3 = w32Func(zGetWindowThreadProcessId,{this_win, pid_ptr })

if id_2 != id_3 then
  ok = w32Func( zAttachThreadInput,{id_2, id_3, True })
  ok = w32Func( xSetForegroundWindow,{this_win})
  ok = w32Func( zAttachThreadInput,{id_2, id_3, False })
else
  ok = w32Func( xSetForegroundWindow,{this_win})
end if

release_mem(pid_ptr)
end procedure

procedure check( integer self, integer event, sequence params )
  integer e_event   e_event = params[1]
  integer wParam
  
  if e_event= zWM_SETTINGCHANGE  and
  wParam = zSPI_SETWORKAREA then
     resize() 
  end if
end procedure
setHandler( Win, w32HEvent, routine_id( "check" ) )


WinMain(Win,Normal)
