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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

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!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.