How to make WPF Window close when Grasshopper closes?

unhandled

#1

I have a component which creates a WPF Window when it is double clicked. The owner of the Window is registered as the handle of its parent document.

public sealed class MyComponent : GH_Component
{
  public MainWindow MyMainWindow {get; set;}

  protected override void CreateAttributes()
  {
    MyAttribs = new AttributesB(this);
  }
  
  private class AttributesB : GH_ComponentAttributes
  {
    public AttributesB(IGH_Component component) : base(component) { }

    public override GH_ObjectResponse RespondToMouseDoubleClick(GH_Canvas sender, GH_CanvaseMouseEvent e)
    {
      ((MyComponent)Owner).ShowMainWindow();
      return GH_ObjectResponse.Handled;
    }
  }

  public void ShowMainWindow()
  {
    if(MyMainWindow == null || MyMainWindow.IsClosed())
    {
      MyMainWindow = new MainWindow(MyComponent);
    }

    GH_DocumentEditor owner = Grasshopper.Instances.DocumentEditor;
    WindowInteropHelper helper = new WindowInteropHelper(MyMainWindow);
    helper.Owner = owner.Handle;

    MyMainWindow.Show();
  }
}

When I minimize the Grasshopper window, this new window also minimizes along with it. However, when I close the Grasshopper window, the new window does not close with it. After I close the new window manually, Rhino becomes unresponsive, as if the user cannot interact with it.

Is there a way to call the new window to close when Grasshopper closes?


(David Rutten) #2

Grasshopper doesn’t actually close when you click on the [X] button, it intercepts the event and merely hides the window. If you can handle winforms events, that would be ideal. Can you?


#3

I see. But what is the event I should handle? I tried to use GH_DocumentEditor.EditorFoldStateChanged but the results are the same.


(David Rutten) #4

I have to run to town for an appointment, but have a look at Form.Visiblechanged for when the form goes away entirely, and maybe check the WindowState property from a timer. I can’t find an event for changes to WindowState. FoldState is something particular to Grasshopper, it means that the window was collapsed into it’s own title bar. This happens when you double click on the title bar, you may want to handle that case though.


#5

Thanks. I’ll have a look at that.