J
Jean Dophin
I am using vs2003 and visio 2003 sdk. I created a new project from the
wizard. Then I copied and pasted the master drop code sample to the
generated code. Anyway, it compiled fine, but it does not show anything on
the screen. Even when I disabled all the macros. Even the message box does
not show. I don't know why, any suggestion? see the code below
==================================
namespace VisioProject1 {
using System;
using System.Runtime.InteropServices;
using Extensibility;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Visio;
/// <summary>This class implements the IDTExtensibility2 interface in order
/// to connect to Microsoft Visio as a COM add-in.</summary>
[GuidAttribute("7ac11288-f428-4a12-9e8c-8bf3f2f6fa45"),
ProgId("VisioProject1.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2 {
private Microsoft.Office.Interop.Visio.Application vsoApplication;
private object addInInstance;
/// <summary>This constructor intentionally left blank.</summary>
public Connect() {
// No initialization required.
}
/// <summary>This method implements the OnAddInsUpdate method of the
/// IDTExtensibility2 interface. It receives notification that the
/// collection of add-ins has changed.</summary>
/// <param name="custom">Array of parameters that are host application
/// specific</param>
public void OnAddInsUpdate(ref System.Array custom) {
}
/// <summary>This method implements the OnBeginShutdown method of the
/// IDTExtensibility2 interface. It receives notification that the host
/// application is being unloaded.</summary>
/// <param name="custom">Array of parameters that are host application
/// specific</param>
public void OnBeginShutdown(ref System.Array custom) {
}
/// <summary>This method implements the OnConnection method of the
/// IDTExtensibility2 interface. It receives notification that the
/// add-in is being loaded.</summary>
/// <param name="application">Root object of the host application
/// </param>
/// <param name="connectMode">Describes how the add-in is being loaded
/// </param>
/// <param name="addInInst">Object representing this add-in</param>
/// <param name="custom">Array of parameters that are host application
/// specific.</param>
public void OnConnection(
object application,
Extensibility.ext_ConnectMode connectMode,
object addInInst,
ref System.Array custom) {
vsoApplication = (Microsoft.Office.Interop.Visio.Application)
application;
addInInstance = addInInst;
System.Windows.Forms.MessageBox.Show("VisioProject1 connected.",
"VisioProject1 add-in");
}
/// <summary>This procedure is called when the add-in is about to be
/// unloaded. The add-in will be unloaded when Microsoft Visio is
/// shutting down or when a user has removed or deselected the add-in
/// from the "COM Add-Ins" dialog.</summary>
/// <param name="removeMode">Indicator telling the add-in why Visio is
/// disconnecting </param>
/// <param name="custom">Array of additional parameters for the add-in,
/// not used in this case</param>
public void OnDisconnection(
Extensibility.ext_DisconnectMode disconnectMode,
ref System.Array custom) {
}
/// <summary>This method implements the OnStartupComplete method of the
/// IDTExtensibility2 interface. It receives notification that the host
/// application has completed loading.</summary>
/// <param name="custom">Array of parameters that are host application
/// specific</param>
public void OnStartupComplete(ref System.Array custom)
{
System.Windows.Forms.MessageBox.Show("My message");
DropMaster myClass = new DropMaster();
myClass.DropMasterOnPage(vsoApplication.ActivePage, "Triangle", "Basic
Shapes", 2, 2, Microsoft.Office.Interop.Visio.VisUnitCodes.visInches);
}
public class DropMaster
{
/// <summary>This constructor is intentionally left blank.</summary>
public DropMaster()
{
// No initialization is required.
}
/// <summary>This method looks for the document with the name specified
/// in stencilName in the Documents collection and, if the document is
/// not in the collection, opens it as a docked stencil. It then gets
/// the master, using its universal name masterNameU, and drops it on
/// the specified page.</summary>
/// <param name="pageToDrop">Page where the master will be dropped
/// </param>
/// <param name="masterNameU">Universal name of the master in the
/// stencil</param>
/// <param name="stencilName">Name of the stencil from which the master
/// is to be found</param>
/// <param name="pinX">X-coordinate of the pin in the specified units
/// </param>
/// <param name="pinY">Y-coordinate of the pin in the specified units
/// </param>
/// <param name="units">Units used by pinX and pinY</param>
/// <returns>Shape that was created by dropping the master on the page
/// indicated by the page parameter</returns>
public Microsoft.Office.Interop.Visio.Shape DropMasterOnPage(
Microsoft.Office.Interop.Visio.Page pageToDrop,
string masterNameU,
string stencilName,
int pinX,
int pinY,
object units)
{
Microsoft.Office.Interop.Visio.Application visioApplication;
Microsoft.Office.Interop.Visio.Documents visioDocuments;
Microsoft.Office.Interop.Visio.Document stencil;
Microsoft.Office.Interop.Visio.Master masterInStencil;
Microsoft.Office.Interop.Visio.Shape droppedShape = null;
Microsoft.Office.Interop.Visio.Cell cellPinX;
Microsoft.Office.Interop.Visio.Cell cellPinY;
double pinXInternal;
double pinYInternal;
visioApplication = (Microsoft.Office.Interop.Visio.Application)
pageToDrop.Application;
try
{
// Verify that all incoming string parameters are not of zero
// length, except for the ones that have default values as ""
// and the output parameters.
if (masterNameU.Length == 0)
{
throw new System.ArgumentNullException("nasterNameU",
"Zero length string input.");
}
if (stencilName.Length == 0)
{
throw new System.ArgumentNullException("stencilName",
"Zero length string input.");
}
// Find the stencil in the Documents collection by name.
visioDocuments = visioApplication.Documents;
try
{
stencil = visioDocuments[stencilName];
}
catch
{
// The stencil is not in the collection; open it as a
// docked stencil.
stencil = visioDocuments.OpenEx(stencilName,
(short)Microsoft.Office.Interop.Visio.
VisOpenSaveArgs.visOpenDocked);
}
// Get a master from the stencil by its universal name.
masterInStencil =
stencil.Masters.get_ItemU(masterNameU);
// Convert the PinX and PinY into internal units.
pinXInternal = visioApplication.ConvertResult(pinX, units,
Microsoft.Office.Interop.Visio.VisUnitCodes.visInches);
pinYInternal = visioApplication.ConvertResult(pinY, units,
Microsoft.Office.Interop.Visio.VisUnitCodes.visInches);
// Drop the master on the page that is passed in.
// Set the PinX and PinY using pinXInternal and
// pinYInternal respectively.
droppedShape = pageToDrop.Drop(
masterInStencil, pinXInternal, pinYInternal);
// Update the units for PinX and PinY of the shape.
cellPinX = droppedShape.get_CellsSRC(
(short)Microsoft.Office.Interop.Visio.
VisSectionIndices.visSectionObject,
(short)Microsoft.Office.Interop.Visio.
VisRowIndices.visRowXFormOut,
(short)Microsoft.Office.Interop.Visio.
VisCellIndices.visXFormPinX);
cellPinY = droppedShape.get_CellsSRC(
(short)Microsoft.Office.Interop.Visio.
VisSectionIndices.visSectionObject,
(short)Microsoft.Office.Interop.Visio.
VisRowIndices.visRowXFormOut,
(short)Microsoft.Office.Interop.Visio.
VisCellIndices.visXFormPinY);
cellPinX.set_Result(units, pinX);
cellPinY.set_Result(units, pinY);
}
catch (Exception err)
{
System.Diagnostics.Debug.WriteLine (err.Message);
}
return droppedShape;
}
}
}
}
wizard. Then I copied and pasted the master drop code sample to the
generated code. Anyway, it compiled fine, but it does not show anything on
the screen. Even when I disabled all the macros. Even the message box does
not show. I don't know why, any suggestion? see the code below
==================================
namespace VisioProject1 {
using System;
using System.Runtime.InteropServices;
using Extensibility;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Visio;
/// <summary>This class implements the IDTExtensibility2 interface in order
/// to connect to Microsoft Visio as a COM add-in.</summary>
[GuidAttribute("7ac11288-f428-4a12-9e8c-8bf3f2f6fa45"),
ProgId("VisioProject1.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2 {
private Microsoft.Office.Interop.Visio.Application vsoApplication;
private object addInInstance;
/// <summary>This constructor intentionally left blank.</summary>
public Connect() {
// No initialization required.
}
/// <summary>This method implements the OnAddInsUpdate method of the
/// IDTExtensibility2 interface. It receives notification that the
/// collection of add-ins has changed.</summary>
/// <param name="custom">Array of parameters that are host application
/// specific</param>
public void OnAddInsUpdate(ref System.Array custom) {
}
/// <summary>This method implements the OnBeginShutdown method of the
/// IDTExtensibility2 interface. It receives notification that the host
/// application is being unloaded.</summary>
/// <param name="custom">Array of parameters that are host application
/// specific</param>
public void OnBeginShutdown(ref System.Array custom) {
}
/// <summary>This method implements the OnConnection method of the
/// IDTExtensibility2 interface. It receives notification that the
/// add-in is being loaded.</summary>
/// <param name="application">Root object of the host application
/// </param>
/// <param name="connectMode">Describes how the add-in is being loaded
/// </param>
/// <param name="addInInst">Object representing this add-in</param>
/// <param name="custom">Array of parameters that are host application
/// specific.</param>
public void OnConnection(
object application,
Extensibility.ext_ConnectMode connectMode,
object addInInst,
ref System.Array custom) {
vsoApplication = (Microsoft.Office.Interop.Visio.Application)
application;
addInInstance = addInInst;
System.Windows.Forms.MessageBox.Show("VisioProject1 connected.",
"VisioProject1 add-in");
}
/// <summary>This procedure is called when the add-in is about to be
/// unloaded. The add-in will be unloaded when Microsoft Visio is
/// shutting down or when a user has removed or deselected the add-in
/// from the "COM Add-Ins" dialog.</summary>
/// <param name="removeMode">Indicator telling the add-in why Visio is
/// disconnecting </param>
/// <param name="custom">Array of additional parameters for the add-in,
/// not used in this case</param>
public void OnDisconnection(
Extensibility.ext_DisconnectMode disconnectMode,
ref System.Array custom) {
}
/// <summary>This method implements the OnStartupComplete method of the
/// IDTExtensibility2 interface. It receives notification that the host
/// application has completed loading.</summary>
/// <param name="custom">Array of parameters that are host application
/// specific</param>
public void OnStartupComplete(ref System.Array custom)
{
System.Windows.Forms.MessageBox.Show("My message");
DropMaster myClass = new DropMaster();
myClass.DropMasterOnPage(vsoApplication.ActivePage, "Triangle", "Basic
Shapes", 2, 2, Microsoft.Office.Interop.Visio.VisUnitCodes.visInches);
}
public class DropMaster
{
/// <summary>This constructor is intentionally left blank.</summary>
public DropMaster()
{
// No initialization is required.
}
/// <summary>This method looks for the document with the name specified
/// in stencilName in the Documents collection and, if the document is
/// not in the collection, opens it as a docked stencil. It then gets
/// the master, using its universal name masterNameU, and drops it on
/// the specified page.</summary>
/// <param name="pageToDrop">Page where the master will be dropped
/// </param>
/// <param name="masterNameU">Universal name of the master in the
/// stencil</param>
/// <param name="stencilName">Name of the stencil from which the master
/// is to be found</param>
/// <param name="pinX">X-coordinate of the pin in the specified units
/// </param>
/// <param name="pinY">Y-coordinate of the pin in the specified units
/// </param>
/// <param name="units">Units used by pinX and pinY</param>
/// <returns>Shape that was created by dropping the master on the page
/// indicated by the page parameter</returns>
public Microsoft.Office.Interop.Visio.Shape DropMasterOnPage(
Microsoft.Office.Interop.Visio.Page pageToDrop,
string masterNameU,
string stencilName,
int pinX,
int pinY,
object units)
{
Microsoft.Office.Interop.Visio.Application visioApplication;
Microsoft.Office.Interop.Visio.Documents visioDocuments;
Microsoft.Office.Interop.Visio.Document stencil;
Microsoft.Office.Interop.Visio.Master masterInStencil;
Microsoft.Office.Interop.Visio.Shape droppedShape = null;
Microsoft.Office.Interop.Visio.Cell cellPinX;
Microsoft.Office.Interop.Visio.Cell cellPinY;
double pinXInternal;
double pinYInternal;
visioApplication = (Microsoft.Office.Interop.Visio.Application)
pageToDrop.Application;
try
{
// Verify that all incoming string parameters are not of zero
// length, except for the ones that have default values as ""
// and the output parameters.
if (masterNameU.Length == 0)
{
throw new System.ArgumentNullException("nasterNameU",
"Zero length string input.");
}
if (stencilName.Length == 0)
{
throw new System.ArgumentNullException("stencilName",
"Zero length string input.");
}
// Find the stencil in the Documents collection by name.
visioDocuments = visioApplication.Documents;
try
{
stencil = visioDocuments[stencilName];
}
catch
{
// The stencil is not in the collection; open it as a
// docked stencil.
stencil = visioDocuments.OpenEx(stencilName,
(short)Microsoft.Office.Interop.Visio.
VisOpenSaveArgs.visOpenDocked);
}
// Get a master from the stencil by its universal name.
masterInStencil =
stencil.Masters.get_ItemU(masterNameU);
// Convert the PinX and PinY into internal units.
pinXInternal = visioApplication.ConvertResult(pinX, units,
Microsoft.Office.Interop.Visio.VisUnitCodes.visInches);
pinYInternal = visioApplication.ConvertResult(pinY, units,
Microsoft.Office.Interop.Visio.VisUnitCodes.visInches);
// Drop the master on the page that is passed in.
// Set the PinX and PinY using pinXInternal and
// pinYInternal respectively.
droppedShape = pageToDrop.Drop(
masterInStencil, pinXInternal, pinYInternal);
// Update the units for PinX and PinY of the shape.
cellPinX = droppedShape.get_CellsSRC(
(short)Microsoft.Office.Interop.Visio.
VisSectionIndices.visSectionObject,
(short)Microsoft.Office.Interop.Visio.
VisRowIndices.visRowXFormOut,
(short)Microsoft.Office.Interop.Visio.
VisCellIndices.visXFormPinX);
cellPinY = droppedShape.get_CellsSRC(
(short)Microsoft.Office.Interop.Visio.
VisSectionIndices.visSectionObject,
(short)Microsoft.Office.Interop.Visio.
VisRowIndices.visRowXFormOut,
(short)Microsoft.Office.Interop.Visio.
VisCellIndices.visXFormPinY);
cellPinX.set_Result(units, pinX);
cellPinY.set_Result(units, pinY);
}
catch (Exception err)
{
System.Diagnostics.Debug.WriteLine (err.Message);
}
return droppedShape;
}
}
}
}