Thread: Bringing focus to an window if app already running.

  1. #1

    Bringing focus to an window if app already running.

    I'm using a Mutex to check for instances of an app already running and use a message box to inform the user if it's already running but I would like to just have it restore the window to focus (say if its minimized or behind other active windows).

    I tried using ShowWindow with SW_RESTORE but that doesn't wanna work, anyone have any ideas?

  2. #2
    Join Date
    Jan 2002
    This has been discussed here:

    (How do you allow only one instance of an app?)

    If you take 'Unregistered' sample code and insert 'No-One's'
    sample code into it once GetLastError has returned ERROR_ALREADY_EXISTS then use ShowWindow on the returned handle from FindWindow that should do it.

    Which is probably too surreal so (credit to Unregistered & No-One) :

    HANDLE hMutex=CreateMutex(NULL, FALSE, "myAppMutex"); 
    if (hMutex) 
        if (GetLastError() == ERROR_ALREADY_EXISTS) 
           HWND hwnd=FindWindow(lpClassName, lpWindow);
           if (hwnd)
               UpdateWindow(hwnd);   //for good measure
    where lpClassName is pointer to class name and lpWindow is pointer to window name.

    I think that should about do it....but don't quote me on that!

  3. #3
    Registered User
    Join Date
    Jan 2002
    This works, just add it to the beginning of WinMain.

        /* Vars */
       HANDLE hMutex; // Mutex Handle
       HWND hwndShow; // Find Window HWND
        /* Create A Mutex To Register Program Is Running */
        hMutex = CreateMutex(
        /* If Program Is Already Running, Show Program & Stop */
        if (hMutex) {
            if (GetLastError() == ERROR_ALREADY_EXISTS) {
                // Replace WINCLASS_HERE with name of your class
                hwndShow = FindWindow(WINCLASS_HERE, NULL);
                if (hwndShow) {
                    // Minimize The Program So It Can Be Restored
                    ShowWindow(hwndShow, SW_MINIMIZE);
                    // Restore The Program In Its Original State
                    ShowWindow(hwndShow, SW_RESTORE);
    Make sure to change the code to reflect your Window class. You don't have to include the SW_MINIMIZE ShowWindow, its just there to make sure the window shows no matter how many other maximized windows you have.
    Last edited by Sindol; 01-24-2002 at 02:54 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 08:23 AM
  2. Adding buttons, edit boxes, etc to the window
    By rainmanddw in forum Windows Programming
    Replies: 1
    Last Post: 04-10-2006, 03:07 PM
  3. my wndProc is out of scope
    By Raison in forum Windows Programming
    Replies: 35
    Last Post: 06-25-2004, 07:23 AM
  4. g_hWndMain = hWnd;error C2065,C2440
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 12-09-2001, 03:36 PM
  5. Invoking MSWord
    By Donn in forum C Programming
    Replies: 21
    Last Post: 09-08-2001, 04:08 PM