The wyDay blog is where you find all the latest news and tips about our existing products and new products to come.

wyDay blog

Posts in the ‘vb.net’ tag

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 youre a limited user on Windows 2000 Windows 7, and will also return false if you are an admin but arent elevated on Windows Vista and Windows 7.

In other words, it will return false if you dont 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 youll get an ugly yellow elevation box:

To get the nice UAC box youll need to code sign your application. I wont 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 the quickest tip youll ever read. But first, some background. If run your .NET app on Windows Vista or Windows 7 unaltered you might notice the fonts look a little funky. Especially when compared to Microsofts own apps. See the ugly font in the window on the right:

On the left Im using the default system font in Windows Vista & 7: Segoe UI. On the right Im using the default font that every Windows Forms app uses: Microsoft Sans Serif. Its quite obvious that the Segoe UI font is slightly larger and more legible. Not only that, but if you use the font on the right instead the Microsoft Sans Serif your app will be out of place in Windows Vista & 7, since every other app in Vista & 7 uses the Segoe UI font.

Problem: Segoe UI isnt in Pre-Vista systems

The problem is that Segoe UI is only shipped with Vista & 7 versions of Windows. So you cant just hard-code the Font property of the form to be Segoe UI (or your app will look like utter crap in Windows 2000 & Windows XP).

And thus this simple snippet of code to use Segoe UI on Windows Vista & 7 while still using the default system font in Windows 98, 2000 & XP.

public Form1()
{
// use Segoe UI in Vista & 7
Font = SystemFonts.MessageBoxFont; InitializeComponent();
}

Or, in VB.NET:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' use Segoe UI in Vista & 7
Font = SystemFonts.MessageBoxFont
End Sub

Thats all you have to do to use Segoe UI font in your app. And if you set the AutoScaleMode of the form to Font then all of the controls on the form will scale to fit the larger font:

Bold font problem

What if you want to bold a title? Simple, right? Just click the Font property of the label you want to bold:

However, the problem with doing this is now every control on the form except the label you want to bold will be Segoe UI font. Why is this? Every control on your form will have the same font as the parent unless specified otherwise.

In our case the parent is the Form and were explicitly setting the font to be bold for our label to be Bold Microsoft Sans Serif.

Ok, ok but how do I make a bold label use Segoe UI?

We just learned you wont be able to do it in the designer, so instead youll have to do it in code:

public Form1()
{
// use Segoe UI in Vista & 7
Font = SystemFonts.MessageBoxFont; InitializeComponent(); // set the title to be bold
lblTitle.Font = new Font(Font, FontStyle.Bold);
}

Or in VB.NET

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' use Segoe UI in Vista & 7
Font = SystemFonts.MessageBoxFont ' set the title to be bold
lblTitle.Font = New Font(Font, FontStyle.Bold)
End Sub

7 Days of Windows 7

Join me tomorrow when I talk about adding professional looking menus to your app. See the full list of articles in the series.

For the next 7 Days Ill be giving 7 C# & .NET tips to make your Windows application look like it was built for Vista and Windows 7. All of these tips are used in our products (wyBuild, wyUpdate, and LimeLM) and they range from the simple one-liner, free open source controls, and to the advanced tip.

And since wyBuild, wyUpdate, and LimeLM are all compatible with Windows 2000 through Windows 7 all of these tips will be backward compatible.

7 days of Windows 7

Day 1: Windows Vista & 7 Font, Segoe UI, in C# and VB.NET Day 2: Making the menus in your .NET app look professional Day 3: Windows 7 Progress Bar in .NET Day 4: Drag & drop icons in .NET Windows 2000, XP, Vista, and 7 Day 5: Shield icons, UAC, and process elevation in .NET on Windows 2000, XP, Vista, and 7 Day 6: Windows Vista and Windows 7 .NET Controls Every possible one you could ever want Day 7: Finishing touches: Make your .NET app shine with professionalism