Interop’ng with Vista (Displaying Battery Power in the Taskbar) – Part 2

After a couple of days of developing (see previous post: Interop’ng with Vista (Displaying Battery Power in the Taskbar), I had a fairly working little utility to display the current battery status for my laptop working.  But what it had in functionality it lacked in appearance.

myMojo Battery Power Monitor v1

Every once in a while, I would pick up the project again, do some more Googling and try out a few more things but without any lock on getting the transparency to work.  All directions pointed at implementing the IDeskBand2 interface rather than the IDeskBand, though any attempt would just crash explorer when I would add the utility to the Taskbar.  So more Googling, more attempts later, I finally stumbled upon an excellent post which worked - http://cgeers.wordpress.com/2008/02/16/internet-explorer-toolbar/

I still implemented IDeskBand interface though overriding the OnPaintBackground using Geer’s supplied code

#region Transparency
[DllImport("uxtheme", ExactSpelling = true)]
public extern static Int32 DrawThemeParentBackground(IntPtr hWnd, IntPtr hdc, ref Rectangle pRect);
protected override void OnPaintBackground(PaintEventArgs e)
        {
            if (this.BackColor == Color.Transparent)
            {
                IntPtr hdc = e.Graphics.GetHdc();
                Rectangle rec = new Rectangle(e.ClipRectangle.Left,
                    e.ClipRectangle.Top, e.ClipRectangle.Width, e.ClipRectangle.Height);
                DrawThemeParentBackground(this.Handle, hdc, ref rec);
                e.Graphics.ReleaseHdc(hdc);
            }
            else
            {
                base.OnPaintBackground(e);
            }
        }
#endregion

and voila!

myMojo Battery Power Monitor v2

(and changing the progress bar to a set of images helped a lot)

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Live

Tags: , ,

Leave a Reply