1

次のような Outlook コンテキスト メニュー ボタンを作成する多くの例を見ています。

    private void Application_ItemContextMenuDisplay(Microsoft.Office.Core.CommandBar CommandBar, 
                                                    Selection Selection)
    {

        var button = (Office.CommandBarButton)CommandBar.Controls.
                            Add(Office.MsoControlType.msoControlButton, missing,
                                missing, missing, missing);            
        button.accName = "SowSelectedItem";
        button.DescriptionText = "Show Selected item";
        button.Caption = button.DescriptionText;
        button.Tag = "ShowSelectedItem";
        button.Click += ContextMenuItemClicked;
    }

これは一度だけ正しく動作します。メニューにボタンが作成され、クリック可能になり、最初のアクセスでイベント ハンドラーが起動します。

ただし、メニューのアクティブ化は繰り返し発生し、実行されるたびに別のイベント ハンドラーが追加されたように見えます (ただし、メニューには 1 つのボタンしか表示されません)。クリック ハンドラーが蓄積されています (毎回新しいボタンを追加していますが)。

わかりましたので、私はできると思いました:

  • フラグを設定し、ボタンを 1 回だけロードします (その後のアクティブ化では表示されません)。
  • ボタンをキャッシュし、毎回追加します (以降のアクティベーションでも表示されません)。
  • ボタンをキャッシュして、毎回表示および有効化します (表示されません)。

ここで何かが足りない気がします。正しいボタン アクティベーションと 1 つのイベント ハンドラー ヒットのみを取得するように、ボタンを接続するにはどうすればよいですか?

4

1 に答える 1

2

Outlookがボタンに対して何をしているのかはまだよくわかりませんが、次のコードは、メニューのアクティブ化と非アクティブ化の両方を実装し、非アクティブ化でイベントハンドラーを削除することで、問題に対処しています。

public partial class ContextMenuLookupAddin
{
    Outlook.Explorer currentExplorer = null;
    CommandBarButton contextButton = null;

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        currentExplorer = Application.ActiveExplorer();
        Application.ItemContextMenuDisplay += Application_ItemContextMenuDisplay;
        Application.ContextMenuClose +=Application_ContextMenuClose;
    }

    private void Application_ItemContextMenuDisplay(Microsoft.Office.Core.CommandBar CommandBar, 
                                                    Selection Selection)
    {
        if (contextButton == null)
        {
            contextButton = (Office.CommandBarButton)CommandBar.Controls.
                                Add(Office.MsoControlType.msoControlButton, missing,
                                    missing, missing, missing);

            contextButton.accName = "SowSelectedItem";
        }

        contextButton.DescriptionText = "Show Selected item";
        contextButton.Caption = contextButton.DescriptionText;
        contextButton.Tag = "ShowSelectedItem";
        contextButton.FaceId = 4000;
        contextButton.OnAction = "OutlookIntegration.ThisAddIn.ContextMenuItemClicked";
        contextButton.Click += ContextMenuItemClicked;
    }

    private void Application_ContextMenuClose(OlContextMenu ContextMenu)
    {
        contextButton.Click -= ContextMenuItemClicked;
        contextButton.Delete(missing);
        contextButton = null;
    }

    private void ContextMenuItemClicked(CommandBarButton Ctrl, ref bool CancelDefault)
    {
        if (currentExplorer.Selection.Count > 0)
        {
            object selObject = currentExplorer.Selection[1];
            if (selObject is MailItem)
            {
                // do your stuff with the selected message here
                MailItem mail = selObject as MailItem;
                MessageBox.Show("Message Subject: " + mail.Subject);
            }
        } 
    }
 }
于 2012-08-30T09:14:53.913 に答える