• Advertisement

Archived

This topic is now archived and is closed to further replies.

how to activate windows in another instance!

This topic is 6074 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m writing a game for win32 and I want to make it so that only one instance of the program can be running at a time, like minesweeper. Now, i managed to make it so that only one instance runs, and if you try to open the program again it simply closes right away, but i''d like it to also activate the first instance. I tried using FindWindow() to find the first window and then ShowWindow() to make it the active window but for some reason that didn''t work. The platform sdk docs show how to use mutexes to stop more than one instance, but they don''t say anything about activating windows in other instances. Thats really what i''m looking for i guess. Any help will be greatly appreciated

Share this post


Link to post
Share on other sites
Advertisement

#pragma data_seg (".shared")
HWND g_hProgramWnd = NULL;
#pragma data_seg ()

// Mark section as shared
#pragma comment (linker, "/section:.shared,srw")

...

int WINAPI WinMain (...) {
if (g_hProgramWnd) {
// Warning: may not work on Win98, Win2000, or later!
SetForegroundWindow (g_hProgramWnd);
} else {
...
// Main program window to be shared.
g_hProgramWnd = CreateWindow (...);
}
}

Share this post


Link to post
Share on other sites
Worked perfectly! Thank you very much

Oh, and SetForegroundWindow does work in all win9x/ME, as well as NT/2000, but in NT 5/2000 it sometimes will just flash the window instead of activating it (flash on the taskbar) if the user is working in another window.

Share this post


Link to post
Share on other sites
That method works fine on existing OSes, but with WindowsXP and the Fast User Switching stuff (i.e. multiple desktops running at the same time) you''ll have to do a bit more work to make sure you activate the correct instance...

  
if (IsAppAlreadyRunning(..)==true)
{
// app is already running.

// check if it is being used by current user/session

HWND hWnd = FindWindow( szWindowClass, szWindowTitle );
if (hWnd)
{
// app is running on this desktop - so restore it

if (IsIconic(hWnd))
{
// restore window if it''s minimised

ShowWindow(hWnd, SW_RESTORE);
}
// make app window the active one

SetForegroundWindow(hWnd);
}
else
{
// app is running, but not on this desktop

MessageBox(NULL, "Another user is using application", szWindowTitle, MB_OK);
}
}


szWindowTitle is the title of your application window
szWindowClass is the window class of your application window


--
Simon O''''Connor
Creative Asylum Ltd
www.creative-asylum.com

Share this post


Link to post
Share on other sites

  • Advertisement