MFC Interop: ThemeManager.SetTheme() on UserControl in HwndSource

WPF Studio, Themes, and Shared Library for WPF Forum

Posted 6 years ago by Tom P.
Version: 12.2.0570
Platform: .NET 4.0
Environment: Windows 7 (64-bit)
Avatar

I'm putting a WPF interface on a legacy MFC application using the WPF HwndSource object.  It works surprisingly well.

I'm having a ThemeManager issue however. The first time my UserControl is displayed, the Theme doesn't seem to fully take effect. If I close and display it a second time, it works perfectly.

Currently, I'm putting the typical ThemeManager BeginUpdate/EndUpdate in my UserControl.Loaded event handler.

private void UserControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
  ThemeManager.BeginUpdate();
  try
  {
    ThemesMetroThemeCatalogRegistrar.Register();
    ThemeManager.SetAreNativeThemesEnabled(this, true);
    ThemeManager.SetTheme(this, ThemeName.MetroLight.ToString());
  }
  finally
  {
    ThemeManager.EndUpdate();
  }
}

My theory is, since this is MFC Interop, and I don't explicitly create the WPF Application object, I have to do all the ThemeManager stuff somewhere else.

Thoughts?

Comments (3)

Answer - Posted 6 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Tom,

The Loaded event for WPF controls fires after the control has already been realized.  I'd recommend that you run the ThemeManager configuration code before you ever instantiate any sort of WPF UI.  We do it in our app startup code in normal WPF apps.


Actipro Software Support

Posted 6 years ago by Tom P.
Avatar

Yeah, I get that. I guess I was looking for advice as to where/when to do it since in a pure MFC Interop context, there is no App startup code since we don't explicitly create an Application object.

 

Maybe in my call to create my HwndSource object. I'll try that.

Posted 6 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Tom,

Sorry we don't really do MFC here so I'm not sure what advice to offer in that regard.  What you suggested sounds like a good idea though.


Actipro Software Support

The latest build of this product (v2019.1 build 0682) was released 3 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.