我正在尝试在键盘序列上为Visio中当前选定的形状设置PinX和PinY值,例如[CTRL] + [G]。此尝试的目的是以编程方式将形状基于当前所选形状的引脚坐标放到Visio绘图上。我正在使用C#和Microsoft.Office.Interop.Visio API来执行此操作。我使用的是.NET 4.0(mscorlib.dll是版本4.0.30319.1)。
到目前为止,我有这段代码:
Application myApp; // the reference to the Visio Application instance, which is passed into this class via constructor
Shape currShape; // a global variable for this class
//... down to the method in question
void app_KeyUp(int KeyCode, int KeyButtonState, ref bool CancelDefault)
{
currShape = myApp.ActiveWindow.Selection[0];
String xCoord = currShape.get_Cells("PinX").Formula;
String yCoord = currShape.get_Cells("PinY").Formula;
//handle keyboard events here
//...
}
此代码导致COMException;在调查之后,事实证明即使myApp.ActiveWindow.Selection有一个元素[0](如果只选择了一个形状,它是唯一的元素),我无法将该元素实际存储到currShape中。我不知道为什么会这样。奇怪的是,COMException不会导致程序停止。尝试分配给currShape时程序退出该方法,但执行仍在继续。
我尝试用另一种方法获取当前形状;这引发了同样的COMException,除了这次我能看到它,因为这个异常停止了执行,不像以前那样。
此代码:
public void test()
{
currShape = myApp.ActiveWindow.Selection[0];
String x = currShape.Shapes[1].get_Cells("PinX").Formula;
currShape.Shapes[1].get_Cells("PinX").FormulaForce = "5";
}
造成了这个例外:
System.Runtime.InteropServices.COMException was unhandled
Message="\n\nInvalid selection identifier."
Source="Microsoft Visio"
ErrorCode=-2032465753
StackTrace:
at Microsoft.Office.Interop.Visio.SelectionClass.get_Item(Int32 Index)
at WindowsFormsApplication4.Handler.test() in C:\Users\pvs5x\Documents\Visual Studio 2008\Projects\ACCESS(1)\Handler.cs:line 91
at WindowsFormsApplication4.Form3.changeColorToolStripMenuItem_Click(Object sender, EventArgs e) in C:\Users\pvs5x\Documents\Visual Studio 2008\Projects\ACCESS(1)\OpenSafetyCase.cs:line 355
at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ToolStrip.WndProc(Message& m)
at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at WindowsFormsApplication4.Program.Main() in C:\Users\pvs5x\Documents\Visual Studio 2008\Projects\ACCESS(1)\Program.cs:line 20
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
我不知道“无效的选择标识符”是什么意思,而Google搜索仅产生this主题,这是针对Visual Basic而不是C#。
我的两个问题是:
(1)这里出了什么问题?
(2)访问当前所选形状进行此类操作的正确方法是什么?
感谢您的帮助。
答案 0 :(得分:0)
我在过去选择Visio中的形状时遇到了麻烦。我如何解决它是通过创建一个Selection对象,然后使用Select方法。我不确定这对你有帮助。
另外,您可以在VisGuy.com交叉发帖。这就是Visio可编程专家的所在。
答案 1 :(得分:0)
我想知道这个问题是否与Selection集合的索引有关。
尝试一下:
currShape = myApp.ActiveWindow.Selection.Cast<Shape>().FirstOrDefault()
不要忘记通过using System.Linq;
添加对Linq库的引用。
答案 2 :(得分:0)
事实证明我需要访问[1]而不是[0],例如
currShape = myApp.ActiveWindow.Selection[1];
因为Visio从1开始编号对象。尽管myApp.ActiveWindow.Selection上的Visual Studio中的Watch在[0]上只有一个对象,但我必须使用[1]访问它。