SetForegroundWindow fails
-
That's how XP works. It flashes the taskbar instead of having the application take over the screen. If you can, try your App. on a Win 98 machine just to make sure it's XP and not your code.
I found a work-around for this problem. http://codeproject.com/dialog/dlgboxtricks.asp[^]
-
I found a work-around for this problem. http://codeproject.com/dialog/dlgboxtricks.asp[^]
Thanks a bunch.:rose: Looks promising. Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com
-
I found a work-around for this problem. http://codeproject.com/dialog/dlgboxtricks.asp[^]
Well my initial hopes have been dashed.:( It appears to work when I run the App in the Debugger, but not otherwise. I think the problem is the first app is stealing back or not relinquishing focus. I have another path I'm about to head down. I use SetForegroundWindow() at other times in the App to restore from the System Tray for example and they work fine. Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com
-
Hi, I have two windows processes and one sends (posts) a mesage to the other to activate it etc. When the main app gets this message it calls SetForegroundWindow() to bring the app to the foreground so it is the active/on top application. The problem is SetForegroundWindow() is failing (returning 0) and my app stays in the background. I've also tried BringWindowToTop(). The WinXP Task Bar flashes the Icon for my app. I'm going nuts here.:( Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com
The old way was to use AttachThreadInput-- (there's a hefty list of under just what conditions SetForegroundWindow will work) search for that in the Message Boards. The new way to use MS's settlement API: SwitchToThisWindow http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowfunctions/switchtothiswindow.asp[^]
-
The old way was to use AttachThreadInput-- (there's a hefty list of under just what conditions SetForegroundWindow will work) search for that in the Message Boards. The new way to use MS's settlement API: SwitchToThisWindow http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowfunctions/switchtothiswindow.asp[^]
Hi Scott, Thanks for that. I'd never heard of SwitchToThisWindow(). This sort of works, however there are two problems. First the Task Bar pops-up (I have it to Auto-Hide) and flashes the app icon. Second and worse is if I Alt+Tab to another app afer my app has come to the foreground using SwitchToThisWindow() then cause my app to activate using SwitchToThisWindow() the aforementioned app is activated not mine. This is a killer. Finally SwitchToThisWindow sends the previous app to the bottom of the Z-Order which seems very strange. AFAIK this isn't what SetForegroundWindow() does. I can't believe something as simple as this is causing me so much grief. Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com
-
Hi, I have two windows processes and one sends (posts) a mesage to the other to activate it etc. When the main app gets this message it calls SetForegroundWindow() to bring the app to the foreground so it is the active/on top application. The problem is SetForegroundWindow() is failing (returning 0) and my app stays in the background. I've also tried BringWindowToTop(). The WinXP Task Bar flashes the Icon for my app. I'm going nuts here.:( Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com
As others have said this is the way XP is supposed to work; users don't like have the rug window they are working in being pulled away without warning. However, if you are certain you have a really good reason to do it (warning of an impending nuclear strike or the re-election of Dubbya), you can (usually) do it this way:
///
// Bring a window to the foreground. Ignore the howls of protest from users.
///
BOOL StealFocus(CWnd *pWnd)
{
// Attach foreground window thread to our thread
DWORD ForeGroundID = GetWindowThreadProcessId(::GetForegroundWindow(), NULL);
DWORD CurrentID = GetCurrentThreadId();
BOOL bRet = AttachThreadInput(ForeGroundID, CurrentID, TRUE);
pWnd->SetForegroundWindow();
pWnd->SetFocus(); //Just playing safe
// Detach the attached thread
AttachThreadInput(ForeGroundID, CurrentID, FALSE);
return bRet;
}
The opinions expressed in this communication do not necessarily represent those of the author (especially if you find them impolite, discourteous or inflammatory).
-
As others have said this is the way XP is supposed to work; users don't like have the rug window they are working in being pulled away without warning. However, if you are certain you have a really good reason to do it (warning of an impending nuclear strike or the re-election of Dubbya), you can (usually) do it this way:
///
// Bring a window to the foreground. Ignore the howls of protest from users.
///
BOOL StealFocus(CWnd *pWnd)
{
// Attach foreground window thread to our thread
DWORD ForeGroundID = GetWindowThreadProcessId(::GetForegroundWindow(), NULL);
DWORD CurrentID = GetCurrentThreadId();
BOOL bRet = AttachThreadInput(ForeGroundID, CurrentID, TRUE);
pWnd->SetForegroundWindow();
pWnd->SetFocus(); //Just playing safe
// Detach the attached thread
AttachThreadInput(ForeGroundID, CurrentID, FALSE);
return bRet;
}
The opinions expressed in this communication do not necessarily represent those of the author (especially if you find them impolite, discourteous or inflammatory).
Hi Phil, Thanks for the reply. I couldn't get this to work reliably either as indicated in an earlier reply. It worked in the debugger, but not outside. Both of these apps are mine, so I just need the ability to get one to bring the other to the foreground. I've read up on all the issues and what MS is trying to do and why and I'm basically ok with that. But in my case I'm not pulling any rugs out from under anyone but myself.;) I have a solution which seems to work, but I'm not as happy as I'd like with it. I'm about to try doing the SetForgroundWindow() in in App A to get it to bring App B to the foreground and my understanding is this should work. Up untill now I was trying to get App B to bring itself to the foreground. This is the start of a new day for me (here in Oz) which I hope will be better than yesterday.:| Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com
-
Hi Phil, Thanks for the reply. I couldn't get this to work reliably either as indicated in an earlier reply. It worked in the debugger, but not outside. Both of these apps are mine, so I just need the ability to get one to bring the other to the foreground. I've read up on all the issues and what MS is trying to do and why and I'm basically ok with that. But in my case I'm not pulling any rugs out from under anyone but myself.;) I have a solution which seems to work, but I'm not as happy as I'd like with it. I'm about to try doing the SetForgroundWindow() in in App A to get it to bring App B to the foreground and my understanding is this should work. Up untill now I was trying to get App B to bring itself to the foreground. This is the start of a new day for me (here in Oz) which I hope will be better than yesterday.:| Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com
It works fine for me in and out of the debugger as a way for an application to bring itself to the foreground. I simply have
StealFocus(this)
in a dialog that shows a vital message.
The opinions expressed in this communication do not necessarily represent those of the author (especially if you find them impolite, discourteous or inflammatory).
-
It works fine for me in and out of the debugger as a way for an application to bring itself to the foreground. I simply have
StealFocus(this)
in a dialog that shows a vital message.
The opinions expressed in this communication do not necessarily represent those of the author (especially if you find them impolite, discourteous or inflammatory).
Phil J Pearson wrote: It works fine for me in and out of the debugger as a way for an application to bring itself to the foreground. I simply have StealFocus(this) in a dialog that shows a vital message. What version of Windows are you using? Does this bring the app to the foreground or just the dialog? I assume both as one will follow the other but need to ask. I'm using XP with SP1. I'm also using a P3 550Mhz box which tends to show problems that newer, faster machines don't. Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com
-
Phil J Pearson wrote: It works fine for me in and out of the debugger as a way for an application to bring itself to the foreground. I simply have StealFocus(this) in a dialog that shows a vital message. What version of Windows are you using? Does this bring the app to the foreground or just the dialog? I assume both as one will follow the other but need to ask. I'm using XP with SP1. I'm also using a P3 550Mhz box which tends to show problems that newer, faster machines don't. Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com
It works on Windows 2000, XP, XP SP1a and XP SP2 on a range of hardware from Celeron 300 onwards. It brings the whole app to the foreground.
The opinions expressed in this communication do not necessarily represent those of the author (especially if you find them impolite, discourteous or inflammatory).