The default O-Calc Pro Plugin template will create a message box showing the message “Hello World”. Below are some explanations of the various code components of this simple plugin example.
Defining the Plugin Type:
- /// <summary>
- /// Declare the type of plugin as one of:
- /// DOCKED_TAB
- /// MENU_ITEM
- /// BOTH_DOCKED_AND_MENU
- /// CLEARANCE_SAG_PROVIDER
- /// </summary>
- public PLUGIN_TYPE Type
- {
- get
- {
- return PLUGIN_TYPE.BOTH_DOCKED_AND_MENU;
- }
- }
There are four different plug-in types that can be defined. In the code snippet above, the plugin type as been defined as both a ‘Menu Item’ and a new ‘Docked Tab’.
Naming the Plugin:
- /// <summary>
- /// Declare the name of the plugin usd for synthesizing the registry keys ect
- /// </summary>
- public String Name
- {
- get
- {
- return "MyOCalcProPlugin";
- }
- }
This will be the name of the menu item as it appears in the O-Calc Pro interface. To change the name of the resultant compiled plugin file that gets built to the location C:\Program Files (x86)\Osmose\O-Calc Pro\bin\Plugins, you would need to change the name of the Visual Studio Project. Note: the name of the compiled plugin file is what will be displayed in the O-Calc Pro ‘Manage Plugins’ list.
Specifying the Docked Tab Parameters :
- /// <summary>
- /// Add a tabbed form to the tabbed window (if the plugin type is
- /// PLUGIN_TYPE.DOCKED_TAB
- /// or
- /// PLUGIN_TYPE.BOTH_DOCKED_AND_MENU
- /// </summary>
- /// <param name="pPPLMain"></param>
- public void AddForm(PPL_Lib.PPLMain pPPLMain)
- {
- . . .
- }
- PluginForm cForm = null;
- class PluginForm : WeifenLuo.WinFormsUI.Docking.DockContent
- {
- public PluginForm()
- {
- this.Name = "pluginForm";
- this.Text = "Plugin";
- System.Windows.Forms.Label helloLabel = new System.Windows.Forms.Label();
- helloLabel.AutoSize = true;
- helloLabel.Name = "helloLabel";
- helloLabel.Text = "Hello World";
- helloLabel.Location = new System.Drawing.Point(20, 20);
- helloLabel.Visible = true;
- Controls.Add(helloLabel);
- }
- public Guid cGuid;
- protected override string GetPersistString()
- {
- return cGuid.ToString();
- }
Here the developer can specify the various parameters associated with the docked tab, such as tab name, display text, and other features of the new docked tab that will appear within the O-Calc Pro interface.
Defining the location of the Menu Item:
- public void AddToMenu(PPL_Lib.PPLMain pPPLMain, System.Windows.Forms.ToolStrip pToolStrip)
- {
- //save the reference to the O-Calc Pro main
- cPPLMain = pPPLMain;
- //create the toolstrip button
- cToolStripMenuItemButton = new ToolStripMenuItem(Name);
- cToolStripMenuItemButton.AutoToolTip = true;
- cToolStripMenuItemButton.ToolTipText = Description;
- //find the dropdown menu we want to add the toolsrip button to
- int itemindex = 0;
- System.Diagnostics.Debug.Assert(pToolStrip.Items[itemindex] is ToolStripDropDownButton);
- if (pToolStrip.Items[itemindex] is ToolStripDropDownButton)
- {
- ToolStripDropDownButton tsb = pToolStrip.Items[itemindex] as ToolStripDropDownButton;
- System.Diagnostics.Debug.Assert(tsb.Text == "&File");
- tsb.DropDownItems.Insert(tsb.DropDownItems.Count - 1, cToolStripMenuItemButton);
- tsb.DropDownItems.Insert(tsb.DropDownItems.Count - 1, new ToolStripSeparator());
- //add an event handler when the dropdown menu is opened to allow us
- //to enable or disble the toolstrip button (optional)
- tsb.DropDownOpened += tsb_DropDownOpened;
- }
In the default code snippet above, the new menu item is placed at the bottom of the ‘File’ drop down menu. This menu item will be disabled until a pole has been loaded within the application.
- /// <summary>
- /// The menu containing our tool is being displayed. Optionally
- /// enable or disable the toolstrip button depending on
- /// our criteria
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void tsb_DropDownOpened(object sender, EventArgs e)
- {
- bool enabled = false;
- if (cPPLMain != null)
- {
- enabled = (cPPLMain.GetMainStructure() is PPLPole);
- }
- cToolStripMenuItemButton.Enabled = enabled;
- }
Perform the Plugin Operation
- /// <summary>
- /// Perform the plugin tool's operation
- /// </summary>
- private void DoPluginOperation()
- {
- try
- {
- PPLMessageBox.Show("Hello World!");
- }
- catch (Exception ex)
- {
- PPLMessageBox.Show(ex.Message, "Error in " + Name);
- }
- }
In this example, clicking on the plugin menu item found in the File dropdown, will display the “Hello World” message box.
Below is a video that demonstrates this ‘Hello World’ plugin:
Recent Comments