c# WinForm 控件样式 _____

自己用 WinForm 拖控件做出来的程序样式:

图片说明

Win7 系统样式:

图片说明

请问 Win7 系统的控件样式是怎么做出来的?

以上面 ListView 为例。项选中的时候跟 Win7 系统的差好远。

我是想知道原理,有代码的话最好啦!

 static class NativeMethods
{
    public const int WM_CREATE = 0x1;

    [DllImport("uxtheme.dll", CharSet = CharSet.Unicode)]
    public extern static int SetWindowTheme(
        IntPtr hWnd, string pszSubAppName, string pszSubIdList);
}

class MyListView : System.Windows.Forms.ListView
{
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == Win.WM_CREATE) {
            NativeMethods.SetWindowTheme(this.Handle, "Explorer", null);
        }
        base.WndProc(ref m);
    }
}

用这个MyListView

自己绘制效果,它有绘制item的事件,你也可以找一些第三方的,来美化你的程序

http://www.codeproject.com/Articles/18078/Vista-Controls
原理介绍

static class NativeMethods
{
public const int WM_CREATE = 0x1;

[DllImport("uxtheme.dll", CharSet = CharSet.Unicode)]
public extern static int SetWindowTheme(
    IntPtr hWnd, string pszSubAppName, string pszSubIdList);

}

class MyListView : System.Windows.Forms.ListView
{
protected override void WndProc(ref Message m)
{
if (m.Msg == Win.WM_CREATE) {
NativeMethods.SetWindowTheme(this.Handle, "Explorer", null);
}
base.WndProc(ref m);
}
}

自定义消息处理并改变显示效果