Add Buttons to Task Pane (C#)
This example shows how to add standard SOLIDWORKS and custom buttons to the
Task Pane.
//----------------------------------------------------------------
// Preconditions:
// 1. SOLIDWORKS is running.
// 2. Copy the custom button images, localHelp.bmp and save.png, from
// install_dir\samples\tutorial\api
to this macro's folder.
// 3. Clear the Stop VSTA debugger on macro exit checkbox if selected
// in Tools > Options in SOLIDWORKS.
//
// Postconditions:
// 1. The Task Pane, with Microsoft Calendar control loaded, opens
// with the specified SOLIDWORKS standard and custom buttons
at the
// top of the pane.
// 2. Click each button from left to right. A message box is displayed
// after each button click. The message box might appear on the
// taskbar, so examine the taskbar and click the message box to open
// it. Additionall,y clicking the Close button prompts you to remove
// the control from the Task Pane.
// 3. Click the Stop Debugging button in the IDE.
// 4. Select the Stop VSTA debugger on macro exit checkbox in
// Tools > Options in
SOLIDWORKS, if you want to turn it back on.
//----------------------------------------------------------------
using
SolidWorks.Interop.sldworks;
using
SolidWorks.Interop.swconst;
using System;
using System.Collections;
using System.Windows.Forms;
using System.Diagnostics;
namespace
TaskPaneViewButtonsCSharp.csproj
{
partial
class
SolidWorksMacro
{
public TaskpaneView
swTaskPane;
public
int buttonIdx;
public
void Main()
{
bool result =
false;
string folder =
null;
string bitmap =
null;
string toolTip
= null;
string ctrlName
= null;
string
ctrlLicKey = null;
folder = swApp.GetCurrentMacroPathFolder() +
"\\";
// Use default image for Task Pane tab
bitmap = "";
toolTip = "Microsoft Calendar";
ctrlName = "MSCAL.Calendar";
ctrlLicKey = "";
swTaskPane = (TaskpaneView)swApp.CreateTaskpaneView2(bitmap,
toolTip);
// Add standard and custom buttons to
Task Pane
result = swTaskPane.AddCustomButton(folder
+ "localHelp.bmp",
"Help (custom bmp)");
result = swTaskPane.AddCustomButton(folder +
"save.png",
"Save (custom png)");
result = swTaskPane.AddStandardButton((int)swTaskPaneBitmapsOptions_e.swTaskPaneBitmapsOptions_Next,
"Next (standard)");
result = swTaskPane.AddStandardButton((int)swTaskPaneBitmapsOptions_e.swTaskPaneBitmapsOptions_Back,
"Back (standard)");
result = swTaskPane.AddStandardButton((int)swTaskPaneBitmapsOptions_e.swTaskPaneBitmapsOptions_Ok,
"OK (standard)");
result = swTaskPane.AddStandardButton((int)swTaskPaneBitmapsOptions_e.swTaskPaneBitmapsOptions_Close,
"Close (standard)");
// Add control to Task Pane for the
buttons
swTaskPane.AddControl(ctrlName,
ctrlLicKey);
// Set up events
AttachEventHandlers();
}
public
void AttachEventHandlers()
{
AttachSWEvents();
}
public
void AttachSWEvents()
{
swTaskPane.TaskPaneActivateNotify += this.swTaskPane_TaskPaneActivateNotify;
swTaskPane.TaskPaneDestroyNotify += this.swTaskPane_TaskPaneDestroyNotify;
swTaskPane.TaskPaneToolbarButtonClicked +=
this.swTaskPane_TaskPaneToolbarButtonClicked;
}
public
int swTaskPane_TaskPaneActivateNotify()
{
if (swTaskPane.GetButtonState(0)
== false)
{
for (buttonIdx
= 0; buttonIdx <= 20; buttonIdx++)
{
swTaskPane.SetButtonState(buttonIdx,
true);
}
}
else
{
for (buttonIdx
= 0; buttonIdx <= 20; buttonIdx++)
{
swTaskPane.SetButtonState(buttonIdx,
false);
}
}
return 0;
}
public
int swTaskPane_TaskPaneDestroyNotify()
{
MessageBox.Show("Remove control from
Task Pane?");
return 1;
}
public
int swTaskPane_TaskPaneToolbarButtonClicked(int
ButtonIndex)
{
switch ((ButtonIndex
+ 1))
{
case 1:
MessageBox.Show("Help (custom
bmp) button clicked.");
break;
case 2:
MessageBox.Show("Save (custom
png) button clicked.");
break;
case 3:
MessageBox.Show("Next button
clicked.");
break;
case 4:
MessageBox.Show("Back button
clicked.");
break;
case 5:
MessageBox.Show("Okay button
clicked.");
break;
case 6:
MessageBox.Show("Close button
clicked.");
swTaskPane.DeleteView();
break;
}
return 1;
}
///
<summary>
///
The SldWorks swApp variable is pre-assigned for you.
///
</summary>
public
SldWorks swApp;
}
}