首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C#。无法重新定位浮动自定义任务窗格

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

Stack Overflow用户
提问于 2011-08-02 17:56:04
回答 4查看 2.9K关注 0票数 3

当创建自定义任务窗格(CustomTaskPane MSDN)并将其DockPosition设置为浮动时,我希望指定窗口的TopLeft属性。由于Office没有直接提供这样做的可能性,所以建议更改CommandBar相应属性值:

代码语言:javascript
运行
复制
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的E_FAILED。

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

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

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。但是,有人能解释使用CommandBar-方法有什么问题吗?也许我可以“重新配置”smth,让这个顶级/左设置者毫无例外地工作。

EN

回答 4

Stack Overflow用户

发布于 2013-09-06 08:31:24

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

代码语言:javascript
运行
复制
    /// <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;
    }

可以随意使用..。:-)

你好,约格

票数 2
EN

Stack Overflow用户

发布于 2013-06-26 03:48:31

它应该有效,而且MVP Cindy对这个错误有一个有趣的评论(她在回答这个论坛问题之前测试了它的工作能力)- http://social.msdn.microsoft.com/Forums/vstudio/en-US/2df0e430-4d93-416e-89a0-56f8ad5dc988/seting-position-of-a-floating-custome-task-pane?prof=required

在其中,她说,使用错误的变量获取应用程序对象会导致错误,即:

代码语言:javascript
运行
复制
Globals.MyAddIn.Application -> this will ultimately cause an exception   
Globals.ThisAddin.Application -> this will ultimately work

其中,我们假设两个都返回相同的Application对象。

如果你觉得这很奇怪那你就有个好伙伴了。

我在这个问题上添加了一个注释,该问题询问用于访问Application的变量的名称为什么会产生任何不同--当然,它使用的是同一个Application对象。

我怀疑这是一些可怕的内部,反射一样的限制强加给其他一些内部。但无辜的开发人员没有受到这种非常奇怪的情况的保护。

票数 1
EN

Stack Overflow用户

发布于 2011-08-04 11:09:08

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6916402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档