1

I'm trying to force an internet explorer window to be TOPMOST. No other windows should show on top of the internet explorer window. I have to use internet explorer. I'm also running this in Windows 7. Apparently that makes a difference but all the information I found on that is rather vague and basically consists of people shouting "why isn't this easier!". This is my code:

HWND ieWin = FindWindow(TEXT("IEFrame"), 0);
SetFocus(ieWin);
SetWindowPos(ieWin, HWND_TOPMOST, 0, 0, 100, 100, SWP_SHOWWINDOW );
UpdateWindow(GetParent(ieWin));

This is running in a loop so I can force the window to have these settings continuously but it's not helping. Sometimes the above code works and sometimes it doesn't.

dlee777
  • 11
  • 2
  • 2
    See http://blogs.msdn.com/b/oldnewthing/archive/2011/03/10/10138969.aspx. Newer versions of Windows have put in measures to try to block applications that insist on behaving badly. – Mark Ransom Mar 09 '12 at 15:47
  • 1
    Most likely your users won't be thrilled by this behavior. – Mark B Mar 09 '12 at 15:55
  • 1
    I'm not thrilled by this behaviour but the users asked for it and no is not an option. Hopefully, it can't be done will be an acceptable answer. – dlee777 Mar 09 '12 at 16:04
  • `HWND badWin = FindWindow("YourWindow"); SendMessage(badWin, WM_CLOSE);` – MSalters Mar 09 '12 at 16:06
  • 1
    @dlee777: Raymond Chen (who we linked to) is a Microsoft architect. You can tell your users that _Microsoft explicitly tells you not to do this_. – MSalters Mar 09 '12 at 16:07

2 Answers2

3

No. What if two programs did this?.

MSalters
  • 173,980
  • 10
  • 155
  • 350
0

After much discussion I did wind up forcing the window to stay on top using a loop. If anyone else really needs to do this in the future they really need to make sure they are following the steps below.

1) Ensure that they will have absolute control over the windows that their user can open. In my case The user will only be able to open my program and internet explorer.

2) The first time you set the IE window to show set it to the foreground.

3) While looping to set the window to TOP_MOST do NOT set the focus to the IE window.

4) Make sure you have SWP_SHOWWINDOW, SWP_NOSIZE, and SWP_NOMOVE. Otherwise that loop will change your window's size and place at every iteration.

And if you can avoid it at all possible, don't ever do this at all :P

dlee777
  • 11
  • 2