Windows API Code Pack for the .NET Library


In this post I demoed some of the managed wrappers that are available for .NET developers to target the Windows Vista and Windows 7 API’s. The Vista Bridge has now been officially renamed to Windows API Code Pack for the .NET Library and will be available after Window 7 is released.

I must express some disappointment, that this is going to be made available as a separate library, as I would have much preferred it if this was a part of the forthcoming .NET framework 4.0., and I could use the controls in Visual Studio. It may wall be that Windows 7 will be released before .NET Framework 4.0., which may well explain this, as anyone that has tried the Windows 7 beta, will attest to it containing .NET Framework 3.5 SP1 only.

Windows Vista (and Windows 7) Bridge for managed developers


To quote the Windows SDK Team

“Many of you don’t know about the Vista Bridge sample library, what can it do or even where you can find it.

Let’s start with the basics; i.e. definitions. Vista Bridge is a sample library that contains managed wrappers for a number of Windows Vista APIs that are not exposed in the .NET Framework. The wrappers include common Open and Save file dialogs and new Vista advanced task dialogs.”

This SDK is very late ( by at least a couple of years) and contains a lot of features managed developers have moaned and moaned about being missing.

Finally, now, in version 1.4, we are nearing something that developers can use in their applications. The new Windows 7 taskbar – in fact all of the Windows 7 API’s – will be included in future versions of the Vista Bridge. If you are looking to develop your managed application targeting the new Windows 7 API’s, then the code is available here.

The great thing is that you can use both Windows Forms and Windows Presentation Foundation, though the samples are mostly WPF.

Libraries

Note: There is a CHM with information on how to use the controls in Windows Forms, and hopefully a fully documented Visual Basic CHM as well. Here are a few screenshots of the controls

Aero Wizard

AeroWizard

Breadcrumb Control

BreadCrumb

Vista and Windows 7 Interop

VistaInteop

From the Interop samples above you have

ComplexTaskDialog

Customised Open File Dialog

OpenFileDialogCustomised

So, all-in-all there are some very useful controls that I hope will be fully released in the Windows 7/Visual Studio 2010 time frame, and will be incorporated into Visual Studio 2010.

If you develop smart client applications that will be targeting Windows 7, then I really do urge you get to know and use the Windows 7 Taskbar API’s. The Windows team have dramatically changed the way that the taskbar is used, with reduced repetition, jump lists and so forth, with the common file dialog being an axis that the Windows 7 Taskbar is built upon.

Windows 7 TreeView and ListView


The default Tree and List view controls in windows forms look rather dated. In an earlier post, I demonstrated just how easy it was to change the theme of the TreeView control and ListView controls so they look like Vista’s explorer.

The same code can be used to attain the Windows 7 Tree and List view controls look and feel.

TreeAndList

Here I have a TreeView with an ImageList, and a ListView with an ImageList that has some large images, and am populating it when the form loads. If you would like to go further with the other controls (buttons etc.), there is a very good article on codeproject.com worth checking out.

