ToDo’s and WPF

Much like Scott Hanselman with Baby Smash, I used this as an excuse to play with WPF.  If you’re like me, chances are you have a ToDo.txt on your desktop.  It tends to get lost in the sea of windows I have open OR I just forget about it for a few days.

So here is my solution.  It took about 5 hours to code from start to a stable, usable state.  A WPF application that “lives” on my desktop that lists all my todo’s.  The source code will be posted over at CodePlex shortly at http://codeplex.com/todo/ along with post an article over at Coding4FunI need to transcode it to Visual Basic still.

Update:  Code posted over at CodePlex with c# and VB.  Plus I did a release for it too!

The really cool thing is the list on your desktop will update in real time with the ToDo list!

image 

Really?  WPF?

Yup, I tried to do the transparent text with a Win32 app first and gave up after 15 minutes.  Never doing WPF before, I had a rough proof of concept in 3 minutes.  My toolset was WPF due to this.

Smooth Sailing though … just a bit choppy

I had a few WTF moments with WPF.  Dan Fernandez had a few moments of me grumbling due to this.

So with WPF, it works radically different with threading.  I used a FileSystemWatcher object to get notifications with the ToDo’s and in a Win32 application, I could update the user interface directly.

WPF is a bit more, what is the word I’m looking for, anal, yup, that’s it, about thread safety.  This is good and bad.

To update the user interface element, you need to talk to the Dispatcher object.  Here is the code I used for my application.

private delegate void NoArgDelegate();
private void todoWatcher_Changed(object sender, FileSystemEventArgs e)
{
    txtTodoList.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new NoArgDelegate(loadTextBox));
}

Another interesting thing was relearning where stuff was again.  Screen.Width was moved and a this.Handle.

Here is some of the OnLoad code.  As you see, to get a window handle, you have to use the WindowInteropHelper.  To fetch your screen size, use SystemParameters.PrimaryScreenWidth.  The SystemParameters has a ton of other items in it too on top of just screen size.

var helper = new WindowInteropHelper(this);

Win32.SetWindowPos((int) helper.Handle,
                   Win32.HWND_BOTTOM,
                   0, 0, 0, 0,
                   Win32.SWP_NOMOVE | Win32.SWP_NOSIZE |
                   Win32.SWP_SHOWWINDOW);

Win32.HideFromAltTab(helper.Handle);

Width = SystemParameters.PrimaryScreenWidth;
Top = SystemParameters.PrimaryScreenHeight - Height;

Another head turner was the lack of a NotifyIcon in WPF.  The easy workaround is to include the System.Windows.Forms dll and then reference it in.  Also means you need to manually create your ContextMenuStrip which is a tad of a bummer.  Adding in the code below will gain you a notify icon along with an exit button.

private NotifyIcon notifyIcon;
private void createNotifyIcon()
{
    notifyIcon = new NotifyIcon {Text = "ToDo", Icon = new Icon("report.ico"), Visible = true};

    // have to use the Forms version
    var contextMenu = new ContextMenuStrip { Name = "contextMenu" };
    var exitToolStripMenuItem = new ToolStripMenuItem {Name = "exitToolStripMenuItem1", Text = "Exit"};
    
    // contextMenu
    contextMenu.Items.AddRange(new ToolStripItem[] { exitToolStripMenuItem });
    contextMenu.Size = new Size(155, 114);
    
    // exitToolStripMenuItem1
    exitToolStripMenuItem.Size = new Size(154, 22);
    exitToolStripMenuItem.Click += exitToolStripMenuItem_Click;

    notifyIcon.ContextMenuStrip = contextMenu;
}

void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
    notifyIcon.Dispose();

    Close();
}

Once I got past doing stuff the new way over the old way, I have to say, WPF has some really powerful stuff built in.  I love how it forces you to think multi-threaded too right off the bat!

Chris Gray Jul 18, 2008 @ 12:13 PM

# re: ToDo’s and WPF
This is a great little app. I have been eager to try out WPF, I can't wait to try this out. Thanks.

Scott Hanselman Jul 29, 2008 @ 1:27 AM

# re: ToDo’s and WPF
Sweet! I'm sold.

Post a Comment

Please add 8 and 7 and type the answer here: