Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making Vista style apps in C#

I'm running windows vista and want to get the appearance to look like regular vista programs. Are there any really good tutorials/articles on how to build Vista Style apps? I would also like to learn how to use the native code and convert it to C# like in this example.

like image 355
Kredns Avatar asked Dec 09 '22 21:12

Kredns


1 Answers

If you're using WinForms it's relatively easy to accomplish it because WinForms is based on the native Win32 controls. Many controls have ways to enhance their rendering by setting additional flags (by sending messages to the native control) or using SetWindowTheme. This can be achieved through Interop.

As an example, take a simple ListView. If you want an explorer-style listview you use SetWindowTheme on the exposed handle of the ListView. We use interop to get access to the native SetWindowTheme() function, hook the window procedure of the ListView and apply the theme when the control is created:

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 ListView : System.Windows.Forms.ListView
{
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == NativeMethods.WM_CREATE) {
            NativeMethods.SetWindowTheme(this.Handle, "Explorer", null);
        }
        base.WndProc(ref m);
    }
}

The difference between a default ListView and our enhanced version: ListView difference http://img100.imageshack.us/img100/1027/62586064nt6.png

Unfortunately there isn't one simple way for every control. Some controls don't even have any WinForms wrapper. Recently I stumbled upon a nice compilation in this CodeProject article which is worth a look. There may also be managed libraries out there that package this.

like image 147
gix Avatar answered Dec 21 '22 14:12

gix