Set focus to Office 2007 Custom Task Pane by clicking on the title bar

copy from outside of GFW:http://aritrasaha.wordpress.com/2009/06/30/set-focus-to-office-2007-custom-task-pane-by-clicking-on-the-title-bar/

When we mouse over the title bar of the Custom Task Pane (CTP), Office provides us a handle to move the CTP – this behavior does not trigger any event or changes the focus, this is by design.

We can trap the Windows API Messages passed to the CTP and handle them to set focus to the CTP when the user clicks on the title bar. The title bar of the CTP turns orange to indicate it’s in focus.

 

The steps are as follows :-

1.       Subclass a NativeWindow and override its WndPrc protected method to handle the WM_LBUTTONUP message as follows :

 

    public class TaskPaneEvent : System.Windows.Forms.NativeWindow
    {
        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            const int WM_LBUTTONUP = 0×202;
 
            // *always* let the base class process the message
            base.WndProc(ref m);
 
            if (m.Msg == WM_LBUTTONUP)
            {
                // Set Focus to the control inside the CTP
                taskPane.Control.Focus();
            }           
        }
     }  

 

 2.       Find the handle of the Office Task Pane (using SPY++) and pass it to the AssignHandle method of the TaskPaneEvent class.

 

string sTaskPaneHeading = "My Custom Task Pane";
 
      IntPtr h = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
      h = NativeMethods.FindWindowExW(h, new IntPtr(0), "MsoCommandBarDock", "MsoDockRight");
//The 4th parameter can be MsoDockLeft, MsoDockTop or MsoDockBottom depending upon the position of the CTP in Office UI
      h = NativeMethods.FindWindowExW(h, new IntPtr(0), "MsoCommandBar", sTaskPaneHeading);
 
      TaskPaneEvent tbEvent = new TaskPaneEvent();
      tbEvent.AssignHandle(h); 

  

 

 P.S.  Rememeber to release the handle of the TaskPaneEvent when the add in exits, to avoid Office crashes.

    tbEvent.ReleaseHandle();

posted @ 2012-03-26 20:40  BinSys  阅读(446)  评论(0编辑  收藏  举报