LimeLM
wyBuild
Support forum
Wyatt Says...
wyDay Home

"Wyatt Says..." is a collection of articles by Wyatt O'Day talking about wyDay products and the things we've learned along the way.


Wyatt Says...

Archive for the ‘Windows Vista’ Category

This is another easy tip to wrap up this series of articles.

Keyboard accessibility

Start your application and unplug your mouse. Can you navigate and use your application? If you hit the tab key does the next logical control get focused? Can you open the file menu by pressing Alt-F?

Set tab ordering

Simply set the “TabIndex” property of your controls in ascending starting with 0. This way when your users press the tab key they next focused control is the next control in the flow of your form.

Menu quick keys

To add “quick key” ability to your menus you just need to put an ampersand (“&”) before the letter that will be used for quick access. For example, instead of a menu item captioned “File”, caption it “&File” instead. Now your users can access that menu quickly by pressing Alt-F.

SystemStyle Buttons, Radio Buttons, and Checkboxes

In Windows Vista Microsoft changed the buttons, radio buttons, and checkboxes to have subtle animations. When you hover, check, and click these controls they all yield organic animations. But if you built your app using Windows Forms the subtle animations aren’t there.

To add these animations all you have to do is set the “FlatStyle” of these controls to “System”.

Windows UX Guide

Microsoft has assembled a great collection of guidelines for designing applications. It’s filled with screenshot examples showing good & bad designs. Plus it’s surprising self deprecating – some of their examples of bad design are screenshots taken directly from Microsoft apps.

You should skim the guide at least once and bookmark it for later.

7 Days of Windows 7

That’s it for the series. If you missed the earlier articles you can see the full list of articles in the series.

One of the great things about Vista and Windows 7 is the user isolation. Even admin users need to “elevate” their account to make system changes. Take this Date and Time dialog from Windows 7 as an example:

Every user can view the Date and time, but only administrators can change it.

Adding this ability to your .NET application

Although this series of articles is called “7 days of Windows 7” this particular article is applicable to Windows 2000 – Windows 7.

Step 1. Do we have permission?

The first step is to check if we can write to system registry or system files & folders. There are many ways to do this, but the easiest method is a simple Windows API call:

// check if user is an admin for Windows 2000 and above
[DllImport("shell32.dll", EntryPoint = "#680", CharSet = CharSet.Unicode)]
public static extern bool IsUserAnAdmin();

This will return false if you’re a limited user on Windows 2000 – Windows 7, and will also return false if you are an admin but aren’t elevated on Windows Vista and Windows 7.

In other words, it will return false if you don’t have permission to access system files & registry. And ‘ IsUserAnAdmin” returns true if you do have permission.

Step 2. Notifying the user that elevation will happen: UAC Shield Icon

To set the shield icon to one of your buttons you have to do a few things. First, set the FlatStyle of your button to “System”:

Next, you need to define a couple of functions:

public static bool AtLeastVista()
{
    return (Environment.OSVersion.Platform == PlatformID.Win32NT && Environment.OSVersion.Version.Major >= 6);
}

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr SendMessage(HandleRef hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

public static void SetButtonShield(Button btn, bool showShield)
{
    //Note: make sure the button FlatStyle = FlatStyle.System
    // BCM_SETSHIELD = 0x0000160C
    SendMessage(new HandleRef(btn, btn.Handle), 0x160C, IntPtr.Zero, showShield ? new IntPtr(1) : IntPtr.Zero);
}

Now, simply use this snippet in your code:

…
// UAC Shield on next button for Windows Vista+
if (AtLeastVista())
    SetButtonShield(btnName, true);
…

Step 3. Re-launching process with administrator privileges

All we have to do now is show the elevation dialog and elevate the current program. You might want to specify some arguments, but the barebones of it is as follows:

ProcessStartInfo psi = new ProcessStartInfo
                           {
                               Arguments = "-justelevated",
                               ErrorDialog = true,

                               // Handle is the handle for your form
                               ErrorDialogParentHandle = Handle,
                               FileName = Application.ExecutablePath,
                               Verb = "runas"
                           };
try
{
    Process.Start(psi);
    Close();
}
catch (Exception ex)
{
    // the process couldn't be started. This happens for 1 of 3 reasons:

    // 1. The user cancelled the UAC box
    // 2. The limited user tried to elevate to an Admin that has a blank password
    // 3. The limited user tries to elevate as a Guest account
    MessageBox.Show(ex.Message);
}

Step 4. Code signing

Chances are that if you try to elevate your application you’ll get an ugly yellow elevation box:


To get the nice UAC box you’ll need to code sign your application. I won’t link to any code signing providers (because the list is huge), but you can get a code signing certificate from anywhere between $100 for 3 years to $400 or $500 for a single year. It depends on the company you use and the amount of searching you want to do.

7 Days of Windows 7

Join me tomorrow when I talk about Every possible Windows Vista and Windows 7 .NET Control You could ever want. See the full list of articles in the series.

This will be another easy tip because all the work is already done and wrapped up in a nice little control. It’s open source and handles all of the details, get it here.

Progress bars in Windows 7 are nearly identical to what they were in Windows Vista. They’re green, animated and have 3 states: Normal, Error, and Paused:

Using Windows API

To get both the 3-state progress bar (regular, error, and paused) along with the taskbar progress bar we need to use some Windows API. The easier of the two to implement is changing the state of the progress bar. This works on Windows Vista and Windows 7:

[DllImport("user32.dll", CharSet = CharSet.Auto)]
internal static extern int SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);

SetPaused()
{
    // 0x410 = PBM_SETSTATE
    // 0x0003 = PBST_PAUSE
    SendMessage(progressBar.Handle, 0x410, 0x0003, 0);
}

SetError()
{
    // 0x0002 = PBST_ERROR
    SendMessage(progressBar.Handle, 0x410, 0x0002, 0);
}

SetNormal()
{
    // 0x0001 = PBST_NORMAL
    SendMessage(progressBar.Handle, 0x410, 0x0001, 0);
}

Then there’s the matter of the taskbar progress bar in Windows 7. You can download the Windows7ProgressBar source code (zip file) to see how I did it, but the gist of it is you need to implement the ITakbarList3 Interface, and then use the SetProgressState to set the state (Normal, Error, Paused, or Marquee) and SetProgressValue to set the value.

7 Days of Windows 7

Join me Monday when I talk about Drag & Drop icons. See the full list of articles in the series.