-- start demo --
include win32lib.ew
atom ptr,  ptr2,  ok,  proghwnd

constant    
    -- new functions !
    zFindWindow = registerw32Function( user32,  "FindWindowA",  {C_POINTER, C_INT}, C_INT ),  
    -- ok,  the above func is now in win32lib !
    zBeep = registerw32Function( user32,  "MessageBeep",  {C_INT}, C_INT )

    ptr = allocate_string( "Win32Lib AppWindow 1" ) -- classname
    ptr2 = allocate_string( "kick_self" ) -- title
    -- first,  try and find a window using:
    -- classname and title,  as in above strings. 
    proghwnd = w32Func(  zFindWindow,  { ptr,  ptr2 }  )

constant
    Win = create(  Window,  "kick_self",  0,  Default,  Default,  300,  100,  0  )

----

procedure check_self(  integer self,  integer event,  sequence params  )
    
    -- get the default Euphoria icon from exw.exe, 
    ok = w32Func(  xLoadIcon,  { instance(  ),  allocate_string( "exw" ) }  )
    -- and insert it in this program's title bar.
    ok = sendMessage(  Win,  128,  0,  ok  )
    if proghwnd != 0 then 
        ok = w32Func(  zBeep,  { 0 }  )
        -- just to test,  lets see/report this instance for 3 seconds...
        setVisible( Win, 1 )
        setFont( Win, "System", 10, 1 )
        setPenPos( Win, 1, 1 )
        wPuts( Win, "...another instance found,   so good-bye." )
        sleep( 3 )
        -- set focus to first instance
        ok = w32Func(  xSetForegroundWindow, {proghwnd} )
        --  closeWindow( Win )  -- no longer works here !!!???
        abort( 0 )  -- I'd rather closeWindow(  ) than abort, 
                    -- but only this works now.
    end if
    
end procedure
setHandler(  Win,  w32HOpen,  routine_id( "check_self" )  )

----

WinMain(  Win,  Normal  )
-- end demo --

