none
Using modal winforms in OneNote RRS feed

  • Question

  • Hello MS-Office Experts,

    I am developing a COM-based OneNote 2013 add-in in c#. As part of the add-in, modal winforms are to be shown. Normally, one would do this along the lines of

    OneNote.Window context = control.Context as OneNote.Window;
                CWin32WindowWrapper owner =
                    new CWin32WindowWrapper((IntPtr)context.WindowHandle);
    
    MyForm form = new MyForm();
    form.ShowDialog(owner);

    However, OneNote requires the add-in to be executed inside the DLLSurrogate/dllhost process. This collides with the desired modality of the winform as modality across process boundaries is not meant to be. As a consequence many minor and major issues arise:

    + the winform does not flash when clicking on OneNote (the supposed owner of the winform)

    + the winform looses focus when clicking on OneNote. However, OneNote is still being blocked

    + the add-in hangs when touching OneNote (i.e. touching the multi-touch monitor where OneNote is visible)

    Any idea what to do?

    I figure that the root cause of the problem is the process boundary. Is it possible to create a custom surrogate that runs inside the OneNote process? Would that solve the problem?

    Thank you for your time an effort

    Best

    Friday, October 23, 2015 10:50 AM

All replies

  • Why did you need to specific the owner for the dialog? Is it helpful removing the owner?
    Monday, October 26, 2015 12:21 PM
  • If the owner is not passed into the modal dialog, i.e.

    form.ShowDialog();

    the modal dialog freezes no more on touching gestures. However, the dialog is not really blocking OneNote any longer. For example, if i click on a OneNote control its event handler is suspended until the dialog is closed.

    i believe to have found the root cause of the whole mess (without being able to come up with a solution): OneNote add-ins are in-process dll-servers that runs inside the dllhost/COMSurrogate process. I.e. OneNote is executed in the OneNote process and the winform dialog in the DllSurrogate process. Unfortunately, modal relationship of windows across process boundaries is a tricky thing.

    Any Ideas? One of my more creative ideas was to unregister the add-in from the SCM and re-register it as a real in-proc server, but that may be total nonsense.

    Friday, November 6, 2015 9:09 PM
  • Hi,

    Based on the research, the owner for the dialog is setting to the workspace window in OneNote instead of the OneNote application window. After I change the window to the OneNote application window, the code works well for me.

    Here is the code for your reference:

    [DllImport("User32.dll")]
    public static extern IntPtr GetParent(IntPtr hWnd);
    
    public void OnButtonClick(IRibbonControl control)
    {
                
                OneNote.Window context = control.Context as OneNote.Window;
               
                CWin32WindowWrapper owner =
                 new CWin32WindowWrapper(GetParent(GetParent((IntPtr)context.WindowHandle))); 
                
                MyForm form = new MyForm();
                form.ShowDialog(owner);
    
    
    }

    And we can get the real window by using Microsoft Spy++ via the window handler in the code. Here is an figure for your reference:


    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, November 9, 2015 10:01 AM
    Moderator