1

不知道如何解释,但是:

  • 有一个控件MyPopup,做出来的ToolStripDropDown
  • 有很多基于MyPopup控件(称为弹出窗口);
  • ;打开弹出窗口没有Form问题
  • 但是弹出窗口打开弹出窗口有问题

问题是在子弹出窗口关闭后,即使父窗口Form获得焦点,父弹出窗口仍保留在屏幕上。关闭卡住的父弹出窗口的唯一方法是(用鼠标)将注意力集中在它上并点击Esc.

为了让弹出窗口能够显示另一个弹出窗口,我必须欺骗Closing事件:

    void Popup_Closing(object sender, ToolStripDropDownClosingEventArgs e)
    {
        // prevent from closing when stay
        if (_stay && e.CloseReason != ToolStripDropDownCloseReason.CloseCalled )
        {
            e.Cancel = true;
            return;
        }
    }

在关闭子弹出窗口之前或之后,父弹出窗口具有:

  • _stay的值为False;
  • Popup.AutoClose值为True;

我尝试使用以下内容将鼠标焦点“带回”父弹出窗口:

  • TopLevel=true没运气;
  • Focus();没运气;
  • Focused=true;没运气;
  • AutoClose=true;没运气;
  • Captured=true;没运气;

还尝试将上述值设置为False然后设置为True,仍然没有运气。

这里还有一些代码,可能有用也可能没用:

public class MyPopup : UserControl
{
    protected bool _stay = false;

    private ToolStripDropDown _popup;
    private ToolStripControlHost _host;

    public MyPopup()
    {
        // create popup
        _popup = new ToolStripDropDown();
        _popup.Margin = _popup.Padding = Padding.Empty;
        _popup.AutoSize = false;
        _popup.Closing += Popup_Closing;
        // add host
        _host = new ToolStripControlHost(this);
        _host.Margin = _host.Padding = Padding.Empty;
        _host.AutoSize = false;
        _popup.Items.Add(_host);
    }

    public void Show(Control parent, int x, int y)
    {
        _popup.Show(parent, x, y);
    }

    public new void Hide()
    {
        _popup.Close();
    }

    private void Popup_Closing(object sender, ToolStripDropDownClosingEventArgs e)
    {
        // prevent from closing when stay
        if (_stay && e.CloseReason != ToolStripDropDownCloseReason.CloseCalled )
        {
            e.Cancel = true;
            return;
        }
    }

    protected void PopupChildClosedDefaultEvent(object sender, EventArgs e)
    {
        // hide popup if mouse is outside of client region when closing child popup
        if (!ClientRectangle.Contains(PointToClient(MousePosition)))
            Hide();
        else
        {
            // >> here I am trying different things <<
            _popup.AutoClose = false;
            _popup.AutoClose = true;
        }
    }
}

public class PopupParent: MyPopup
{
    private void TestChildren()
    {
        _stay = true;
        PopupChild popup = new PopupChild();
        popup.Show(button1, 0, 0);
        popup.Closed += PopupChildClosedDefaultEvent;
        _stay = false;
    }
}

public class PopupChild: MyPopup
{
}

问题:在鼠标事件(单击客户区域外的某处)失去自动关闭能力后,有什么方法可以修复“损坏的”弹出窗口?

4

1 回答 1

1

好的,早上给大脑带来了一些新鲜感,我设法通过以下方式解决了这个问题:

            _popup.Close();
            _popup.Show(_parent, _x, _y);

所以我必须重新显示弹出窗口才能让它像以前一样运行。它闪烁,但有效。

于 2013-04-23T06:42:01.043 回答