The project illustrates How to get the location of the custom Task Pane when it is floating

Introduction

This samples demonstrates how to get the location of the custom Task Pane when it is floating. Some users have this requirement and there is no existing sample in MSDN.

Customer Evidence:

http://social.msdn.microsoft.com/forums/en-US/vsto/thread/ed72d66d-6159-49fd-a6e2-ad365f275bb3/

http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/946b1d90-c201-43d6-8972-d31261d23fba

http://social.msdn.microsoft.com/forums/en-us/vsto/thread/D4185407-F484-4756-966D-3B825A00C112

Building the Project

Open the project (VBNETWordAddIn.vbproj) in the Visual Studio 2013 and build it.

 

 

Visual Basic
Edit|Remove
Partial Public Class NativeMethods 
 
    ''' <summary> 
 
    ''' Retrieves a handle to the top-level window whose class name and window name match the specified strings 
 
    ''' </summary> 
 
    ''' <param name="lpClassName">The class name or a class atom created by a previous call to the RegisterClass or RegisterClassEx function</param> 
 
    ''' <param name="lpWindowName">The window name (the window's title). If this parameter is NULL, all window names match. </param> 
 
    ''' <returns>If the function succeeds, the return value is a handle to the window that has the specified class name and window name.</returns> 
 
  
 
    <System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint:="FindWindowW")> _ 
 
    Public Shared Function FindWindowW(<System.Runtime.InteropServices.InAttribute> <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)> lpClassName As String, <System.Runtime.InteropServices.InAttribute> <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)> lpWindowName As StringAs System.IntPtr 
 
    End Function 
 
End Class 
 
  
 
  
 
<System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)> _ 
 
Public Structure tagRECT 
 
    Public left As Integer 
 
  
 
    Public top As Integer 
 
  
 
    Public right As Integer 
 
  
 
    Public bottom As Integer 
 
End Structure 
 
  
 
Partial Public Class NativeMethods 
 
    ''' <summary> 
 
    ''' Retrieves the dimensions of the bounding rectangle of the specified window.  
 
    ''' The dimensions are given in screen coordinates that are relative to the upper-left corner of the screen. 
 
    ''' </summary> 
 
    ''' <param name="hWnd">A handle to the window.</param> 
 
    ''' <param name="lpRect">A pointer to a RECT structure that receives the screen coordinates of the upper-left and lower-right corners of the window. </param> 
 
    ''' <returns></returns> 
 
  
 
  
 
    <System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint:="GetWindowRect")> _ 
 
    Public Shared Function GetWindowRect(<System.Runtime.InteropServices.InAttribute> hWnd As System.IntPtr, <System.Runtime.InteropServices.OutAttribute> ByRef lpRect As tagRECT) As <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)> Boolean 
 
    End Function 
 
End Class 
 
  
 
Public Class ThisAddIn 
 
    Private myUserControl As MyUserControl 
 
    Private myCustomTaskPane As Microsoft.Office.Tools.CustomTaskPane 
 
  
 
    Private Const paneName As String = "My Task Pane" 
 
  
 
    Private Sub ThisAddIn_Startup() Handles Me.Startup 
 
        myUserControl = New MyUserControl() 
 
        myCustomTaskPane = Me.CustomTaskPanes.Add(myUserControl, paneName) 
 
        myCustomTaskPane.Visible = True 
 
  
 
        myCustomTaskPane.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionFloating 
 
  
 
        Try 
 
            ' Solution 1 - using commandbar object 
 
  
 
            Dim cmdbar As Office.CommandBar = Globals.ThisAddIn.Application.CommandBars(paneName) 
 
  
 
            System.Windows.Forms.MessageBox.Show("Location of custom task pane " & vbCr & vbLf + "Top: " + CStr(cmdbar.Top) + " Left:" + CStr(cmdbar.Left)) 
 
  
 
  
 
            ' Solution 2 - using windows APIs FindWindow, GetWindowRect methods 
 
  
 
            Dim hCtp As IntPtr = NativeMethods.FindWindowW("MsoCommandBar", paneName) 
 
            Dim rect As tagRECT 
 
  
 
            NativeMethods.GetWindowRect(hCtp, rect) 
 
  
 
            System.Windows.Forms.MessageBox.Show("Location of custom task pane " & vbCr & vbLf + "Top: " + CStr(rect.top) + vbCr & vbLf + "Left: " + CStr(rect.left) + vbCr & vbLf + "Right: " + CStr(rect.right) + vbCr & vbLf + "Bottom: " + CStr(rect.bottom) + vbCr & vbLf) 
 
        Catch ex As Exception 
 
            System.Windows.Forms.MessageBox.Show(ex.Message) 
 
        End Try 
 
    End Sub 
 
  
 
    Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown 
 
  
 
    End Sub 
 
  
 
End Class 
 

 

1.       This project creates an AddIn named VBNETWordAddIn.dll.

2.       Open the word document 揟estDoc.docx?from the project root folder and goto COM Add-ins page and select the AddIn 揤BNETWordAddIn?that we have created.

3.       You will see the two message alerts with custom task pane location information. These message alerts are coming from two different solutions as listed in the code segment.

4.       Once you completed the testing, you can remove the AddIn by going into the COM Add-ins page and deselecting the custom add-in.

Microsoft All-In-One Code Framework is a free, centralized code sample library driven by developers' real-world pains and needs. The goal is to provide customer-driven code samples for all Microsoft development technologies, and reduce developers' efforts in solving typical programming tasks. Our team listens to developers’ pains in the MSDN forums, social media and various DEV communities. We write code samples based on developers’ frequently asked programming tasks, and allow developers to download them with a short sample publishing cycle. Additionally, we offer a free code sample request service. It is a proactive way for our developer community to obtain code samples directly from Microsoft.