用盡研究之後,我發現它。問題是,當你調用
SetWindowTheme(this.Handle, "", "");
定製ListView
類中,它可以防止視覺樣式從影響ListView
控制的appearence但不是ListView
頭控制(SysHeader32
窗口),這是ListView
子窗口。 所以調用SetWindowTheme
功能時,我們需要提供的標題窗口,而不是ListView控件的句柄的句柄:
[DllImportAttribute("uxtheme.dll")]
private static extern int SetWindowTheme(IntPtr hWnd, string appname, string idlist);
[DllImport("user32")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);
// Callback method to be used when enumerating windows:
private static bool EnumWindow(IntPtr handle, IntPtr pointer)
{
GCHandle gch = GCHandle.FromIntPtr(pointer);
List<IntPtr> list = gch.Target as List<IntPtr>;
if (list == null)
throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");
list.Add(handle);
return true;
}
// delegate for the EnumChildWindows method:
private delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);
// get first child:
private static void DisableVisualStylesForFirstChild(IntPtr parent)
{
List<IntPtr> children = new List<IntPtr>();
GCHandle listHandle = GCHandle.Alloc(children);
try
{
EnumWindowProc childProc = new EnumWindowProc(EnumWindow);
EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));
if (children.Count > 0)
SetWindowTheme(children[0], "", "");
}
finally
{
if (listHandle.IsAllocated)
listHandle.Free();
}
}
protected override void OnHandleCreated(EventArgs e)
{
DisableVisualStylesForFirstChild(this.Handle);
base.OnHandleCreated(e);
}
因爲它是基於Kamil Lach建議的解決方案,所以我將獎勵他的答案與我提供的賞金。 – LihO 2012-04-17 14:12:29