Click here to Skip to main content
Email Password   helpLost your password?

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:

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

You can play with a few properties:

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

void Hide()

Forces the popup to hide.

void SetBackgroundBitmap(string strFilename, Color transparencyColor)

Sets the background bitmap and its transparency color.

Parameters

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

    void SetCloseBitmap(Image image, Color transparencyColor, Point position)

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

    Parameters

    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

    Conclusion

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

  • You must Sign In to use this message board.
     
     
    Per page   
     FirstPrevNext
    GeneralI 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??
    Sherif W. Girgis
    12:17 13 Mar '10  
    Please I need your reply urgently
    GeneralHow to display more than one notifier at same time?
    MayurPanchal
    20:56 11 Mar '10  
    How to display more than one notifier at same time?

    when another notifier open it hoide previous one i want to show all notifier same time.

    can you tell me how to do that?

    Thanks
    QuestionEmbedd resource
    marcodimarco
    0:15 8 Mar '10  
    Hello,

    How to really embed the bmp's into the .exe file ? Ive set the build action to embedded resource and copy to output dir to copy always. I understand that the images are copied to the output folder when building the solution but the program wont run when i delete them. How to create 1 single .exe file with the images compiled into it ?
    AnswerRe: Embedd resource
    Natza Mitzi
    0:40 9 Mar '10  
    Add the bmp to the resource file (make sure that it has a good short name).
    - Double click the project properties folder.
    - Go to the resources tab.
    - In the resources tab, Select "images".
    - Drag the file into the panel.

    From now, (assuming the project name is MyProject and the image name is image1) you can use it using:

    MyProject.Properties.Resources.image1
    QuestionWhere is the Missing Resource file for TaskBarNotifier.vb?
    timnboys
    8:59 27 Feb '10  
    Where is the Missing Resource file for TaskBarNotifier.vb?
    AnswerRe: Where is the Missing Resource file for TaskBarNotifier.vb?
    marcodimarco
    23:23 28 Feb '10  
    You have to embed the resource files manually. It probable is missing the %skin%.bmp file. Also make sure that the resources files are copied to the build location on every build/debug.
    GeneralTrigger closing on event
    marcodimarco
    22:41 25 Feb '10  
    Hello,

    I've succesfully imported your class and created a real nice popup. I want to let you know that this is a very great class!

    I also would like to ask if it would be possible to trigger the closing event when another event is triggered?

    Example:
    Im building a tool to display caller information on incoming calls. Ive got several events, for example the ringing event: means new call. You popup is triggered when this event is fired. Now i want you popup to close when: the user presses the close button (already in your script), when a certain period of time elapsed (already in your script) and when the call is terminated (not in your script).

    So upon firing the callTerminated event, close the popup, even if the delay time is not over yet.

    Thanks in advance!
    “Programmer: An organism that can turn caffeine into code.”

    GeneralRe: Trigger closing on event
    marcodimarco
    23:25 25 Feb '10  
    I feel so stupid...already going to answer my own question for the second time.
    Ive just did the following as a test and it works...:

    private void pictureBox1_Click(object sender, EventArgs e)
    {
    taskbarNotifier1.Close();
    }

    Thanks anyway!
    GeneralInclude in project? [modified]
    marcodimarco
    0:46 25 Feb '10  
    Hi,

    I'll just go ahead and probable ask the most noobish question, but how to include this in my project ? I dragged the TaskbarNotifier.cs file into my solution explorer but how to link this to my application ? I do have 'using CustomUIControls;' but i dont think thats the include for that file .. is it ?

    Thanks in advance (im very new in at this language!)

    Never mind, i already answered my own question. Works great, good stuff!!!
    modified on Thursday, February 25, 2010 6:02 AM

    Generalpropage message through network
    TrumanChua
    7:01 5 Feb '10  
    hi john,

    just read your article on codeproject

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

    was wondering if you've seen any articles or have any thoughts on
    how to wire this software up so that you can pop the message on another
    computer other than your own, basically propagate the message through
    an existing network

    great article.

    thank you.

    truman

    tchua@wolve.com
    QuestionText Alignment of strContent
    flaurita
    6:04 19 Nov '09  
    How can I change the "center text alignment" of the strContent?

    I tryed to play with the StringFormat object but without luck.
    I'd like to align text on the left of the ContentRectangle.

    Tnx in advance
    GeneralEvents won't fire
    nowh3r32h1d3
    0:03 30 Oct '09  
    Weird.
    Everything works fine except my events won't fire.
    Even the "textRectangle" won't show up.

    What could be the problem?

    (Nice Toast!)
    GeneralNotifier do not appear if client code is on a class library o console project
    Francesco Randazzo
    6:08 14 Oct '09  
    I hava an application with multiple dll. in the deeper application layer there is a dll containing the NotifierTaskbar class(called deep.dll). a client dll (called client.dll) intercepts an event, prepare the taskbar class to show and call the show method. The problem is that the popup do not appear.
    Any idea? There is something i ignore?
    p.s. I tried to call the dll containing the taskbarclass from a console project but the problem persists.
    GeneralRe: Notifier do not appear if client code is on a class library o console project
    allentranks
    22:43 9 Dec '09  
    class library or console project can not hold the thread for TaskbarNotifier..
    a messbox can:

    /* codes: */

    notifier.Show("title","content",500,3000,500);
    System.Windows.Forms.MessageBox.Show("anything");

    /* end of codes */

    you will see the notifier works.

    However, we cannot use MessageBox to hold the thread for notifier anyway...

    It's headachy..
    Generalproperty no found "Skin.bmp" VC# Express
    Daniel Garzon
    13:52 7 Oct '09  
    Hi,i have a problem which is: "Resource 'skin3.bmp' cannot be found in class 'Form1.Front'", i already put the images on the local folder, even in the bin folder plus i changed the property build action to a "embedded resource". any ideas??? thkx lots
    QuestionProblem with Win7
    bzyy11
    0:26 15 Sep '09  
    The background is black , not transparent . but it is ok in win2008 r2. I think it is win7 aero cause it.
    AnswerRe: Problem with Win7
    bzyy11
    2:20 15 Sep '09  
    I solved this problem . In the ontimer function , replace with follow:
    case TaskbarStates.appearing:
    if (Height < BackgroundBitmap.Height)
    {
    Top -= nIncrementShow;
    Height += nIncrementShow;
    //SetBounds(Left, Top - nIncrementShow, Width, Height + nIncrementShow, BoundsSpecified.All);
    }

                            if (Top < WorkAreaRectangle.Bottom)
    {
    Top += nIncrementHide;
    Height -= nIncrementHide;
    //SetBounds(Left, Top + nIncrementHide, Width, Height - nIncrementHide);
    }

    Perhaps SetBounds dos not clear the old image in win7.
    GeneralRe: Problem with Win7
    see_seA
    6:08 5 Oct '09  
    Well done!! Was looking at a way to solve this problem! Works nicely.
    GeneralRe: Problem with Win7
    Ewinz87
    11:27 24 Feb '10  
    Tried using your code after converting it however VB.Net version doesn't use WorkAreaRectangle.
    Any idea how to fix this for VB.Net? Here's the code used for VB.Net:

    Case TaskbarStates.Appearing

    If Height < Me.bmpBackground.Height Then
    SetBounds(Left, Top - Me.iIncrementShow, Width, Height + Me.iIncrementShow)
    Else
    Me.tmrAnimation.Stop()
    Height = Me.bmpBackground.Height
    Me.tmrAnimation.Interval = Me.iVisibleEvents
    Me.eWindowState = TaskbarStates.Visible
    Me.tmrAnimation.Start()
    End If

    GeneralRe: Problem with Win7
    Member 6984454
    20:28 27 Feb '10  
    There is a vb.net version already. Download is at the top...
    GeneralRe: Problem with Win7
    Ewinz87
    20:37 27 Feb '10  
    You miss-understood me.. I'm trying to add the above modification in c# to the vb.net version which isn't exactly the same.
    GeneralRe: Problem with Win7
    Syle
    0:17 1 Mar '10  
    One of the if statements are for appearing and one for disappearing.
    GeneralProblems with Vista?
    LegoMindstorms
    7:39 25 Aug '09  
    Hello,

    First, I would like to say thank you for this great control.

    Unfortunately, I have a problem with you control on Windows Vista. Some users of my program report that they don't get a popup. They all have Windows Vista, but others with Vista have a working popup.

    I cannot reproduce the problem, but have you any idea of what it could be and how to fix it?

    Regards,
    Thomas
    GeneralIs there a way to make the edges smooth
    Muhammad Ahmed
    7:16 10 Aug '09  
    Hello,
    Thanks for the great stuff first of all,
    I observed that edges of the bitmap based popups are little jagged, Is there a way to make them smooth ?

    Thanks,
    Ahmed Blush

    Ahmed

    GeneralLicense
    tknman0700
    12:13 26 Jul '09  
    What is the license with this class? Great work!


    Last Updated 31 Mar 2003 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010