Making Vista style apps in C#

2020-06-05 08:09发布

问题:

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.

回答1:

If you're using WPF you can use Microsoft's Vista Bridge library which has several useful controls. Otherwise, just look at the Windows UX Guidelines and roll your own. From that page:

Some of the features included in the Vista Bridge Sample Library are - Vista style Task and File Dialogs, Common Open and Save dialogs, Application Recovery and Restart, Known Folders, Network Lists, Power Management, User Account Control, CommandLink control, Aero Wizard Control, System provided icons etc.

Additionally, you can check out Aero.Controls and Aero.Wizard, two free open source WinForms packs I wrote that set you up with some standard controls and a wizard, respectively.



回答2:

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 == Win.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.



回答3:

If you want apps for Windows XP to just look 100% like Vista, then it's going to be very complex as you will need to override the way Windows are painted so you can design your own compatible version.

Usually these things are done one the Windows theme-level, sometimes by including special handlers that change the way all apps look.
Then there are tools like FastAero that add transparency to all Windows XP forms (source code available).

In any case, if you want your apps to look Vista-like, you need to respect the Windows User Experience Interaction Guidelines for Aero.

You can also use WPF to fake the Aero-style of controls so your apps will look similar on any version of Windows.



回答4:

You could take a look at a third party solution.

We use DevExpress to achieve nicely styled WinForm apps on WinXP, specifically their WinForms ribbon controls