gpt4 book ai didi

C#。 Excel 加载项。无法重新定位 float 自定义任务 Pane

转载 作者:行者123 更新时间:2023-11-30 17:16:51 25 4
gpt4 key购买 nike

当创建自定义任务 Pane ( _CustomTaskPane MSDN ) 并将其 DockPosition 设置为 float 时,我想指定 TopLeft出现窗口的属性。由于 Office COM API 不提供直接执行此操作的可能性,因此人们建议更改 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之后才做的。甚至订阅任务 Pane 的 VisibleStateChange 以更加确定。但是,我收到了 HRESULT E_FAILED 的 COMException。

问题是我可以在调试时读取这些属性(顶部和左侧),但是设置它们会抛出异常。

看起来这个问题至少在互联网上出现过几次:

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

2) [http://aritrasaha.wordpress.com/2009/05/19/programatically-position-office-2007-floating-custom-task-pane/]

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

解决方法是使用 Windows API。但是,任何人都可以解释使用 CommandBar 方法有什么问题吗?也许我可以“重新配置”此 Top/Left-setters 无一异常(exception)地工作。

最佳答案

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

    /// <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;
}

请随意使用它...:-)

问候,约尔格

关于C#。 Excel 加载项。无法重新定位 float 自定义任务 Pane ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6916402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com