Skip to main content
Email Password   helpLost your password?
  • Download source files - 31 Kb
  • The problem

    Once I had written a control panel applet to control my NT Service I though that it might be handy to be able to control the service from an icon in the task tray. Since the result of clicking an icon in the task tray is often similar to running up a control panel applet I hoped to be able to reuse much of my code. The end result was a framework for task bar applets and a derived class that managed control panel applets. I then just let the task bar applet load my control panel applet and provided access to it from the task tray with no code changes required. The resulting classes can load any control panel applet into the task tray, just supply the file name and applet index...

    What's so special about a task tray applet anyway?

    To put an icon in the task tray all an application has to do is call Shell_NotifyIcon() to supply the icon, tool tip, callback message and a window to send it to. Making an application that's just a task tray applet is just a case of creating a hidden window, calling Shell_NotifyIcon() and processing the messages. The messages are your standard mouse click type messages. It's not that difficult but it takes a bit of time.

    What does CJBTaskTrayApplet give you?

    Creating a hidden window with a WndProc that responds to Task Tray applet messages correctly and does all the right things can be cumbersome if all you want to do is pop up a dialog box app from the task tray. The framework lets you derive from an base class that handles all of that stuff for you. All you need to do is override a few virtual functions and you have a dialog popping up! CJBTaskTrayApplet also gives you multiple applets in one exe (with multiple separate icons in the task tray) and automatic right button menu handling.

    Your derived class

    To implement your task tray applet you need to do the following:

    If you wish you can also:

    When I implemented CJBControlPanelApplet I placed multiple applets in a single DLL. After writing much of the same "base class has a static list" code again for the task tray applet I decided to try and create a common base class that incorporated and hid all of the "I'm a class whos instances are in a static list" functionality. It seemed simple enough until I realised that to be type-safe and remove heaps of casts in the derived class the base class needed to know about the derived class. Without a second thought I templatised the base class and had the derived class inherit from it thus:

    class CTaskBarApplet : public CLinkedClass<CTaskBarApplet>

    Then I thought about it and it seemed like a really odd thing to do. It worked great and gave no warnings but was it nice? Since then I've spoken to several people and read Stroustrup 3e (and he thinks it's a "Good Thing") and now I like the idea. It did seem very odd at first though...

    Once I had a standard way to link instances of a class together I needed a way to navigate this list at run-time. After several aborted attempts I ended up with an STL-style iterator. This lets you do things like:

    for (Iterator addIt = Begin(); addIt != End(); addIt++)
    {
       addIt->AddIcon();   // call any derived class method here
    
    }

    Of course, having moved the common functionality out into a base class I never got around to using the base class in CJBControlPanelApplet, but I've used it in plenty of other places.

    The CPlTrayApplet and CPlAppletClient classes

    By this stage I had a task tray framework but I still needed to somehow reuse the code in my control panel applet. Since a .cpl is just a DLL, and since I'd worked out how CPlApplets worked when I wrote CJBControlPanelApplet I decided to write a wrapper class that could load a control panel applet, send it all the right messages to initialise it and then run the applet. Once I had CJBCPlAppletClient I derived a CJBCPlTrayApplet from CJBTaskTrayApplet and wired the two together. The end result was a collaboration something like this:

    CJBTaskBarApplet.gif (4158 bytes)

    CJBCPlAppletClient isn't linked in any way to the task tray classes. It's just used by CJBCPlTrayApplet . It can be used anywhere you want to access a control panel applet programmatically.

    Using MFC with the framework

    When I started looking at task tray applets I tried to write one with MFC. I've seen it done but you have to do a fair bit of work to get it to work right. Message map entries for the callback message, preventing the CWinApp's main window from popping up briefly, etc, etc. I didn't need MFC so I wrote the straight Win32 solution. Then I wanted a single complex dialog in a task tray applet and I wanted to use MFC for the dialog and my framework for the applet... It all went well until I ran it and the dialog died with assertion failures, I didn't have a CWinApp derived class and it was a serious requirement... Since I didn't have time to convert my applet framework to a CWinApp derived framework I cheated. Since all that is required is that a CWinApp object exists so that the dialog has a message pump to use I stuck a vanilla CWinApp object inside WinMain and called AfxWinInit() directly and everything worked fine... An #ifdef around this version of WinMain() means that at the flick of a #define you can have either MFC enabled or lean and mean versions of your task tray applets.

    I really keep meaning to sit down and make CJBTaskTrayApplet a real CWinApp derivative, but, this works...

    See article on Len's homepage for the latest updates.

    You must Sign In to use this message board.
     
     
    Per page   
     FirstPrevNext
    GeneralKilling explorer.exe Pin
    Daniel Desormeaux
    6:05 9 Dec '04  
    GeneralRe: Killing explorer.exe Pin
    KarstenK
    23:16 16 May '06  
    GeneralProblems in VC 7.1 Pin
    ThomT
    12:34 29 Jun '03  
    GeneralRe: Problems in VC 7.1 Pin
    Len Holgate
    13:50 29 Jun '03  
    GeneralRe: Problems in VC 7.1 Pin
    ThomT
    14:41 29 Jun '03  
    GeneralRe: Problems in VC 7.1 Pin
    Len Holgate
    21:39 29 Jun '03  
    GeneralRe: Problems in VC 7.1 Pin
    ThomT
    15:02 30 Jul '03  
    GeneralRe: Problems in VC 7.1 Pin
    Len Holgate
    22:34 30 Jul '03  
    GeneralRe: Problems in VC 7.1 Pin
    Len Holgate
    22:43 30 Jul '03  
    GeneralRe: Problems in VC 7.1 Pin
    ThomT
    5:17 31 Jul '03  
    GeneralRe: Problems in VC 7.1 Pin
    Len Holgate
    7:38 31 Jul '03  
    GeneralRe: Problems in VC 7.1 Pin
    Len Holgate
    9:20 31 Jul '03  
    GeneralRe: Problems in VC 7.1 Pin
    ThomT
    12:24 31 Jul '03  
    GeneralDiscriminate between Click and Double-Click Pin
    Brett Levine
    12:06 12 May '02  
    GeneralMenu Pin
    Anonymous
    7:22 2 May '02  
    GeneralTask Tray and Service Pin
    Anonymous
    5:49 5 Mar '02  
    GeneralRe: Task Tray and Service Pin
    Paul A. Howes
    6:14 5 Mar '02  
    GeneralRe: Task Tray and Service Pin
    david ewan
    10:18 3 Jun '03  
    GeneralRe: Task Tray and Service Pin
    Len Holgate
    10:44 3 Jun '03  
    GeneralRe: Task Tray and Service Pin
    david ewan
    10:55 3 Jun '03  
    GeneralRe: Task Tray and Service Pin
    Len Holgate
    11:01 3 Jun '03  
    GeneralRe: Task Tray and Service Pin
    vive_la_squeeze
    11:55 14 Sep '03  
    GeneralThe Pop Dialog is not activate Pin
    lixun
    17:43 9 Jan '02  
    GeneralRe: The Pop Dialog is not activate Pin
    Len Holgate
    21:57 9 Jan '02  
    GeneralMFC DLL + ATL + Shell Pin
    Kenu
    10:32 23 Mar '01  


    Last Updated 24 Feb 2000 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009