LimeLM
wyBuild
Support forum
wyDay blog
wyDay Home

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


wyDay blog

Archive for the ‘Microsoft’ Category

Those of you who follow this blog regularly already know the tip I’m sharing today. It’s about the control I created a couple of years ago: VistaMenu.

Normally I would just show a menu from Windows 7 and say “this is how you do it”. Take the menu above, for example. It’s the context menu when you right click the desktop in Windows 7. It’s subtle and usable. It’s less about being visually appealing than it is about being usable.

I find Windows Vista and Windows 7 menus to be aesthetically pleasing. However, not everyone would agree.

Aesthetics – ask 1000 people, get 1000 opinions

How do you judge art? It’s damn near impossible to get 100% consensus. Luckily programs aren’t art – they have utility first and style second.

Or, put more simply, if the style detracts from usability it is bad style.

Here are a couple of examples of bad menus. The following menus are tacky for the simple reason that the styling adds no extras usability. The excessive gradients and bad backgrounds distracts from the purpose of the menus:

Difference between MenuStrip and MainMenu

If you’ve been programming with Windows Forms for any length of time, it’s very likely you already use MenuStrips and ContextMenuStrips. They are entirely custom-draw menus created by Microsoft and included in .NET Framework 2.0+.

However, the theme of the MenuStrip and ContextMenuStrip controls don’t automatically adjust to match the Windows 7 theme. But, as you can see below, the MainMenu and ContextMenu controls do match the Windows 7 theme.

You can add MainMenu & ContextMenu to Visual Studio by right clicking the toolbox, clicking “Choose Items…” and checking the “MainMenu” and “ContextMenu” check boxes.

Why you should always use MainMenu

If all the discussion of aesthetics was lost on you, then you should always use MainMenu for two simple reasons:

  1. Your program will look like it fits in with all the other programs on your users’ computers
  2. Microsoft does the hard work for you. That is, with each new version of Windows Microsoft subtly tweaks the menu to be more usable.

The MenuStrip and ContextMenuStrip controls are written entirely in C# and will forever look as tacky as they do now. The MainMenu and ContextMenu, however, are simple wrappers around the Windows API. This means that whenever Microsoft updates the menu API in Windows you will get all the usability improvements without lifting a finger.

Look at the screenshot below and you can see how Microsoft even changed the menu API between Windows Vista and Windows 7. The borders are sharper, the colors are tweaked, and the gradient is toned down.

Adding icons to MainMenu and ContextMenus

Microsoft was good enough to add simple API for adding icons to menus. I wrote a .NET wrapper for this API a couple of years ago, VistaMenu. Enough chit-chat, here’s how you use VistaMenu. It’s open source and works with Windows 98 – Windows 7.

7 Days of Windows 7

Join me tomorrow when I talk about Windows 7 progress bars. See the full list of articles in the series.

This will be the quickest tip you’ll 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 Microsoft’s own apps. See the ugly font in the window on the right:

On the left I’m using the default system font in Windows Vista & 7: Segoe UI. On the right I’m using the default font that every Windows Forms app uses: Microsoft Sans Serif. It’s 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 isn’t in Pre-Vista systems

The problem is that Segoe UI is only shipped with Vista & 7 versions of Windows. So you can’t 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 

That’s 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 we’re 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 won’t be able to do it in the designer, so instead you’ll 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.

If you’ve done any Windows Forms designing then you’ve undoubtedly used the LinkLabel control. You’ve also likely noticed its hideous look:

The ugliness of the LinkLabel that ships with Windows Forms is four fold.

  1. The repulsive navy blue color reminds me of Netscape Navigator (anyone else remember the graceful “Illegal Operation” crashes?).
  2. There’s no hover effect. OK, I lied. There’s a hover effect but you have to code it. And if you do code it, you better put on your epilepsy goggles – it’s gonna flicker.
  3. The non-standard hand cursor is missing the shadow and the gradient.
  4. The LinkLabel font is oddly rendered:

The Windows.Forms LinkLabel looks as though the text was drawn to the screen once, then drawn again without erasing the first copy. This gives the LinkLabel a jagged, “blocky” look.

LinkLabel2 – How I fixed it

It was a simple bit of code. I used ‘TextRenderer.DrawText(…)’ to do the actual drawing. The hand cursor was simply a matter of intercepting the WM_SETCURSOR message to my LinkLabel2 control:

[DllImport("user32.dll")]
public static extern int LoadCursor(int hInstance, int lpCursorName);

[DllImport("user32.dll")]
public static extern int SetCursor(int hCursor);

protected override void WndProc(ref Message m)
{
    //WM_SETCURSOR == 32
    if (m.Msg == 32)
    {
        //IDC_HAND == 32649
        SetCursor(LoadCursor(0, 32649));

        //the message has been handled
        m.Result = IntPtr.Zero;
        return;
    }

    base.WndProc(ref m);
}

But what about SysLink?

SysLink is a link control added to “ComCtl32.dll” in Windows XP. So, why not use SysLink? Because SysLink doesn’t exist in Windows 2000/98.

Maybe in 3 years, when Windows 2000 is dead, a simple SysLink wrapper for C# can be written.

Get it

It works with all the .NET 2.0 + languages (C#, VB.NET, etc.).

Also while you’re at it download wyUpdate, or any of my other open source projects.

Go to the LinkLabel2 site for further updates.