Home

Desktop Sidebar allows panel to display their own context menu when user click with right mouse button on panel and this menu is merged with standard Desktop Sidebar menu.

Let's add to previous example context menu with "Reset counter" command.

To accomplish this we have to implement two interfaces in our panel:

IPanelContextMenu has GetContextMenu method which is called by Desktop Sidebar when it displays context menu and panel can return his own menu

ICommandTarget interface includes methods which are called when user choose option from context menu

We will start by defining constant RESET_COUNTER in firstPanel class. It will be used to identify command inserted to menu

const int RESET_COUNTER=102;

Now we should add implementation of GetContextMenu

// IPanelContextMenu Members
public IntPtr GetContextMenu(tagPOINT pt)
{
   IControlFactory cf=sidebar.GetControlFactory();
   System.IntPtr menu=cf.CreatePopupMenu();
   cf.InsertMenuItem((int)menu,0,"Reset Counter",RESET_COUNTER);
   cf.InsertMenuSeparator((int)menu,1);
   return menu;
}

It's just create Popup Menu, add "Reset Counter" command and separator to it and return such menu to Desktop Sidebar

and final change is implementation OnPrivateCommand from ICommandTarget interface.

// ICommandTarget Members
public void OnPrivateCommand(int id)
{
   if (id==RESET_COUNTER)
   {
      counter=0;
      textOutput.SetText("Counter:"+counter);
   }
}

When identifier of command chosen by user is our RESET_COUNTER we reset counter and display updated value on sidebar

And that is it. It is simple, isn't it?

Complete source of modified panel

using System;
using DesktopSidebar;

namespace firstPlugin
{
   public class firstPanel: 
      IPanel, 
      IPanelWindow, 
      ITextOutputParent,
      IPanelContextMenu,
      ICommandTarget 
   {
      Sidebar sidebar;
      IPanelParent parent;
      int cookie;

      ITextOutput textOutput;

      IPanelConfig config;
      int counter;

      const int RESET_COUNTER=101;

      public firstPanel()
      {
      }

      // IPlugin Members
      public void Create(
         int hwndParent, 
         Sidebar Sidebar, 
         IPanelParent Parent, 
         IPanelConfig Config, 
         ICanvas canvas, 
         IXmlNode configRoot, 
         IXmlNode panelConfig, 
         IXmlNode settingsRoot, 
         IXmlNode panelSettings, 
         int Cookie)
      {
         sidebar=Sidebar;
         parent=Parent;
         config=Config;
         cookie=Cookie;

         parent.SetCaption(cookie,"First .NET Panel");

         textOutput=Sidebar.GetControlFactory().CreateTextOutput();
         textOutput.Init(Sidebar.GetGlobalSettings(),Sidebar.GetSkinManager(),this,true);
         textOutput.Create(hwndParent,true);

         config.InitSetting("myCounter",0);
         config.Load(panelSettings);

         counter=System.Convert.ToInt32(config.GetSetting("myCounter"));

         textOutput.SetText("Counter:"+counter);
      }

      public void Close()
      {
         textOutput.Close();
      }

      public bool Tick(bool minute)
      {
         return false;
      }

      public void Save(IXmlBuilder panelItem, IXmlBuilder settingsRoot)
      {
         config.SetSetting("myCounter",counter);
         config.Save(panelItem);
      }

      // IPanelWindow Members
      public int GetFitHeight(int width)
      {
         return textOutput.GetFitHeight();
      }

      public System.IntPtr GetHwnd()
      {
         return (System.IntPtr)textOutput.GetHwnd();
      }

      // ITextOutputParent Members
      public void OnDrawBackground(ITextOutput textOutput, IGraphics graphics)
      {
         parent.DrawControlBackground(graphics,cookie,textOutput.GetHwnd());
      }

      public void OnClick(ITextOutput textOutput, bool dbclk)
      {
         counter++;
         textOutput.SetText("Counter:"+counter);
      }

      public void OnMouseLeave(ITextOutput textOutput)
      {
      }

      public void OnShowDetails(ITextOutput textOutput)
      {
      }

      public void OnMouseHover(ITextOutput textOutput)
      {
      }

      // IPanelContextMenu Members
      public IntPtr GetContextMenu(tagPOINT pt)
      {
         IControlFactory cf=sidebar.GetControlFactory();
         System.IntPtr menu=cf.CreatePopupMenu();
         cf.InsertMenuItem((int)menu,0,"Reset Counter",RESET_COUNTER);
         cf.InsertMenuSeparator((int)menu,1);
         return menu;
      }

      // ICommandTarget Members
      public void OnPrivateCommand(int id)
      {
         if (id==RESET_COUNTER)
         {
            counter=0;
            textOutput.SetText("Counter:"+counter);
         }
      }

      public int GetPrivateCommandImage(int id)
      {
         return -1;
      }

      public void OnUpdatePrivateCommand(int id, IUpdateUI pAction)
      {
      }

      public void OnCommand(string cmdName)
      {
      }

      public void OnUpdateCommand(string cmdName, IUpdateUI pAction)
      {
      }
   }
}