簡體   English   中英

C#如何檢測Excel是否失去焦點

[英]C# how to detect if Excel has lost focus

我有一個Excel AddIn(用C#和ExcelDNA編寫),其格式用於允許用戶將數據輸入到繼承自文本框的控件中。 形式是模態的。 控件中的輸入使上下文菜單根據用戶的輸入顯示出來。

如果用戶已輸入數據並且上下文菜單可見,然后用戶將另一個應用程序作為活動應用程序,則上下文菜單將覆蓋該應用程序。

我可以使用Excel應用程序之外的事件來確定Excel失去了焦點嗎?

我想出了一種使上下文菜單顯示而不覆蓋其他項目的方法。 我的問題是,上下文菜單在輸入文本框期間始終可見,因為我在文本框TextChanged事件中將AutoClose屬性設置為false。 現在,當我重新填充項目列表時,在TextChanged事件中將AutoClose屬性設置為false。 輸入第三個字符后,對於在文本框中輸入的任何擊鍵,都將執行此操作。

然后,我創建了一個contextmenu關閉事件,如下所示:

    #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);
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM