none
Word loses focus (minimized) after closing non-modal form RRS feed

  • Question

  • Hi,

    I have a problem with the Word Add-in I'm developing. 

    The problem:

     If I open non-modal form called Form1, and inside this form I press a button that opens another modal form called Form2. Now I close both forms, but Word windows is losing focus and minimized.When I open a non-modal form (let's call it Form1) by using

    Form1.Show(IWin32Window);

    Where the IWin32Window object is created by this method:

    public static MyWin32Window getWordWindow()   

    {           

    IntPtr wordWindow = NativeMethods.FindWindowW("OpusApp", Globals.ThisAddIn.Application.ActiveWindow.Caption + " - Microsoft Word");

    MyWin32Window myWin = new MyWin32Window(wordWindow);           

    return myWin;       

    }


    The problem occurs when I open another form from Form1, let's say Form2 is opened, but Form2 is modal form and opened with

    Form2.ShowDialog();

    It works fine, but when I close Form2 and then Form1, the Word window is minimized. How can I prevent this behavior?

    I also tried setting Form1 as the owner of Form2, like this:

    Form2.ShowDialog(this);

    But same result.

    Thanks,

    Etai.





    • Edited by etaiso Wednesday, January 22, 2014 8:07 AM
    Wednesday, January 22, 2014 7:49 AM

All replies

  • Hello Etai,

    The ShowDialog method accepts the IWin32Window interface too. Did you try to pass an instance of the IWin32Window interface?

    Finally, Word 2013 introduced the Hwnd property for the Window class. So, if you use Word 2013 on the machine, there is no need to use the FindWindow function from Window API.

    Wednesday, January 22, 2014 7:22 PM
  • Hi Eugene, thanks for your reply.

    I do know that ShowDialog accepts IWin32Window. I used inherited class to prevent Word from crashing on exit. Anyway, they are both suffering from the losing focus issue.

    My add-in supports Word 2010. I think this is something regarding the owner of the second form.

    Thursday, January 23, 2014 7:10 AM
  • Hi Etai,

    What I am try to say is that you need to specify the same window handle represented by the IWin32Window interface for both calls:

    Form1.Show(IWin32Window);
    // 
    Form2.ShowDialog(IWin32Window);
    So, you need pass a value returned by the getWordWindow() method in both cases.
    • Marked as answer by George HuaModerator Wednesday, January 29, 2014 2:14 AM
    • Unmarked as answer by etaiso Tuesday, February 4, 2014 7:36 AM
    Friday, January 24, 2014 3:38 PM
  • Sorry for bumping this up but the solution does not works for me. I will try to post more code here:

    Form1 (the modeless one) is opened via:

    Form1.Show(SourceAddIn.getWordWindow());

    Its Show(IWin32Window) is shadowed and looks like this:

    public new void Show(IWin32Window win)
    {
        if (firstRun)
        {
            firstRun = false;
    
            base.Show(win);
        }
        else
        {
            if (NeedRefresh)
            {
                 BuildTargetsWithTable();
                 NeedRefresh = false;
            }
    
            this.Visible = true;
        }
    }

    And Form2 (the modal form) is opened via:

    Form2.ShowDialog(SourceAddIn.getWordWindow());


    Tuesday, February 4, 2014 7:43 AM