C#。 Excel Addin。 无法重新定位浮动自定义任务窗格

创build自定义任务窗格( _CustomTaskPane MSDN )并将其DockPosition设置为浮动时,我想要指定出现的窗口的顶部左侧属性。 由于Office COM API不提供直接执行此操作的可能性,因此人们build议更改CommandBar相应属性的值:

var application = (_Application)_nativeTaskPane.Application; application.CommandBars["Task Pane Title"].Top = top; application.CommandBars["Task Pane Title"].Left = left; 

在上面的代码中,我假设

1)_nativeTaskPane是我的实例_CustomTaskPane(实际上它是Microsoft.Office.Core.CustomTaskPane)

2)_Application是Microsoft.Office.Interop.Excel._Application

当然,在设置Visible = true之后,我正在做这件事。 即使订阅了任务窗格的VisibleStateChange也更加确定。 不过,我得到一个与HRESULT E_FAILED的COMException。

事情是我可以在debugging时读取这些属性(Top&Left),但是设置它们会引发exception。

看起来问题至less在互联网上popup几次:

1) http://www.add-in-express.com/forum/read.php?FID=1&TID=5595

2)[/ url]

3)[http://www.visualstudiodev.com/visual-studio-tools-for-office/need-location-of-custom-task-pane-45822.shtml]

解决方法是使用Windows API。 但是,任何人都可以解释使用CommandBar方法可能是错误的? 也许我可以“重新configuration”这个Top / Left-setters工作,没有例外。

这是我在我的程序中使用的解决scheme:

  /// <summary> /// Set a custom panes position in the undocked state. /// </summary> /// <param name="customTaskPane">The custom task pane.</param> /// <param name="x">The new X position.</param> /// <param name="y">The new Y position.</param> private void SetCustomPanePositionWhenFloating(CustomTaskPane customTaskPane, int x, int y) { var oldDockPosition = customTaskPane.DockPosition; var oldVisibleState = customTaskPane.Visible; customTaskPane.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionFloating; customTaskPane.Visible = true; //The task pane must be visible to set its position var window = FindWindowW("MsoCommandBar", customTaskPane.Title); //MLHIDE if (window == null) return; WinApi.MoveWindow(window, x, y, customTaskPane.Width, customTaskPane.Height, true); customTaskPane.Visible = oldVisibleState; customTaskPane.DockPosition = oldDockPosition; } [DllImport("user32.dll", EntryPoint = "FindWindowW")] public static extern System.IntPtr FindWindowW([System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string lpClassName, [System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string lpWindowName); [DllImport("user32.dll", EntryPoint = "MoveWindow")] [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)] public static extern bool MoveWindow([System.Runtime.InteropServices.InAttribute()] System.IntPtr hWnd, int X, int Y, int nWidth, int nHeight, [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)] bool bRepaint); /// <summary> /// Set a custom panes size in the undocked state. /// </summary> /// <param name="customTaskPane">The custom task pane.</param> /// <param name="width">The new width.</param> /// <param name="height">The new height.</param> private void SetCustomPaneSizeWhenFloating(CustomTaskPane customTaskPane, int width, int height) { var oldDockPosition = customTaskPane.DockPosition; customTaskPane.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionFloating; customTaskPane.Width = width; customTaskPane.Height = height; customTaskPane.DockPosition = oldDockPosition; } 

随意使用它… :-)

问候,Jörg

它应该工作,有一个有趣的评论由MVP辛迪·梅斯特关于这个错误在这里(她testing,它的作品在回答这个论坛问题之前) – http://social.msdn.microsoft.com/Forums/vstudio/en-US/ 2df0e430-4d93-416e-89a0-56f8ad5dc988 /塞汀位对的一浮动-custome任务窗格?的教授=所需

在这里,她说使用错误的variables来获取应用程序对象会导致错误,即:

 Globals.MyAddIn.Application -> this will ultimately cause an exception Globals.ThisAddin.Application -> this will ultimately work 

我们假定两个都返回相同的Application对象。

如果你认为这是离奇的,那么你就是一个好公司。

我为这个问题添加了一个注释,问为什么用于访问Application对象的variables的名称有什么不同 – 当然,它是使用相同的Application对象。

我怀疑这是一些可怕的内部的,像其他内部强加的reflection一样的限制。 但是,无辜的开发人员不受保护,从这种非常奇怪的情况。

我认为,一旦自定义窗格设置为floating您不能通过定义更改其顶部/左侧属性。 你想要达到什么目的? 你想把窗格放在特定的位置? 如果是,则在将visible属性设置为true之前执行此操作

您应该引用的命令栏是“任务窗格”,它是CommandBars集合中的标准CommandBar。 您得到了HRESULT消息,因为在“CommandBars”集合中找不到“任务窗格标题”CommandBar。