要用 .NET 程式來做 DDE 的連結, 最簡單的方法是用 NDde這個程式庫. NDde雖然有點複雜, 但比起微軟提供的 DDEML (DDE Management Library) 要好用十倍以上. 以下簡單介紹 使用NDde連接 DDE 資訊源的方法.
首先宣告 DdeClient, 使用 Connect() 方法連線, 並指定 client.Advise 事件處理程式
NDde.Client.DdeClient client = new NDde.Client.DdeClient(service, topic);
client.Connect();
client.Advise += new EventHandler(client_Advise);
其中的 service 和 topic 看使用的資訊源而定. 如果你不確定 service 和 topic 是什麼, 只要能在 Excel 用 DDE 連結, 在 Excel 的公式中就是分三個部份 =serivce|topic!item.
client_Advise 方法就是用來接收動態更新的事件. 當 DDE 資訊源有更新資料時, client_Advise 就會立刻被執行. 更新的資料就以 string 儲存在參數 e.Item 之中
void client_Advise(object sender, NDde.Client.DdeAdviseEventArgs e)
{
string item = e.Item;
// 更新資料後的處理
}
只要這兩個簡單的步驟就可以開始動態連結資訊源了, 夠簡單了吧?