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

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
alexoCommented:
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

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.

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