AnnMenu example for C++ Builder

//Global declarations
LEADRasterAnnotation * pRasterAnn= NULL;
LEADRasterAnnToolBar* pRasterAnnToolbar= NULL;

void __fastcall TForm1::Button1Click(TObject *Sender)
{
   CoCreateInstance(CLSID_LEADRasterAnnotation, NULL, CLSCTX_ALL, IID_ILEADRasterAnnotation, (void**)&pRasterAnn);
   CoCreateInstance(CLSID_LEADRasterAnnToolBar, NULL, CLSCTX_ALL, IID_ILEADRasterAnnToolBar, (void**)&pRasterAnnToolbar);
   pRasterAnn->AnnParentRasterView = LEADRasterView1->Raster;
   LEADEventSink1->Connect (pRasterAnn, DIID__LEADRasterAnnotationEvents);
   pRasterAnn->AnnUserMode = ANN_USERMODE_DESIGN ;
}
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
   if (pRasterAnn)
      pRasterAnn->Release ();
   if (pRasterAnnToolbar)
      pRasterAnnToolbar-> Release();
}

void __fastcall TForm1:: LEADEventSink1Invoke(TObject *Sender, int DispID,
      const TGUID &IID, int LocaleID, WORD Flags, tagDISPPARAMS &Params,
      Pointer varResult, Pointer ExcepInfo, Pointer ArgErr)
{
   switch (DispID)
   {
        case LEADRASTERANNOTATIONEVENTS_ANNMENU:
      {

         int hMenu = (OleVariant)(Params.rgvarg[0]);
         AnsiString szout;
         int nID;
         LEADRasterAnnMenu* pRasAnnMenu= NULL;
         ILEADRasterAnnMenu *pPopupMenuItem = NULL;
         short x;

         HRESULT hr = CoCreateInstance(CLSID_LEADRasterAnnMenu,
NULL,
                                       CLSCTX_ALL,
                                       IID_ILEADRasterAnnMenu,
                                       (void**)&pRasAnnMenu
                                       );

         if (SUCCEEDED(hr) && (pRasAnnMenu != NULL) )
         {

            pRasAnnMenu->SetAnnMenu (hMenu);
            pRasAnnMenu->EnableMethodErrors = false;
            szout = "There are " + IntToStr(pRasAnnMenu->Count) + " Items";
            ShowMessage (szout);
            szout = "Original Text for item#2: ";
            szout = szout + pRasAnnMenu->get_ItemString(1);
            ShowMessage (szout);
            //set new string
            pRasAnnMenu->set_ItemString (1, AnsiToOLESTR("TestText"));
            szout = "Custom Text for item#2: ";
            szout = szout + pRasAnnMenu->get_ItemString(1);
            ShowMessage (szout);

            //make 3rd and 4th menu items have the same command id
            nID = pRasAnnMenu->get_ItemID (3);
            pRasAnnMenu->set_ItemID(2, nID);

            //check all unchecked menu items that are not popup submenus
            //disable all enabled menu items that are not popup submenus
            for (x = 0; x <= pRasAnnMenu->Count; x ++)
            {
               if (pRasAnnMenu->get_ItemType (x) == ANN_MENUITEMTYPE_NORMAL)
               {
                  if (pRasAnnMenu->get_ItemChecked (x))
                     pRasAnnMenu->set_ItemChecked(x, true);
                  if (pRasAnnMenu->get_ItemEnabled (x))
                     pRasAnnMenu->set_ItemEnabled(x, false);
               }
            }

            //insert 2 user-defined menu items
            pRasAnnMenu->AddItem (AnsiToOLESTR("Test&1"), ANN_MENUITEMTYPE_NORMAL, ANN_EVENT_MENUFIRST + 0, (short)(pRasAnnMenu->Count - 1));
            pRasAnnMenu->AddItem (AnsiToOLESTR("Test&2"), ANN_MENUITEMTYPE_NORMAL, ANN_EVENT_MENUFIRST + 1, (short)(pRasAnnMenu->Count - 1));

            //delete the last menu item
            pRasAnnMenu->DeleteItem ((short)(pRasAnnMenu->Count - 1));

            //delete the first menu item
            pRasAnnMenu->DeleteItem (0);

            //insert a popup menu
            pRasAnnMenu->AddItem (AnsiToOLESTR("Popup"), ANN_MENUITEMTYPE_SUBMENU, 0, pRasAnnMenu->Count);

            //get the new item, and add some items to it
            pRasAnnMenu->GetSubMenu ((short)(pRasAnnMenu->Count - 1), &pPopupMenuItem);

            pPopupMenuItem->AddItem (AnsiToOLESTR("Popup&1"), ANN_MENUITEMTYPE_NORMAL, ANN_EVENT_MENUFIRST + 2, pPopupMenuItem->Count);
            pPopupMenuItem->AddItem (AnsiToOLESTR("Popup&2"), ANN_MENUITEMTYPE_NORMAL, ANN_EVENT_MENUFIRST + 3, pPopupMenuItem->Count);

            pRasAnnMenu->Release();
         }
      }
      break;

      case   LEADRASTERANNOTATIONEVENTS_ANNUSERMENU:
      {
         int nID = (OleVariant)(Params.rgvarg[0]);
         if (nID == ANN_EVENT_MENUFIRST)
             pRasterAnn->AnnFlip (false, 0, true);
         else
             ShowMessage ("nID = ANN_EVENT_MENUFIRST + " + IntToStr(nID - ANN_EVENT_MENUFIRST));
      }
      break;
   }
}