Problem activating a window

I am using the following code:


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


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.
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

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: (Delphi)

Instead of all stuff above, call

VendiAuthor Commented:

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?
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.

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.

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();
            GetWindowThreadProcessId(theForegroundWnd, NULL),
GetCurrentThreadId(), TRUE);
HANDLE hWndMyWindow = FindWindow(NULL, "MyWindowTitle");
retVal = SetForegroundWindow(g_hWndMyWindow); // my window
            GetWindowThreadProcessId(theForegroundWnd, NULL),
            GetCurrentThreadId(), FALSE);

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

                                                        dwTimeoutMS, 0);
      return retVal;

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

From novice to tech pro — start learning today.