Initialize the menu

<< Click to Display Table of Contents >>

Navigation:  Create a PDF Editor > .NET (C#) Example >

Initialize the menu

The menu is initialized by just this line of code:

pdfViewer1.InitMainMenu(menuStrip1, doExecuteWPViewAction, fileToolStripMenuItem, infoToolStripMenuItem);

 

 

menuStrip1: The is is our main menu

doExecuteWPViewAction: This is the function which handles the click events

fileToolStripMenuItem: This is the caption item of the first menu strip. WPViewPDF will insert new file menu items at the start

infoToolStripMenuItem: This is the caption item of the last menu strip. WPViewPDF will append new info menu items at the end

 

dotnetstep2

Almost the complete menu was auto generated, only the item "Close" existed before.

 

 

The function InitMainMenu has been implemented this inside the pdfviewer class. It first request the count of action "kinds". Each action kind should create one menu caption with a drop down menu. Inside the dropdown menu there are the action "operations". Certain flags are used to hide a submenu or to make a submenu caption item for a deeper drop down menu.

 

public void InitMainMenu(System.Windows.Forms.MenuStrip menuStrip,

  System.EventHandler OnClick ,

  System.Windows.Forms.ToolStripMenuItem FileMenu ,

  System.Windows.Forms.ToolStripMenuItem InfoMenu      

  )

{

    int kmax = Command( commands.COMPDF_ACTION_READ, "kinds", 0);

    bool isPlus = (Command(commands.COMPDF_GetWPViewPDFPLUSFlag) != 0);

 

   

    for(int k = 0; k<kmax; k++)

    {

System.Windows.Forms.ToolStripMenuItem men;

System.Windows.Forms.ToolStripMenuItem asubmenu = null;

int mencount = 0;

bool reverse = true;

// -------------------------------------------------------------------------------

if ((k == 0) && (FileMenu != null))

    men = FileMenu;

else if ((k == kmax - 1) && (InfoMenu != null))

{

    men = InfoMenu;

    reverse = false;

}

else

{

    men = new System.Windows.Forms.ToolStripMenuItem();

    men.Text = CommandGetStr(commands.COMPDF_ACTION_READ, "kcaption", k);

}

// -------------------------------------------------------------------------------

int omax = Command(commands.COMPDF_ACTION_READ, "operation", k);

 

 

for(int o = omax-1; o>=0; o--)

{

  int ac = (k << 16) + o;

  string s = CommandGetStr(commands.COMPDF_ACTION_READ, "caption", ac);

  int flags =  Command(commands.COMPDF_ACTION_READFLAGS, ac );

    /*  flags:

  wpNeedSeperator,  // 1: add an seperator after the menu

  wpNoMenuItem,     // 2: Dont create menu items

  wpIsPlusAction,   // 4: Requires WPViewPDF PLUS

  wpRequireWriting,  // 8: Requires the PDF to be not protected

  wpGlobalOperation, // 16: This action doe not require PDF to be loaded

  wpMakeSubmenu      // 32: The following items until  wpNeedSeperator should be sub menus of this

    */

  if ((s!="") && ((flags & 32)!=0))

   {

asubmenu = new System.Windows.Forms.ToolStripMenuItem();  

asubmenu.Text = s;

men.DropDownItems.Add(asubmenu);

   }

  else

  if ((s!="") && (isPlus || ((flags & 4)==0))  && ((flags & 2)==0))

   {

System.Windows.Forms.ToolStripMenuItem submen;

submen = new System.Windows.Forms.ToolStripMenuItem();

submen.Text = s;

submen.ToolTipText =  CommandGetStr(commands.COMPDF_ACTION_READ, "hint", ac) ;

submen.Tag = ac;

submen.Click += new System.EventHandler(OnClick);

 

if (asubmenu != null)

    asubmenu.DropDownItems.Add(submen);

else

{

    if(reverse)men.DropDownItems.Insert(0, submen);

    else men.DropDownItems.Add(submen);

 

}

 

if ((flags & 1)!=0)

  asubmenu = null;

mencount++;

   }

}

if (mencount>0)

{

    menuStrip.Items.Add(men);

}

else men = null;

    }

}

 

 

InitMainMenu will create new menu items with the propery "Tag" set to an integer value. The integer can be used to execute the action in the viewer. (It consists of the action kind in the high word and the action "Woperation" in the low word).

The value of 0 is not used, 1 refers to "File open", 2 will be used for "File Append" - any other value should not be expected to be fixed.