-- demo of hiding a window, and getting it back,
-- or changing colors, by pressing a hotkey combination
-- originally by Eddy Van Esch, 15/01/2001

include win32lib.ew
without warning

constant
zWM_HOTKEY = #312,
zMOD_CTRL = #2,
key1 = 'S',   -- hotkeys are CTRL S,Z,X... change as necessary
key2 = 'Z',
key3 = 'X',

zRegisterHotKey = registerw32Function( user32,
         "RegisterHotKey",{ C_LONG, C_LONG, C_LONG, C_LONG }, C_LONG ),
zUnregisterHotKey = registerw32Function( user32,
         "UnregisterHotKey",{ C_LONG, C_LONG }, C_LONG )

atom ok, myHandle

sequence msg
msg="Click  'Hide window'  button to hide this window.\n" &
    "Press hotkey combination CTRL S to show the window again.\n\n" &
    "CTRL Z, and CTRL X change the window's colors."

constant
Win1 = create( Window, "HotKey Demo", 0, Default, Default,400, 200, 0 ),
HideBtn = create( PushButton, "Hide window", Win1,120, 12, 144, 40, 0 ),
TheTxt = create( LText, msg, Win1, 40, 70, 300, 70, 0 )

-- Hide button's behavior
procedure onClick_HideBtn( integer self, integer event, sequence params )
  setVisible( Win1, False )  -- Hide the window
end procedure

-- Event handler: intercept the hotkey and define what to do with it:
--procedure Events(atom iMsg, atom wparam, atom lparam)
-- PROBLEMS??
procedure Events( integer self, integer event, sequence params )
  integer iMsg  iMsg = params[1] -- likely
  atom lparam = params[3]
  atom wparam  wparam = params[2]
--?  lparam
--printf(1, "%x\n", {iMsg} )
  if iMsg = zWM_HOTKEY then -- zWM_HOTKEY = #312; iMsg seems never this
    if hi_word(lparam) = 'S' then
      --unhide window, can be used to do anything you
      --want here...
      setVisible( Win1, True )
    elsif hi_word(lparam) = 'Z' then
      setWindowBackColor(Win1,Yellow)
      setWindowBackColor(TheTxt,Yellow)
--    elsif hi_word(lparam) = 'X' then
    elsif wparam = 3 then
      setWindowBackColor(Win1,getSysColor(COLOR_BTNFACE))
      setWindowBackColor(TheTxt,getSysColor(COLOR_BTNFACE))
    end if
  end if
end procedure

procedure register_keys( integer self, integer event, sequence params )
  -- get our window's handle
  myHandle = getHandle(Win1)
--  ? 99 ok
  -- register hotkeys
  ok = w32Func(zRegisterHotKey, {myHandle, 1, zMOD_CTRL, key1})
  ok = w32Func(zRegisterHotKey, {myHandle, 2, zMOD_CTRL, key2})
  ok = w32Func(zRegisterHotKey, {myHandle, 3, zMOD_CTRL, key3})
end procedure

--
setHandler( HideBtn, w32HClick, routine_id( "onClick_HideBtn" ) )
setHandler( Win1, w32HEvent, routine_id( "Events" ) )
setHandler( Win1, w32HOpen, routine_id("register_keys") )

WinMain( Win1, Normal )  --normal event loop
