Bring An Application To The Foreground Through Another Application
-
I have a need of a way to bring one application to the foreground from another application. Application 'A' is either laucnhed by itself or it can be launched from application 'B' upon a button press. There's no guarantee which app was started first, or if the application to be brought to the foreground (app 'A') is even running at that point in time. If 'A' is not running I am launching it upon a button press in app 'B' - but if 'A' is already running I want to bring it to the foreground to alert the user it's already running (and not spawn a second instance of app 'A'). I'm currently using a mutex to check to see if app 'A' is already running, but I am not sure how to bring it to the foreground when it is. Any thoughts or ideas on how to proceed? : Dean 'Karnatos' Michaud
-
I have a need of a way to bring one application to the foreground from another application. Application 'A' is either laucnhed by itself or it can be launched from application 'B' upon a button press. There's no guarantee which app was started first, or if the application to be brought to the foreground (app 'A') is even running at that point in time. If 'A' is not running I am launching it upon a button press in app 'B' - but if 'A' is already running I want to bring it to the foreground to alert the user it's already running (and not spawn a second instance of app 'A'). I'm currently using a mutex to check to see if app 'A' is already running, but I am not sure how to bring it to the foreground when it is. Any thoughts or ideas on how to proceed? : Dean 'Karnatos' Michaud
Try to use SetForegroundWindow() then use ShowWindow() with SW_SHOWNORMAL flag. Michael Liu
-
Try to use SetForegroundWindow() then use ShowWindow() with SW_SHOWNORMAL flag. Michael Liu
To set the foreground, I need to either get: A) hWnd for app 'B' while in app 'A' (not sure how to do that) to use "BOOL SetForegroundWindow(HWND hWnd)" B) Get a pointer to the CWnd of app 'B' (not sure how to do this either) to use "BOOL CWnd::SetForegroundWindow( )" : Dean 'Karnatos' Michaud
-
I have a need of a way to bring one application to the foreground from another application. Application 'A' is either laucnhed by itself or it can be launched from application 'B' upon a button press. There's no guarantee which app was started first, or if the application to be brought to the foreground (app 'A') is even running at that point in time. If 'A' is not running I am launching it upon a button press in app 'B' - but if 'A' is already running I want to bring it to the foreground to alert the user it's already running (and not spawn a second instance of app 'A'). I'm currently using a mutex to check to see if app 'A' is already running, but I am not sure how to bring it to the foreground when it is. Any thoughts or ideas on how to proceed? : Dean 'Karnatos' Michaud
Are you running under W2K or XP? If so, SetForegroundWindow no longer works unless you own the process. There is a workaround - you attach your thread to the thread with current foreground thread and then SetForegroundWindow works. Check the link for a good example. D http://www.mooremvp.freeserve.co.uk/Win32/framed\_tip033.htm
-
I have a need of a way to bring one application to the foreground from another application. Application 'A' is either laucnhed by itself or it can be launched from application 'B' upon a button press. There's no guarantee which app was started first, or if the application to be brought to the foreground (app 'A') is even running at that point in time. If 'A' is not running I am launching it upon a button press in app 'B' - but if 'A' is already running I want to bring it to the foreground to alert the user it's already running (and not spawn a second instance of app 'A'). I'm currently using a mutex to check to see if app 'A' is already running, but I am not sure how to bring it to the foreground when it is. Any thoughts or ideas on how to proceed? : Dean 'Karnatos' Michaud
Ok, after mucking around with this a little more, seems this is *one* way of getting the job done. I am calling this code once a button is pressed to bring up an application to the foreground. 1) CWnd::FindWindow() finds the window based on the file name. 2) CWnd::SetWindowPos() shows the found window as on top, without resizing it or moving it. 3) CWnd::ShowWindow() is making sure the application is not minimized/maximized.
BOOL rc; CWnd\* my\_wnd = FindWindow(NULL, "Application-Name"); if (my\_wnd != NULL) { rc = my\_wnd->SetWindowPos(&wndTop, 0, 0, 0, 0, SWP\_NOMOVE|SWP\_NOSIZE|SWP\_SHOWWINDOW); rc = my\_wnd->ShowWindow(SW\_SHOWNORMAL); }
What I'd like to know is another way of getting the CWnd pointer to the window - I don't like writing code that depends on another applications having the same title (it very-well could change without me knowing, either by the dept. that wrote the code changing it and forgetting to notify me, or someone's changing the title of it for some purpose of their own). So, now my search moves onto looking for another way of finding a specific window that is running since FindWindow() relies on the window title not changing. : Dean 'Karnatos' Michaud
-
Are you running under W2K or XP? If so, SetForegroundWindow no longer works unless you own the process. There is a workaround - you attach your thread to the thread with current foreground thread and then SetForegroundWindow works. Check the link for a good example. D http://www.mooremvp.freeserve.co.uk/Win32/framed\_tip033.htm
I need to support Win98 to WinXP... I'd noticed in the MSDN Library that SetForegroundWindow() was not working as it had before in WinXP. Thank you though :) : Dean 'Karnatos' Michaud
-
I have a need of a way to bring one application to the foreground from another application. Application 'A' is either laucnhed by itself or it can be launched from application 'B' upon a button press. There's no guarantee which app was started first, or if the application to be brought to the foreground (app 'A') is even running at that point in time. If 'A' is not running I am launching it upon a button press in app 'B' - but if 'A' is already running I want to bring it to the foreground to alert the user it's already running (and not spawn a second instance of app 'A'). I'm currently using a mutex to check to see if app 'A' is already running, but I am not sure how to bring it to the foreground when it is. Any thoughts or ideas on how to proceed? : Dean 'Karnatos' Michaud
SetForegroundWindow. with some caveats. Here's a great article : http://www.etree.com/tech/Articles/attachthreadinput.pdf And here's some code to do it: DWORD MyThreadId=GetCurrentThreadId(); DWORD ForeThreadId=::GetWindowThreadProcessId(::GetForegroundWindow(),0); HWND hForegroundMe=0; // Get handle to the window you want to place in the foreground. // For instance use EnumThreadWindows if you know the ThreadId // (perhaps you have it saved in a shared memory?) // or use EnumWindows if you know the window text of the window // you are looking for... if (GetWindowLong(hForegroundMe,GWL_STYLE)&WS_MINIMIZE) ::ShowWindow(hForegroundMe,SW_RESTORE),::UpdateWindow(hForegroundMe); if (ForeThreadId!=MyThreadId) AttachThreadInput(ForeThreadId,MyThreadId,TRUE); ::SetForegroundWindow(hForegroundMe); if (ForeThreadId!=MyThreadId) AttachThreadInput(ForeThreadId,MyThreadId,FALSE);