Determine cause for window's activation

I have a main window (WinMain) that on Windows only (doesn’t happen on Mac) is randomly activating when it doesn’t have focus, bringing it to the front periodically. I’m trying to figure out what in my code is causing this, but setting a break point for the window’s activate event doesn’t show anything in the stack besides the main thread.

I’ve also set breakpoints throughout the entire app for anything calling, but none of those are called before it activates.

Is there a good way to easily trace what might be calling the function?

Is there a modal dialog involved, called via ShowModal?

Nope no modals or anything else being shown, it’s just a regular old document window coming to the front periodically.

Just to sure we’re on the same page, I’m asking if it’s possible that the main window is coming to the front after a model dialog is being closed?

Nope no other window is opening or closing. WinMain (document window) is open in the background, a second document window is being used in the foreground, WinMain randomly activates and takes focus. When debugging the breakpoint shows that it has activated but doesn’t show what called the activate method.

Look for “.Show” in your code to see if that offers a clue.

Yes all instances of WinMain.Show have a breakpoint set and none are coming up. I set a breakpoint for all 1263 matches of just the “.show” string in the project and none of those break when debugging. Is there another method that would cause a document window to come to the front on Windows?

Are you accessing any of the controls or properties of that window elsewhere? (I’m grasping at straws here.)

Maybe too obvious of a question, but is the offending window set to be an implicit instance?


I feel like I had similar behavior in the past with a Window which where this was set (and I didn’t mean for it to be).

I think it was this combined with some bad naming and bad (non-existent) encapsulation which was causing the window to get focus when I was calling a routine I thought was in a module, but was actually in the window. (this was a number of years ago)

1 Like

Rename WinMain to wMain and run; with a little luck, you may discover who’s calling it.

Else (but may be the same) made a global search on WinMain and look at the code.

Also make sure that you don’t have a Timer or other code that is calling SetFocus on the window or one of its controls.

So after digging around for a while, it seems to be triggered by an HTMLViewer using me.LoadPage() method (which is necessary for how the app works).

  • If I comment out that LoadPage line of code then the WinMain does not come to focus
  • I’ve already commented out all other events for the HTMLViewer so it’s nothing in those causing focus
  • This only happens on Windows

So would this be a bug? I tried making a small sample app to reproduce the issue but haven’t been able to do so quite yet.

I’m not saying it’s not, but if you can’t reproduce it in a small project, it’s less likely that it’s a bug in the framework and more likely a bug in your code.

What version are you using? 2002R2 release notes have
a couple of issues related to HTMLViewers and Focus, such as


1 Like

The project is maintained in 2019r32 right now, but I just loaded it into 2020r21 and the same thing happens so I don’t think that case is related.

Is there an INPUT element on that page that uses JavaScript to set its focus? Sounds like that’s what’s happening.

This is in a desktop app, and the issue still occurs when loading a basically blank page with just text into the HTMLViewer. I’ve also checked all the custom code for the HTML loaded page and nothing in there related to focus.

If the page you’re loading in to the HTMLViewer is that simple and you’re seeing this issue, it sounds like it’s time to open a Feedback Case and let the engineers take a peek.

Native doesn’t come to the front unless something on the page takes keyboard focus (like where as doesn’t take focus and doesn’t come to front.

Webkit comes to the front even on pages that don’t take focus like

I’d say it was a bug as you should be able to, for example, load a page in the background and bring it to the front if/when needed in code rather than at load time.


Ah you’re right! It’s a Webkit html viewer in the app but in sample app I was attempting to make I didn’t think to set that for Windows. After changing to Webkit it becomes reproducible.

The problem is that Webkit rendering is basically required for this app, since Native is a lot less… good.