Problem activating a window

hi,
I am using the following code:

AttachThreadInput(GetCurrentThreadId(),GetWindowThreadProcessId(hMenu,NULL),true);
::SetFocus(hMenu);
AttachThreadInput(GetCurrentThreadId(),GetWindowThreadProcessId(hMenu,NULL),false);

hMenu is a handle to an Internet Explorer 5 window.  I've also tried the following:

ShowWindow(hMenu,SW_RESTORE);
BringWindowToTop(hMenu);

The first code listing works once, then not again even though by using breakpoints, etc, I know the code is executed a 2nd time.  The 2nd code listing does not work at all.  Again, I know hMenu is a valid handle and I know the code is being executed.  Any ideas as to what is causing the problem?  I am using C++ Builder 4.0, Windows 98, Internet Explorer 5.
LVL 1
VendiAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
alexoConnect With a Mentor Commented:
I think you're confusing SetFocus() with SetForegroundWindow().
The SetFocus() function just sets the keyboard focus to the specified window.

Regarding SetForegroundWindow(): since MS, in their infinite wisdom, decided to change its behavior, we must cheat...

Please check the following references:
  http://www.deja.com/=dnc/getdoc.xp?AN=469050093
  http://www.deja.com/=dnc/getdoc.xp?AN=558494400
  http://www.deja.com/=dnc/getdoc.xp?AN=451249790
  http://www.deja.com/=dnc/getdoc.xp?AN=460536970 (Delphi)


0
 
NickRepinCommented:
Instead of all stuff above, call

SetForegroundWindow(hMenu);
0
 
VendiAuthor Commented:
http://msdn.microsoft.com/library/psdk/winui/windows_1eev.htm

This shows which processes can call SetForegroundWindow.  My program does not meet any of the requirements as far as I can see.  I have tried SetForegroundWindow and it does not work at all.  One note about the 2nd code example above (with BringWindowToTop).  It works fine with an application, like Notepad (and several others I've tried it on) but not with Internet Explorer.  Does this change your suggestion any?  Have you come across anything like this with Internet Explorer?
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
NickRepinCommented:
Vendi, if you want to bring some top-level window to top, activate it and assign the focus to it (at least, as I got it from your code above), the only way is SetForegroundWindow().

hMenu must be the handle of a top-level (not child) window. Explorer or not - it doesn't matter.

Another question, if you want to set the focus to it, for example, to send keystrokes. In this case, you have to use AttachThreadInput/SetFocus.

0
 
VendiAuthor Commented:
Thanks alexo!!  Those links were just what I needed.  Within 10 minutes, I had a working program!

BOOL MySetForegroundWindow(HWND hWnd)
{
      BOOL retVal = FALSE;

        // Save specified timeout period...
DWORD dwTimeoutMS;
SystemParametersInfo (SPI_GETFOREGROUNDLOCKTIMEOUT, 0, &dwTimeoutMS, 0);

// ... then set it to zero to disable it
        SystemParametersInfo (SPI_SETFOREGROUNDLOCKTIMEOUT, 0, (PVOID) 0, 0);

// Switch foreground window to my window
HWND theForegroundWnd = GetForegroundWindow();
AttachThreadInput(
            GetWindowThreadProcessId(theForegroundWnd, NULL),
GetCurrentThreadId(), TRUE);
HANDLE hWndMyWindow = FindWindow(NULL, "MyWindowTitle");
retVal = SetForegroundWindow(g_hWndMyWindow); // my window
AttachThreadInput(
            GetWindowThreadProcessId(theForegroundWnd, NULL),
            GetCurrentThreadId(), FALSE);

// then SetForegroundWindow here
if(retVal) {
  retVal = SetForegroundWindow(hWnd);
      }

SystemParametersInfo (SPI_SETFOREGROUNDLOCKTIMEOUT, 0, (PVOID)
                                                        dwTimeoutMS, 0);
      return retVal;
}

This is the code from the URL's alexo mentioned (modified a little by me :).
0
 
alexoCommented:
Glad that helped!
0
All Courses

From novice to tech pro — start learning today.