-- Under Windows 95 and NT4, if an inactive application calls SetActiveWindow() or -- SetForegroundWindow() using the HWND of one of its windows, this makes the application -- active. The same applies if it calls SetWindowPos() with HWND_TOP and SWP_SHOWWINDOW -- (no need for HWND_TOPMOST windows). These 3 APIs are basically equivalent. -- Under ME and XP, this does not happen. Instead the inactive application has its -- button highlighted on the windows taskbar but is not made active. Also if the -- inactive application calls SetForegroundWindow(), the same thing happens. -- If SetForegroundWindow() is called from an active application (an app with the active window), -- and is passed the HWND of a window in an inactive application, it *can* properly activate -- the other application and the other window. But if an inactive application calls -- SetForegroundWindow(), it can't make itself active. This applies under all 32 bit -- versions of Windows. -- Therefore the only reliable system of inter-application activations that works under -- all versions of Windows is for the currently active application to call SetForegroundWindow() -- using the HWND of a window in the other application. -- The example program below uses the excellent IPC library by Thomas Parslow (PatRat) to -- get a previous instance of the program to reopen a popup menu (its main window is hidden). -- When you first run the program a popup menu will appear, if you run it again the new -- instance finds an already running version, asks it for its windows handle, uses that to -- bring the existing instance into the foreground (as it is a hidden window you will not see this), -- then requests the existing instance to reopen its popup menu, and finally closes itself down leaving -- the original instance running.. -- -- Pete Stoner 13 May 2005 without warning include win32lib.ew include ipc.ew without trace atom ExistingHwnd if not ipc_RegisterProcessName("Test Win") then ExistingHwnd = ipc_CallFunc("Test Win","GetHwnd",{}) if ExistingHwnd != -1 then if w32Func(xSetForegroundWindow,{ExistingHwnd}) then -- existing instance set to foreground ipc_CallProc("Test Win", "ReOpenPopup", {}) -- ask existing instance to reopen popup end if end if abort(1) end if constant Main = createEx( Window, "Test Win", 0, 0, 0, 0, 0, WS_POPUP, WS_EX_TOOLWINDOW), MyPopup = create( Menu, "", Main, 0, 0, 0, 0, 0 ), item1 = create(MenuItem, "test", MyPopup, 0, 0, 0, 0, 0) procedure Main_w32HActivate( integer self, integer event, sequence params ) sequence pos captureMouse( Main ) pos = getPointerPos() popup( MyPopup, pos[1], pos[2] ) end procedure setHandler( Main, w32HActivate, routine_id("Main_w32HActivate") ) function GetHwnd() return getHandle(Main) end function procedure ReOpenPopup() sequence pos setFocus(Main) captureMouse( Main ) pos = getPointerPos() popup( MyPopup, pos[1], pos[2] ) end procedure ipc_RegisterFunc("GetHwnd",routine_id("GetHwnd")) ipc_RegisterProc("ReOpenPopup",routine_id("ReOpenPopup")) WinMain( Main, Normal)