|
9#

樓主 |
發(fā)表于 2014-2-21 14:14:07
|
只看該作者
六、編程步驟-2
代碼一:CustomSmartTag.cs
using System;
using System.Windows.Forms;
using Microsoft.Office.Tools.Excel;
using Microsoft.Office.Interop.SmartTag;
namespace Trin_ExcelDerivedSmartTags
{
public class CustomSmartTag : SmartTag
{
//TO DO
}
}
4、在新建的類中,創(chuàng)建智能標記的操作。操作是指顯示在智能標記菜單中的項。
在啟動事件中,添加代碼。
代碼二:ThisAddIn.cs
// Declare Actions for this SmartTag
Action Action1 = new Action("Display property value");
Action Action2 = new Action("Display smart tag details");
public CustomSmartTag() : base("http://vsto.5d6d.com/#DemoSmartTag", "我的智能標簽")
{
this.Terms.AddRange(new string[] { "潘淳", "陳國良","mye.cn" });
Actions = new Action[] { Action1, Action2 };
Action1.Click += new ActionClickEventHandler(Action1_Click);
Action2.Click += new ActionClickEventHandler(Action2_Click);
}
5、重寫 Recognize 方法以實現(xiàn)自己的自定義識別行為。您的 Recognize 實現(xiàn)必須調(diào)用 PersistTag 方法以使 Excel 能夠識別該智能標記。
代碼三:CustomSmartTag .cs
protected override void Recognize(string text, ISmartTagRecognizerSite site, ISmartTagTokenList tokenList)
{
// Determine whether each smart tag term exists in
// the document text.
foreach (string term in this.Terms)
{
// Search the cell text for the first instance of
// the current smart tag term.
int index = this.CellText.IndexOf(term, 0);
if (index >= 0)
{
// Create a smart tag token and a property bag for the
// recognized term.
ISmartTagProperties propertyBag = site.GetNewPropertyBag();
// Write a new property value.
string key = "Key1";
propertyBag.Write(key, DateTime.Now.ToString());
// Attach the smart tag to the term in the document
this.PersistTag(propertyBag);
// This implementation only finds the first instance
// of a smart tag term in the cell.
break;
}
}
6、創(chuàng)建事件處理程序以響應所創(chuàng)建操作的 Click 事件。
代碼四:CustomSmartTag .cs
// This action displays the property value for the term.
private void Action1_Click(object sender, ActionEventArgs e)
{
ISmartTagProperties propertyBag = e.Properties;
string key = "Key1";
MessageBox.Show("The corresponding value of " + key + " is: " + propertyBag.get_Read(key));
}
// This action displays smart tag details.
private void Action2_Click(object sender, ActionEventArgs e)
{
MessageBox.Show("The current smart tag caption is '" + this.Caption + "'. The current smart tag type is '" + this.SmartTagType + "'.");
}
7、在項目工作簿的代碼文件中,向工作簿中的 VstoSmartTags 添加智能標記實例。
代碼五:CustomSmartTag .cs
{
//啟動“智能標簽/動作”
Application.SmartTagRecognizers.Recognize = true;
//與項目掛鉤
this.VstoSmartTags.Add(new CustomSmartTag().Base);
}
順例說一下,Smart Tag 是一個被忽視的重大功能,想像空間近乎無窮...
|
|