An Icon Overlay Notifier for Halfwit

You are looking at revision 1 of this page, which may be out of date. View the latest version.

Here's an example of a StatusesReceivedEvent handler for Halfwit 2 that overlays the number of new tweets on your taskbar icon in Windows 7.

First, the PlugIn class to register our handler:

public class PlugIn : IHalfwitPlugIn
{
    public void Initialize(Autofac.ContainerBuilder builder)
    {
        builder.RegisterType<Handler>().AsImplementedInterfaces();
    }
}

Pretty straight forward. Now the handler itself:

class Handler : IHalfwitEventHandler<StatusesReceivedEvent>
{
    public void Handle(StatusesReceivedEvent e)
    {
        var wnd = Application.Current.MainWindow;
        if (wnd.TaskbarItemInfo == null)
        {
            wnd.TaskbarItemInfo = new TaskbarItemInfo();
        }

        var grid = new Grid { Width = 20, Height = 20 };
        var ellipse = new Ellipse { Fill = Brushes.DarkRed };
        var text = new TextBlock
        {
            Text = e.Items.Count().ToString(),
            TextAlignment = TextAlignment.Center,
            Foreground = Brushes.White,
            FontSize = 11,
            FontWeight = FontWeights.Bold,
            Height = 16,
            VerticalAlignment = VerticalAlignment.Center,
            Effect = new DropShadowEffect { BlurRadius = 2, Color = SystemColors.InfoColor, ShadowDepth = 0 },
        };

        grid.Children.Add(ellipse);
        grid.Children.Add(text);

        RenderTargetBitmap bmp = new RenderTargetBitmap(20, 20, 96, 96, PixelFormats.Default);
        grid.Arrange(new Rect(0, 0, 20, 20));

        bmp.Render(grid);

        wnd.TaskbarItemInfo.Overlay = (ImageSource)bmp;

        var timer = new DispatcherTimer(TimeSpan.FromSeconds(5), DispatcherPriority.Background, (sender, args) =>
            {
                ((DispatcherTimer)sender).Stop();
                wnd.TaskbarItemInfo.Overlay = null;                    
            }, wnd.Dispatcher);
        timer.Start();
    }
}

So we're finding the application's main window and setting its TaskbarItemInfo property if it doesn't have one. Then we're rendering the number of new tweets as white text on a dark red ellipse. We wait for five seconds, then we hide it.

Taskbar Icon Overlay

Big thanks to Pete Brown whose post Creating Dynamic Windows 7 Taskbar Overlay Icons served as the inspiration for this plug-in! I'll make the plug-in available as a download (it's only an 8KB DLL) once Halfwit 2 has been released.

halfwit mvvm ioc windows-7
Posted by: Matt Hamilton
Last revised: 03 Dec, 2010 11:35 AM History
You are looking at revision 1 of this page, which may be out of date. View the latest version.