0

I'm using the SetWindowTheme function (uxtheme.dll) to make my ListView look like Windows 7 native. I had to use the CreateParams stuff to prevent that ListView flickering when I sort it. It worked, but when I use that CreateParams code, the SetTheme doesn't work anymore. Is there a way to use the Windows 7 native theme and the CreateParams at the same time ?

protected override CreateParams CreateParams
{
    get
    {
        CreateParams cp = base.CreateParams;
        cp.ExStyle |= 0x02000000;  // Turn on WS_EX_COMPOSITED
        return cp;
    }
}


[DllImport("uxtheme.dll", ExactSpelling = true, CharSet = CharSet.Unicode)]
    private static extern int SetWindowTheme(IntPtr hwnd, string pszSubAppName, string pszSubIdList);

public MainWindow()
{
    InitializeComponent();
    SetWindowTheme(listView1.Handle, "Explorer", null);
}

Thanks

EDIT : I solved the problem by adding a SetWindowTheme call in the OnHandleCreated void of my custom ListView. Thanks to Hans Passant.

zdimension
  • 977
  • 1
  • 12
  • 33
  • Works on my machine™. SetWindowTheme returns an error code, clearly you ought to be more interested in its value. The code is wrong, it should be called in an override for ListView.OnHandleCreated(). Which requires you to derive your own class from ListView. Which also gives you a good opportunity to set its DoubleBuffered property to *true*, good odds that you now no longer need the WS_EX_COMPOSITED hack at all anymore. – Hans Passant Apr 21 '14 at 13:31
  • 1
    Essentially you need to call `SetWindowTheme` after the window handle is created. And window handles can get re-created. Hence `OnHandleCreated`. – David Heffernan Apr 21 '14 at 14:06

0 Answers0