Click here to Skip to main content
Click here to Skip to main content

TaskbarNotifier, a skinnable MSN Messenger-like popup in C# and now in VB.NET too

By , 30 Mar 2003
 

Introduction

Since I am learning C#, I thought it would be helpful for me to port my C++ CTaskbarNotifier class (http://www.codeproject.com/dialog/TaskbarNotifier.asp[ ^])

As a result, I coded a MSN Messenger-like skinnable popup, with a close button which looks almost like Microsoft's one (with the associated skin).

The TaskbarNotifier class inherits from System.Windows.Forms.Form and adds a few methods to it.

Features

The MSN messenger like popup supports:

  • A custom transparent bitmap background
  • A skinnable 3-State close button
  • A clickable title text
  • A clickable content text
  • A selection rectangle
  • Custom fonts and colors for the different states of the text (normal/hover)
  • Animation speed parameters

Compatibility

This class is stand alone and doesn't need any particular libraries except .NET default ones. It runs in managed code and hence should be portable.

How to use the class

  • First of all, copy TaskbarNotifier.cs in your project directory.
  • Add on the top of your source code the directive:
    using CustomUIControls;
  • Add a member variable in your class:
    TaskbarNotifier taskbarNotifier;
  • In your constructor add the following lines:
    taskbarNotifier=new TaskbarNotifier();
    taskbarNotifier.SetBackgroundBitmap("skin.bmp",
                        Color.FromArgb(255,0,255));
    taskbarNotifier.SetCloseBitmap("close.bmp",
            Color.FromArgb(255,0,255),new Point(127,8));
    taskbarNotifier.TitleRectangle=new Rectangle(40,9,70,25);
    taskbarNotifier.ContentRectangle=new Rectangle(8,41,133,68);
    taskbarNotifier.TitleClick+=new EventHandler(TitleClick);
    taskbarNotifier.ContentClick+=new EventHandler(ContentClick);
    taskbarNotifier.CloseClick+=new EventHandler(CloseClick);

    Details:

    taskbarNotifier.SetBackgroundBitmap("skin.bmp",
                        Color.FromArgb(255,0,255));
    taskbarNotifier.SetCloseBitmap("close.bmp",
        Color.FromArgb(255,0,255),new Point(127,8));
    

    The first line sets the background bitmap skin and transparency color (must be present), and the second line sets the 3-State close button with its transparency color and its location on the window (this line is optional if you don't want a close button).

    taskbarNotifier.TitleRectangle=new Rectangle(40,9,70,25);
    taskbarNotifier.ContentRectangle=new Rectangle(8,41,133,68);

    These two lines allow us to define the rectangles in which will be displayed, the title and content texts.

    taskbarNotifier.TitleClick+=new EventHandler(OnTitleClick);
    taskbarNotifier.ContentClick+=new EventHandler(OnContentClick);
    taskbarNotifier.CloseClick+=new EventHandler(OnCloseClick);

    These 3 lines allow us to intercept events on the popup such as title/content or close button have been clicked

  • Then we are done, we just need to call:

    taskbarNotifier.Show("TitleText","ContentText",500,3000,500);

    This will show the popup animation with the showing/visible/hiding animations time set as 500ms/3000ms/500ms.

You can play with a few properties:

  • Title, content fonts and colors
  • Ability to click or not on the title/content/close button
  • You can disable the focus rect
  • ... (see below for more details)

Class documentation

Methods

void Show(string strTitle, string strContent, int nTimeToShow, 
                                            int nTimeToStay, int nTimeToHide)

Displays the popup for a certain amount of time.

Parameters

  • strTitle: The string which will be shown as the title of the popup
  • strContent: The string which will be shown as the content of the popup
  • nTimeToShow: Duration of the showing animation (in milliseconds)
  • nTimeToStay: Duration of the visible state before collapsing (in milliseconds)
  • nTimeToHide: Duration of the hiding animation (in milliseconds)
void Hide()

Forces the popup to hide.

void SetBackgroundBitmap(string strFilename, Color transparencyColor)

Sets the background bitmap and its transparency color.

Parameters

  • strFilename: Path of the background bitmap on the disk
  • transparencyColor: Color of the bitmap which won't be visible
void SetBackgroundBitmap(Image image, Color transparencyColor)

Sets the background bitmap and its transparency color.

Parameters

  • image: Background bitmap
  • transparencyColor: Color of the bitmap which won't be visible
      void SetCloseBitmap(string strFilename, 
                  Color transparencyColor, Point position)

      Sets the 3-State close button bitmap, its transparency color and its coordinates.

      Parameters

      • strFilename: Path of the 3-state close button bitmap on the disk (width must be a multiple of 3)
      • transparencyColor: Color of the bitmap which won't be visible
      • position: Location of the close button on the popup
      void SetCloseBitmap(Image image, Color transparencyColor, Point position)

      Sets the 3-State close button bitmap, its transparency color and its coordinates.

      Parameters

      • image: Image/Bitmap object which represents the 3-state close button bitmap (width must be a multiple of 3)
      • transparencyColor: Color of the bitmap which won't be visible
      • position: Location of the close button on the popup

      Properties

      string TitleText (get/set)
      string ContentText (get/set)
      TaskbarStates TaskbarState (get)
      Color NormalTitleColor (get/set)
      Color HoverTitleColor (get/set)
      Color NormalContentColor (get/set)
      Color HoverContentColor (get/set)
      Font NormalTitleFont (get/set)
      Font HoverTitleFont (get/set)
      Font NormalContentFont (get/set)
      Font HoverContentFont (get/set)
      Rectangle TitleRectangle (get/set) //must be defined before calling show())
      Rectangle ContentRectangle (get/set) //must be defined before calling show())
      bool TitleClickable (get/set) (default = false);
      bool ContentClickable (get/set) (default = true);
      bool CloseClickable (get/set) (default = true);
      bool EnableSelectionRectangle (get/set) (default = true);

      Events

      event EventHandler CloseClick
      event EventHandler TitleClick
      event EventHandler ContentClick

      Technical issues

      The popup is skinned using a region generated dynamically from a bitmap and a transparency color:

      protected Region BitmapToRegion(Bitmap bitmap, Color transparencyColor)
      {
          if (bitmap == null)
              throw new ArgumentNullException("Bitmap", "Bitmap cannot be null!");
      
          int height = bitmap.Height;
          int width = bitmap.Width;
      
          GraphicsPath path = new GraphicsPath();
      
          for (int j=0; j<height; j++ )
              for (int i=0; i<width; i++)
              {
                  if (bitmap.GetPixel(i, j) == transparencyColor)
                      continue;
      
                  int x0 = i;
      
                  while ((i < width) && 
                          (bitmap.GetPixel(i, j) != transparencyColor))
                      i++;
      
                  path.AddRectangle(new Rectangle(x0, j, i-x0, 1));
              }
      
          Region region = new Region(path);
          path.Dispose();
          return region;
      }

      The refresh() of the popup is done using the double buffering technique to avoid flickering:

      protected override void OnPaintBackground(PaintEventArgs pea)
      {
          Graphics grfx = pea.Graphics;
          grfx.PageUnit = GraphicsUnit.Pixel;
      
          Graphics offScreenGraphics;
          Bitmap offscreenBitmap;
      
          offscreenBitmap = new Bitmap(BackgroundBitmap.Width, 
                                      BackgroundBitmap.Height);
          offScreenGraphics = Graphics.FromImage(offscreenBitmap);
      
          if (BackgroundBitmap != null)
          {
              offScreenGraphics.DrawImage(BackgroundBitmap, 
                  0, 0, BackgroundBitmap.Width, BackgroundBitmap.Height);
          }
      
          DrawCloseButton(offScreenGraphics);
          DrawText(offScreenGraphics);
      
          grfx.DrawImage(offscreenBitmap, 0, 0);
      }

      Bugs/Limitations

      Since I wanted to keep only managed code, I used the Screen.GetWorkingArea(WorkAreaRectangle) function instead of using unmanaged code to get the taskbar position. As a result, I made the popup always appear at the bottom of WorkAreaRectangle whichever position the taskbar has.

      I didn't find any C# managed equivalent to the Win32 function ShowWindow(SW_SHOWNOACTIVATE) to make the popup, not steal the focus of the active window.

      Updates

      • 01 April 2003: Small bug fix in the OnMouseUp handler.
      • 11 January 2003: Patrick Vanden Driessche updated both the C# and VB.NET versions:
        • The popup now doesn't close automatically when the mouse is still over it
        • The popup is shown again when it was disappearing and the mouse comes over it
        • A few other bugs have been corrected.
      • 10 January 2003: A port of TaskbarNotifier has been done by Patrick Vanden Driessche in VB.NET
      • 05 December 2002: The popup is now shown using the Win32 function ShowWindow(SW_SHOWNOACTIVATE), to prevent the popup from stealing the focus.

      Conclusion

      I hope this code will be useful to you. If you have suggestions to enhance this class functionalities, please post a comment.

    • License

      This article, along with any associated source code and files, is licensed under A Public Domain dedication

      About the Author

      John O'Byrne
      Web Developer
      United States United States
      Member
      I live in Santa Clara CA and work as a software engineer for SAP Business Objects.
       
      My areas of expertise are user interface developments in Eclipse RCP / SWT / Draw 2D and C#

      Sign Up to vote   Poor Excellent
      Add a reason or comment to your vote: x
      Votes of 3 or less require a comment

      Comments and Discussions

       
      Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
      You must Sign In to use this message board.
      Search this forum  
          Spacing  Noise  Layout  Per page   
      QuestionHeigth of taskbarnotifier did not incrementmemberTiger.Hajj26 Apr '13 - 19:36 
      GeneralMy vote of 4memberBurak Tunçbilek30 Jul '12 - 11:50 
      QuestionAlign / Center - other positions for "titleText" and "contentText"membersmartradio12 Jul '12 - 3:16 
      QuestionMultiple PopupsmemberGenJerDan20 Apr '12 - 9:58 
      GeneralThanksmemberforester joe27 Mar '12 - 5:41 
      GeneralMy vote of 5membermanoj kumar choubey24 Feb '12 - 2:45 
      QuestionWindows 7 bugsmemberAnfai Inyotu1 Feb '12 - 5:29 
      AnswerRe: Windows 7 bugsmemberJonnysg019 Feb '12 - 5:49 
      QuestionRe: Windows 7 bugsmemberZoltanar23 May '12 - 9:47 
      John.
      First, THANKS a lot for this piece of code! It is just great, easy to use, and so nice.
       
      Regarding Windows 7, could you please confirm if the bug fixes are all in the code you posted above?
      The problem is the Disapearing method and the transparency which become Black on Win7.
       
      I would love to have this for VB.NET...
       
      Thanks again!
      Zolt
      AnswerRe: Windows 7 bugsmemberAnfai Inyotu4 Jun '12 - 9:38 
      GeneralRe: Windows 7 bugsmemberAnfai Inyotu4 Jun '12 - 10:51 
      GeneralRe: Windows 7 bugsmemberMember 44827692 Aug '12 - 23:48 
      QuestionHow to change Font and/or Font Size for Title and Content?membervb_programmer9 Jan '12 - 22:03 
      Questionmy vote of 5memberAl Moje20 Dec '11 - 14:45 
      QuestionWTL Task Bar NotifiermemberGene OK17 Aug '11 - 7:32 
      GeneralMy vote of 5memberAnirudha_baba28 Apr '11 - 23:59 
      Generalany updates for windows 7 and vs2010 - .net 4.0memberalhambra-eidos26 Apr '11 - 3:07 
      GeneralFails in Threading.memberdiefightdie8 Apr '11 - 1:10 
      GeneralRe: Fails in Threading.membersaiden27 Sep '11 - 12:28 
      GeneralRe: Fails in Threading.memberMarkJoel6011 May '12 - 12:27 
      QuestionTaskbar icon does not show in system tray?memberlinhjob4 Apr '11 - 3:47 
      GeneralThanks for this nifty notification controlmembermeaningoflights14 Mar '11 - 19:59 
      GeneralLicensemembermahboobar12 Mar '11 - 18:26 
      GeneralRe: LicensememberJohn O'Byrne12 Mar '11 - 19:09 
      GeneralTaskbarNotifier, a skinnable MSN Messenger-like popup in C# and ...membergalvin verghese3 Jan '11 - 22:29 
      QuestionStealling Focusmemberhappy k2 Dec '10 - 3:38 
      GeneralMy vote of 5memberbahruz8816 Nov '10 - 22:36 
      GeneralThank youmemberTDung794 Nov '10 - 23:22 
      GeneralVu Van Truongmembervuvantruong19 Sep '10 - 22:49 
      GeneralMemory consumption fixmembershput2 Aug '10 - 10:11 
      GeneralRe: Memory consumption fixmembersee_seA10 Oct '10 - 3:13 
      GeneralRe: Memory consumption fixmemberLife Lockszmith22 May '12 - 11:55 
      QuestionHow you make bitmap act like buttonmemberPassion_Knight2 Jul '10 - 10:46 
      GeneralMove the popup.memberWondermaker23 Jun '10 - 2:00 
      QuestionBackground AlphaBlending not working on Windows 7memberC. Beyazit5 Apr '10 - 7:23 
      AnswerRe: Background AlphaBlending not working on Windows 7membersee_seA21 Apr '10 - 1:48 
      QuestionI want to let the pop up appears only when a change happens in an XML file that I have so how i can do this..anyone can help??memberSherif W. Girgis13 Mar '10 - 11:17 
      AnswerRe: I want to let the pop up appears only when a change happens in an XML file that I have so how i can do this..anyone can help??memberbobmcclane14 Jul '10 - 0:59 
      QuestionHow to display more than one notifier at same time?memberMayurPanchal11 Mar '10 - 19:56 
      AnswerRe: How to display more than one notifier at same time?membermarcodimarco25 Mar '10 - 0:55 
      QuestionEmbedd resourcemembermarcodimarco7 Mar '10 - 23:15 
      AnswerRe: Embedd resourcememberNatza Mitzi8 Mar '10 - 23:40 
      GeneralRe: Embedd resourcemembermarcodimarco25 Mar '10 - 0:09 
      QuestionWhere is the Missing Resource file for TaskBarNotifier.vb?membertimnboys27 Feb '10 - 7:59 
      AnswerRe: Where is the Missing Resource file for TaskBarNotifier.vb?membermarcodimarco28 Feb '10 - 22:23 
      GeneralTrigger closing on eventmembermarcodimarco25 Feb '10 - 21:41 
      GeneralRe: Trigger closing on eventmembermarcodimarco25 Feb '10 - 22:25 
      GeneralRe: Trigger closing on eventmemberleechangjun29 Mar '11 - 22:09 
      GeneralInclude in project? [modified]membermarcodimarco24 Feb '10 - 23:46 
      Generalpropage message through networkmemberTrumanChua5 Feb '10 - 6:01 

      General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

      Permalink | Advertise | Privacy | Mobile
      Web01 | 2.6.130516.1 | Last Updated 31 Mar 2003
      Article Copyright 2002 by John O'Byrne
      Everything else Copyright © CodeProject, 1999-2013
      Terms of Use
      Layout: fixed | fluid