Search code examples
wpfinteropwindowsformshost

Using SetWindowTheme() on controls in WindowsFormsHost in WPF?


I have an application I'm developing which closely mirrors Windows 7's Device Stage. In Device Stage, beneath the main banner there is a ListView containing actions embodied as ListViewItems.

In my WPF application, I used WindowsFormsHost to host a WinForms ListView so that I could use SetWindowTheme() on it and apply Windows Vista/7 styling to it.

This, however, does not work and doesn't achieve the same effect it does when used in Windows Forms.

How can I achieve the Windows 7 look on a ListView in WPF? I'm not looking to create a custom style then apply it because frankly that's too much of a pain in the ass to continue using WPF for this app.

Thanks! :)


Solution

  • Just add the following lines:

    [DllImport("uxtheme.dll", CharSet = CharSet.Unicode)] 
    public static extern int SetWindowTheme(IntPtr hWnd, String pszSubAppName, String pszSubIdList); 
    
    .ctor 
    {
        System.Windows.Forms.Integration.WindowsFormsHost.EnableWindowsFormsInterop();
        System.Windows.Forms.Application.EnableVisualStyles();
    
        SetWindowTheme(MyControl.Handle, "Explorer", null); 
    }