Programatically position Office 2007 Floating Custom Task Pane

SAME OUTSIDE OF GFW:http://aritrasaha.wordpress.com/2009/05/19/programatically-position-office-2007-floating-custom-task-pane/

 

Programatically repositioning the Custom Task Pane (CTP) created using Word / Excel 2007 is possible, when the DockPosition of the CTP is set as

MsoCTPDockPosition.msoCTPDockPositionFloating.

Since the Microsoft.Office.Tools.CustomTaskPane object does not have any top or left properties (it only provides the height and width) - I tried setting the top and left of the Task Pane Command Bar objects of the CTP, since I had earlier successfully used the Command Bar objects to retrieve the top and left position of the CTP.

if (this.CommandBars["My Task Pane"].Position == Microsoft.Office.Core.MsoBarPosition.msoBarFloating){this.CommandBars["My Task Pane"].Top = 0;    this.CommandBars["My Task Pane"].Left = 0;}
While setting the top and left property of the task pane, I got the exception :-

{"Error HRESULT E_FAIL has been returned from a call to a COM component."}

The following method using the Windows API function MoveWindow worked fine in moving the floating task pane to the desired position.
 
 

public partial class NativeMethods

{

[System.Runtime.InteropServices.DllImportAttribute("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);

 

 

[System.Runtime.InteropServices.DllImportAttribute("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);

}

  // Invoking the Win API Methods from the Word AddIn

IntPtr ptr = NativeMethods.FindWindowW("MsoCommandBar","My Task Pane");              NativeMethods.MoveWindow(ptr, 400, 220, 240, 360, true);

 

 

 
 

转载于:https://www.cnblogs.com/binsys/articles/2418513.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值