Sdílet prostřednictvím


Playing macros

When a ShellCommand macro statement is played back, and your Visual Studio Tools form is active, the PlayMacroItem() method will be called. You must override this method in your Visual Studio Tools add-in code, and process the macro statements for your form. The following C# example shows how you override the base method.

public override void PlayMacroItem(MacroPlaybackEventArgs e)
{

}

Event arguments

The following event arguments are passed into the PlayMacroItem() method:

Handled   This is a boolean parameter that indicates whether the action for macro statement has been performed. You should check the value of this parameter at the beginning of your PlayMacroItem() method. If its value is true, the macro statement has already been handled, so your code should take no action. If you determine that your code should perform the action for the macro statement, set the Handled parameter to true when the action is complete. This tells other code that the macro statement has already been processed.

MacroText   This string parameter contains the macro string that was written by the RecordMacroItem() method when the macro was recorded. You will need parse this string to determine whether your add-in must perform the macro action.

MacroComment   This string parameter contains the comment that was written by the RecordMacroItem() method when the macro was recorded.

Processing macro statements

Your code will retrieve the MacroText parameter from the event arguments, and then parse the text to determine whether this macro statement should be performed by your add-in. You can use any approach you like when parsing the macro statement. The Environmental Details sample uses regular expressions to parse the macro statement. The following C# example shows the complete PlayMacroItem() method for the sample. Notice that if the code performs the macro action, it sets the Handled parameter to true. If it doesn't perform the action, it allows the base class to do so.

public override void PlayMacroItem(MacroPlaybackEventArgs e)
{
    Match m;

    if (e.Handled == false)
    {
        // Process Yearly Energy Cost text box
        m = Regex.Match(e.MacroText, "TypeTo YearlyEnergyCost \"(.+)\"");
        if (m.Success)
        {
            if (m.Groups.Count >= 2)
            {
                textBoxYearlyEnergyCost.Focus();
                textBoxYearlyEnergyCost.Text = m.Groups[1].Value;
                e.Handled = true;
            }
        }

        // Process Notes text box
        m = Regex.Match(e.MacroText, "TypeTo EnvNotes \"(.+)\"");
        if (m.Success)
        {
            if (m.Groups.Count >= 2)
            {
                textBoxNotes.Focus();
                textBoxNotes.Text = m.Groups[1].Value;
                e.Handled = true;
            }
        }

        // Process the push buttons and check boxes
        m = Regex.Match(e.MacroText, "ClickHit (\\w+)");
        if (m.Success)
        {
            if (m.Groups.Count >= 2)
            {
                // Energy Star check box
                if (m.Groups[1].Value == "EnergyStar")
                {
                    checkBoxEnergyStar.Focus();
                    checkBoxEnergyStar.Checked = !checkBoxEnergyStar.Checked;
                    e.Handled = true;
                }

                // Recyclable check box
                if (m.Groups[1].Value == "Recyclable")
                {
                    checkBoxRecyclable.Focus();
                    checkBoxRecyclable.Checked = !checkBoxRecyclable.Checked;
                    e.Handled = true;
                }

                // Close button
                if (m.Groups[1].Value == "CloseButton")
                {
                    buttonClose.Focus();
                    buttonClose.PerformClick();
                    e.Handled = true;
                }
            }
        }
    }

    // Macro command was not ours, so let the base class process it.
    base.PlayMacroItem(e);
}