c#如何检测Excel是否失去焦点

本文关键字:Excel 是否 失去 焦点 检测 何检测 | 更新日期: 2023-09-27 17:49:32

我有一个Excel插件(用c#和ExcelDNA编写),其中包含一个表单,用于允许用户将数据输入到继承自文本框的控件中。形式是情态的。进入控件将导致一个上下文菜单出现,其中包含基于用户输入的选项。

如果用户输入了数据,并且上下文菜单是可见的,然后用户将另一个应用程序设置为活动应用程序,那么上下文菜单将覆盖该应用程序。

是否有一个事件,我可以使用Excel应用程序来确定Excel已经失去焦点?

c#如何检测Excel是否失去焦点

我想出了一种方法,使上下文菜单显示而不覆盖其他项。我的问题是,在进入文本框期间,上下文菜单总是可见的,因为我在文本框TextChanged事件中将AutoClose属性设置为false。现在,在重新填充项目列表时,我将TextChanged事件中的AutoClose属性设置为false。

然后我创建了一个上下文菜单关闭事件,如下所示:

    #region Instance Variables
    ContextMenuStrip menuStrip = new System.Windows.Forms.ContextMenuStrip();
    public event EventHandler EntryComplete;
    public event EventHandler EntryNotComplete;
    public event EventHandler EntryError;
    #endregion
    // Control Constructor
    public AutoCompleteTextBox()
    {
        InitializeComponent();
        menuStrip.PreviewKeyDown += menuStrip_PreviewKeyDown;
        this.Leave += AutoCompleteTextBox_Leave;
        // Use closing event so that we can determine when to close the menustrip.
        menuStrip.Closing += new ToolStripDropDownClosingEventHandler(menuStrip_Closing);
    }
    void menuStrip_Closing(object sender, ToolStripDropDownClosingEventArgs e)
    {
        // only close the menu strip when an item is selected or the application loses focus
        if (e.CloseReason != ToolStripDropDownCloseReason.ItemClicked &&
            e.CloseReason != ToolStripDropDownCloseReason.AppFocusChange)
        {
            e.Cancel = true;
        }
    }
    private void AutoCompleteTextBox_TextChanged(object sender, EventArgs e)
    {
        .
        .
        .
        try
        {
            // get information on whether a ToolbarMenuItem has been selected
            MenuItem info = new MenuItem();
            MenuItemInfo selectedToolStripMenuInfo = info.SelectedItem(menuStrip);
            menuStrip.AutoClose = true;
            menuStrip.Visible = false;
            menuStrip.Items.Clear();
            if (selectedToolStripMenuInfo == null)
            {
                EntryNotComplete(sender, e);
            }
            if (base.Text.Length >= 3 && selectedToolStripMenuInfo == null)
            {
                .
                .
                .
                menuStrip.AutoClose = false;
                // foreach loop to add items into list
                foreach (SearchType item in lst)
                {
                    szMenuItem = ...;
                    ToolStripItem tsItem = new ToolStripMenuItem();
                    tsItem.Text = szMenuItem;
                    tsItem.Name = item.DealId;
                    tsItem.Click += tsItem_Click;
                    tsItem.Font = new Font("Courier New", 8.0F, FontStyle.Italic);
                    menuStrip.Items.Add(tsItem);
                }
                Point point = base.Location;
                point.Offset(2, base.Height + 2);
                point = base.GetPositionFromCharIndex(base.SelectionStart);
                point.Offset(2, base.Font.Height + 2);
                base.ContextMenuStrip = menuStrip;
                base.ContextMenuStrip.Show(base.PointToScreen(point));
                base.Focus();
                menuStrip.AutoClose = true;
            }
            else if (base.Text.Length >= 3 && selectedToolStripMenuInfo != null)
            {
                EntryComplete(sender, e);
            }
        }
        catch (Exception ex)
        {
            ErrorDescription = ex.Message;
            menuStrip.AutoClose = true;
            menuStrip.Visible = false;
            EntryError(sender, e);
        }
    }