Visual Basic (c# example beneath)

Imports System.Runtime.InteropServices

Public Class Form1

    Private random As Random

 

    Public Sub New()

        Me.Font = System.Drawing.SystemFonts.IconTitleFont

 

        InitializeComponent()

 

        SetWindowTheme(treeView1.Handle, "explorer", Nothing)

        SetWindowTheme(listView1.Handle, "explorer", Nothing)

 

        Me.treeView1.HotTracking = True

        Me.treeView1.FullRowSelect = True

    End Sub

    ”’ <summary>

    ”’ Native interop method, be sure to include the  System.Runtime.InteropServices

    ”’ name space

    ”’ </summary>

    ”’ <param name="hWnd"></param>

    ”’ <param name="appName"></param>

    ”’ <param name="partList"></param>

    ”’ <returns></returns>

    <DllImport("uxtheme.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)> _

    Private Shared Function SetWindowTheme(ByVal hWnd As IntPtr, ByVal appName As String, ByVal partList As String) As Integer

    End Function

 

 

    ”’ <summary>

    ”’ Using an image collection in an ImageList component, fill the

    ”’ ListView control with 50 random images when the form loads

    ”’ </summary>

    ”’ <param name="sender"></param>

    ”’ <param name="e"></param>

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

        random = New Random()

 

        Me.treeView1.ExpandAll()

        Me.listView1.LargeImageList = Me.imageList2

        Me.listView1.BeginUpdate()

 

        For i As Integer = 0 To 49

            Dim item As New ListViewItem()

 

            item.ImageIndex = random.[Next](0, imageList2.Images.Count)

 

            Dim name As String = DirectCast(item.Name, String)

 

            item.Text = "Item " & name

 

            Me.listView1.Items.Add(item)

        Next

 

        Me.listView1.EndUpdate()

    End Sub

 

 

End Class

C#

using System;

using System.Windows.Forms;

using System.Runtime.InteropServices;

 

namespace WindowsSevenTreeViewAndListView

{

    public partial class Form1 : Form

    {

        Random random;

 

        public Form1()

        {

            this.Font = System.Drawing.SystemFonts.IconTitleFont;

 

            InitializeComponent();

 

            SetWindowTheme(treeView1.Handle, "explorer", null);

            SetWindowTheme(listView1.Handle, "explorer", null);  

 

            this.treeView1.HotTracking = true;

            this.treeView1.FullRowSelect = true;

        }

        /// <summary>

        /// Native interop method, be sure to include the  System.Runtime.InteropServices

        /// name space

        /// </summary>

        /// <param name="hWnd"></param>

        /// <param name="appName"></param>

        /// <param name="partList"></param>

        /// <returns></returns>

        [DllImport("uxtheme.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]

        private static extern int SetWindowTheme(IntPtr hWnd, string appName, string partList);

 

        /// <summary>

        /// Using an image collection in an ImageList component, fill the

        /// ListView control with 50 random images when the form loads

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void Form1_Load(object sender, EventArgs e)

        {

            random = new Random();

 

            this.treeView1.ExpandAll();

            this.listView1.LargeImageList = this.imageList2;          

            this.listView1.BeginUpdate();

 

            for (int i = 0; i < 50; i++)

            {

                ListViewItem item = new ListViewItem();

 

                item.ImageIndex = random.Next(0, imageList2.Images.Count);

 

                string name = (string)item.Name;

 

                item.Text = "Item " + name;

 

                this.listView1.Items.Add(item);

            }

 

            this.listView1.EndUpdate();

        }

    }

}

Why Vista is perceived to be a failure


Building software is like architecture in a lot of ways. An array of engineering disciplines is required to pull off the feat of building a house or apartment block. Civil engineering is a requirement for the foundations and structure of the building. Mechanical engineering is a requirement for the materials that are structurally used. Electrical engineering is required for the wiring, not to mention all the above and more to get the supplies to and from the building site. The electrical and water supply connection to the house are likely to use Automotive engineering and Aeronautical engineering as well.

Windows Vista is built on a superfluity of computer science and engineering disciplines, all the way from in-house coding implementations, native code, to managed code. The more one delves into writing code, the more appreciation one has for the underlying operating system. This is why I love Windows, Linux and Apple Macintosh equally. Yes all have their strengths, but as software engineer and someone that adores software, I find it beyond belief, that one can ever entirely dismiss one platform for another. They are all marvellous achievements!

As a .NET developer though, my day to day focus is on Windows, and the Vista operating system is where most of my day is consumed. I have written about the lacklustre finish of this operating system, but am even more disappointed with the lack of Windows Presentation Foundation applications that are available to use on the platform. It was a decision by Microsoft to implement this new presentation layer for Windows Vista, but they have not supplied any applications for the general user to enjoy. This is why the operating system has got so much bad press, because as a general user, nothing ostensibly distinguishes XP from Vista at present. The same 100% of applications one used on XP are the same 100% of applications you use on Windows Vista. Microsoft is reluctant to update any of its successful applications, because there is no profit in it.

You have to have a very good reason to knock a house down and rebuild it. It is apparent immediately that it is very expensive, when you consider the expertise required to engineer a house. Microsoft has done this with Windows Vista and Windows Presentation Foundation. Anyone that has ever programmed Win32 or Windows Forms will tell you what a nightmare it is to try and change the appearance of a control. Take the built in calendar control for instance. It is painful. Microsoft resolved to change this, alleviate the pain and introduce WPF. The only problem is they have yet to release a major application that uses WPF, and at present, appear unlikely to do so until the next Windows 7 version. This is poisonous, because Vista will now always be seen as a failure, because it ostensibly failed to improve the application environment from XP. Most users are not silly and know this.

To put it another way, you have smashed down your old house, and built a new one (albeit with a stronger foundation, but the old one was strong enough – other houses in the same street are still standing. Like XP), and then proceeded to use the same 15 year old dirty carpets, and 10 year old gas boiler, even the sinks, shower and bathtubs are the same grotty ones you had in the previous house. You’ve also decided to paint the house exactly the same colour as previous, kept the same sofas that the dogs have chewed over the last 5 years; even the beds and bedding have remained the same. It’s the same 10 year old television (some people have flat screens now in the same street), the same phones and light fittings, the same toilets and curtains, even down to the same dustbin and doors. You have added a state of the art security system though from a company called UserAccountControl. Your washing machine and fridge have both worked for the last 15 years, so you see no reason to change to newer models because there is no benefit; it costs more, for the same functionality.

I know this analogy is rather ‘over-the-top’, but this is how your users feel, and this is why they complain, and it will only get worse for Microsoft, unless it changes the furniture for something newer, nicer, fresher, lighter and brighter. This is how people feel about software. This is the place we live in visually, more and more each day. We will never come to your dinner parties again (Vista) because your house is grotty, depressing and archaic. We also know to look out for the same furniture in Windows 7

Windows 7, a case of The Emperor’s New Clothes


Popular blogger’s Long Zheng and Jeff Atwood have disquisitions on inconsistencies in the user interface of Windows Vista which are quite frankly nothing but an embarrassment to whoever was responsible for the user interface for the operating system. I would hate to be the one in charge and have to look as this plethora of user interface inconsistencies. This is in stark contrast to the Office 2007 release, which is also a huge engineering feat and a huge chunk revenue stream-wise into MSFT so much so, you really wonder if Jensen Harris would not be better in charge of Windows 7.

The fact of the matter is that there is no such thing as a new operating system as far as Windows is concerned, only the old version with some additional features. It is this that’s the most disappointing thing about Vista, insofar as it taking so long to procure, but retaining profusely ostensible relics from XP and Windows 2000. This makes the assertion that “The WOW starts now” in Vista seem like a betrayal. Long and Jeff are fastidious – it’s part of their craft – but a great deal of users feel exactly the same. They may not be able to communicate their thoughts and dismay with such written eloquence, suffice to say their derision is just as effective.

If you are going to release a new version of Windows, especially one focused on differentiated UI, then you need to practice what you preach! I for one expect pretty much all that is in Vista, plus new things like touch-screen and device oriented features to be in Windows 7, but Microsoft need to do a complete overhaul of their legacy dialogue boxes. The sad thing is it’s not a major engineering feat and does not require too many resources, just attention to detail that’s all.

Even in Office 2007, the minute you go into the tools Options in Outlook,

OutlookOptions

you face what will be a ten year old dialogue box, by the time the next version of Office comes out. It surely cannot be that difficult to ensure that all the icons used here are consistent with the rest of the user interface. And the ugly gray form, why not improve it and use the same skinning as the main application? These are the final touches needed to give an application a feeling of newness, and something Microsoft proudly almost always get wrong. Look at the treeview in office

OldOutlookTreeView

and now compare it to the one in Vista

NewTreeView

If only the Windows, Office and Visual Studio teams could have communicated and say “hey, tree navigation structure is important in all these applications, lets unify them”.

The long-and-the-short-of-it is that you are lumbered with the old MFC treeview that must at least be a decade old. This is why a lot of people are reluctant to move to Vista, because a lot of the applications they use for 10 hours a day, 5 days a week, are exactly the same on XP as they are on Vista. It makes no sense for a business to upgrade to Vista, especially as the nuts-and-bolts of their operations, usually lie in Office and proprietary software. No room for “The WOW starts now”, and absolutely no return on investment for retraining your IT department to support something that is the equivalent of The Emperors New Clothes.

How to use the Office 2007 Ribbon Control


The lead designer for the ribbon has this video from mix 08.

I really cannot recommend highly enough just how much you should watch this. Especially if you are using a ribbon control in your application. Jensen takes you on a tour of the prototyping the Office team did, consequently you know when to use the ribbon and in what context it is correct to do so. I have come across a lot of developers who have just used the ribbon ‘willy-nilly’ because it was the ‘new kid on the block’, and resulted in creating a really bad application, from a usability point of view. The ribbon solves one key user interface problem, and understanding what this problem is will result in you creating a better application.

There are lessons to be learned here that mean that even if you are not using the ribbon, just the thought process itself is sufficently edifying. It is fascinating just how bad the Office 2003 user interface is, and the scalability that the ribbon creates for your application. One is left beyond doubt just why Office 2007 is far better than the previous version, with an unimpeachable presentation from Jensen.

Re-installing Corrupt .NET Framework 2.0 (Do not delete the .NET folder)


I recently un-installed the .NET framework 2.0 as it was corrupt, then deleted the folder from
C:\Windows\Microsoft.NET\Framework\v2.0.50727.

When using Visual Web Developer in Visual Studio I got a message saying that it could not connect to the local webserver. The reason why is because this WebDev.WebServer.exe resides in the .NET framework folder hence was no longer available.

To avoid this problem, rename the folder, then re-install the framework. If you’ve deleted this already, install VWD express as that will re-install the webserver as a “workaround”