none
Managed Message Filter doesn't work for control embedded in Custom Task Pane

    Question

  • [VSTO for Office 2007, Managed Excel AddIn]

    I have a WinForm control that registers Message Filter (System.Windows.Forms.IMessageFilter).

    When I show form with that control, Message Filter is called,

    when control is added to Custom Task Pane, Message Filter is not called.

    What can I do?

     

    Sunday, July 17, 2011 7:54 AM

Answers

  • Hi Stas,

     

    As far as I know, the WinForm has specific method like DefWndProc(ref Message m) or specific interface link IMessageFilter to handle window message. However Office PIA hasn’t provided such kind of method or interface to handle window message. If you want to handle message in Office application, I think this is a way: find the window handler of the via Window API and let’s see whether we can convert Office application’s window to Windows Form class. Then override DefWndProce or use IMessageFilter.

     

    I hope it can help you.

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Stas Bushuev Friday, July 22, 2011 12:16 PM
    Friday, July 22, 2011 7:46 AM
    Moderator

All replies

  • Hi Stas,
    Thanks for posting in the MSDN Forum.
    Due to the System.Windows.Forms.IMessageFilter isn't a default control component of the Visual studio would you please clarify where I can get it. I assume this control is about window search. Is it right?
    Have a good day,
    Tom

    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, July 19, 2011 12:20 PM
    Moderator
  • Hi Tom

    IMessageFilter used like PreTranslateMessage in MFC.

    I'd better give a simple example:

    namespace ExcelAddInFilter
    {
    	public interface IPreTranslateMessage
    	{
    		bool PreTranslateMessage(ref System.Windows.Forms.Message message);
    	}
    
    	public class Filter : System.Windows.Forms.IMessageFilter
    	{
    		public virtual bool PreFilterMessage(ref System.Windows.Forms.Message m)
    		{
    			System.Windows.Forms.Control c = System.Windows.Forms.Control.FromChildHandle(m.HWnd);
    			IPreTranslateMessage ptm = c as IPreTranslateMessage;
    			if (c != null)
    				return ptm.PreTranslateMessage(ref m);
    			return false;
    		}
    	}
    
    	public class TestControl : System.Windows.Forms.TextBox, IPreTranslateMessage
    	{
    		public virtual bool PreTranslateMessage(ref System.Windows.Forms.Message message)
    		{
    			if (message.Msg == 0x100 /*WM_KEYDOWN*/)
    				System.Diagnostics.Debug.WriteLine("TestControl got WM_KEYDOWN");
    			return false;
    		}
    	}
    
      public partial class ThisAddIn
      {
    		private System.Windows.Forms.Form m_Form = null;
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
    			System.Windows.Forms.Application.AddMessageFilter(new Filter());
    			//Test control on Form
    			m_Form = new System.Windows.Forms.Form();
    			m_Form.Controls.Add(new TestControl());
    			m_Form.Show();
    			//Test control in CTP
    			var usercontrol = new System.Windows.Forms.UserControl();
    			usercontrol.Controls.Add(new TestControl());
    			var ctp = this.CustomTaskPanes.Add(usercontrol, "Bottom");
    			ctp.Visible = true;
    			ctp.DockPosition = Office.MsoCTPDockPosition.msoCTPDockPositionBottom;
        }
    
        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }
    
        //VSTO generated code skipped
      }
    }
    
    


    there are form ant custom task pane after AddIn start.

    If press key in form's TextBox debug message "TestControl got WM_KEYDOWN" will be outputed.

    If press key in CTP's TextBox debug message "TestControl got WM_KEYDOWN" will not be outputed.

     TestControl models control that depends on this scenario for correct work.

    So should I set up something or it is intended to work that way, and in latter case is there a workaround?

     

    Thursday, July 21, 2011 8:38 AM
  • Hi Stas,

     

    As far as I know, the WinForm has specific method like DefWndProc(ref Message m) or specific interface link IMessageFilter to handle window message. However Office PIA hasn’t provided such kind of method or interface to handle window message. If you want to handle message in Office application, I think this is a way: find the window handler of the via Window API and let’s see whether we can convert Office application’s window to Windows Form class. Then override DefWndProce or use IMessageFilter.

     

    I hope it can help you.

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Stas Bushuev Friday, July 22, 2011 12:16 PM
    Friday, July 22, 2011 7:46 AM
    Moderator