通过金山词霸获得单词音标和解释的方法

欢迎光临http://www.5xiuxiu.cn/  淘宝购物导航 源码开源免费下载

 我使用的是金山词霸2003,C#语言,在visual studio 2008的工程中添加引用--》com ---》kEngine

词霸的com接口如下:

// Generated .IDL file (by the OLE/COM Object Viewer)
//
// typelib filename: KSEngine.dll

[
 uuid(4A548BD4-98D6-4FCD-9A3C-79894A606FD1),
 version(1.0),
 helpstring("KSEngine 1.0 Type Library"),
 custom(DE77BA64-517C-11D1-A2DA-0000F8773CE9, 83951780),
 custom(DE77BA63-517C-11D1-A2DA-0000F8773CE9, 1086159069)

]
library KSENGINELib
{
   // TLib :     // TLib : OLE Automation : {00020430-0000-0000-C000-000000000046}
   importlib("stdole2.tlb");

   // Forward declare all types defined in this typelib
   interface IDictionary;
   interface IDictionaries;
   interface IDictionaryEx;
   interface IWord;
   interface IExplanation;
   interface IBlock;

   [
     uuid(69B940A9-FA3A-4F8F-B64E-98BBFA7B8E99),
     helpstring("Dictionary Class")
   ]
   coclass Dictionary {
       [default] interface IDictionary;
   };

   [
     odl,
     uuid(71FA90AF-3ECB-496D-A89D-7EB23C8A0D98),
     helpstring("IDictionary Interface"),
     dual,
     oleautomation
   ]
   interface IDictionary : IDispatch {
       [id(0x00000001), propget, helpstring("property SrcLanguageId")]
       HRESULT SrcLanguageId([out, retval] long* pVal);
       [id(0x00000001), propput, helpstring("property SrcLanguageId")]
       HRESULT SrcLanguageId([in] long pVal);
       [id(0x00000002), propget, helpstring("property DestLanguageId")]
       HRESULT DestLanguageId([out, retval] long* pVal);
       [id(0x00000002), propput, helpstring("property DestLanguageId")]
       HRESULT DestLanguageId([in] long pVal);
       [id(0x00000003), helpstring("method Lookup")]
       HRESULT Lookup(
                       BSTR bstrWord,
                       long lFlag);
       [id(0x00000004), helpstring("method GetTitle")]
       HRESULT GetTitle([out, retval] BSTR* bstrTitle);
       [id(0x00000005), helpstring("method GetExplain")]
       HRESULT GetExplain([out, retval] BSTR* bstrExplain);
       [id(0x00000006), helpstring("method GetPhonetic")]
       HRESULT GetPhonetic([out, retval] BSTR* bstrPhonetic);
       [id(0x00000007), helpstring("method Open")]
       HRESULT Open(BSTR bstrPath);
       [id(0x00000008), helpstring("method Close")]
       HRESULT Close();
       [id(0x00000009), helpstring("method GetExplainA")]
       HRESULT GetExplainA([out, retval] VARIANT* varExplain);
       [id(0x0000000a), propget, helpstring("property bstrExplain")]
       HRESULT bstrExplain([out, retval] BSTR* pVal);
       [id(0x0000000a), propput, helpstring("property bstrExplain")]
       HRESULT bstrExplain([in] BSTR pVal);
       [id(0x0000000b), helpstring("method GetWordCount")]
       HRESULT GetWordCount([out, retval] long* pCount);
       [id(0x0000000c), helpstring("method GetWordPhonetic")]
       HRESULT GetWordPhonetic(
                       [in] BSTR bstrWord,
                       [out, retval] BSTR* pbstrPhonetic);
       [id(0x0000000d), helpstring("method GetWordExplain")]
       HRESULT GetWordExplain(
                       [in] BSTR bstrWord,
                       [out, retval] BSTR* pbstrExplain);
       [id(0x0000000e), helpstring("method GetWord")]
       HRESULT GetWord(
                       [in] long lWordID,
                       [out, retval] BSTR* pbstrWord);
   };

   [
     uuid(B6BA8317-58A0-433C-9B85-69F7B30712A6),
     helpstring("Dictionaries Class")
   ]
   coclass Dictionaries {
       [default] interface IDictionaries;
   };

   [
     odl,
     uuid(CE470311-3C6C-4EF3-97DD-6B69F92CFE5B),
     helpstring("IDictionaries Interface"),
     dual,
     oleautomation
   ]
   interface IDictionaries : IDispatch {
       [id(0x00000001), helpstring("method Load")]
       HRESULT Load();
       [id(0x00000002), helpstring("method Add")]
       HRESULT Add(
                       [in] BSTR bstrDictPath,
                       [out, retval] IDictionaryEx** ppDict);
       [id(0x00000003), helpstring("method Item")]
       HRESULT Item(
                       [in] long Index,
                       [out, retval] IDictionaryEx** ppDict);
       [id(0x00000004), propget, helpstring("property Count")]
       HRESULT Count([out, retval] long* pVal);
   };

   [
     odl,
     uuid(A0783717-535B-4ECF-862A-B30ED3DE7A64),
     helpstring("IDictionaryEx Interface"),
     dual,
     oleautomation
   ]
   interface IDictionaryEx : IDispatch {
       [id(0x00000001), helpstring("method Open")]
       HRESULT Open([in] BSTR bstrPath);
       [id(0x00000002), helpstring("method Close")]
       HRESULT Close();
       [id(0x00000003), propget, helpstring("property Name")]
       HRESULT Name([out, retval] BSTR* pVal);
       [id(0x00000004), propget, helpstring("property Path")]
       HRESULT Path([out, retval] BSTR* pVal);
       [id(0x00000005), helpstring("method Lookup")]
       HRESULT Lookup(
                       [in] BSTR bstrWord,
                       [in] long lFlag,
                       [out, retval] IWord** pWord);
       [id(0x00000006), propget, helpstring("property LocalName")]
       HRESULT LocalName([out, retval] BSTR* pVal);
       [id(0x00000007), propget, helpstring("property DictionaryID")]
       HRESULT DictionaryID([out, retval] long* pVal);
       [id(0x00000008), propget, helpstring("property Version")]
       HRESULT Version([out, retval] long* pVal);
       [id(0x00000009), propget, helpstring("property SourceLCID")]
       HRESULT SourceLCID([out, retval] long* pVal);
       [id(0x0000000a), propget, helpstring("property TargetLCID")]
       HRESULT TargetLCID([out, retval] long* pVal);
       [id(0x0000000b), propget, helpstring("property WordCount")]
       HRESULT WordCount([out, retval] long* pVal);
       [id(0x0000000c), propget, helpstring("property MaxWordLength")]
       HRESULT MaxWordLength([out, retval] long* pVal);
       [id(0x0000000d), propget, helpstring("property MaxDataLength")]
       HRESULT MaxDataLength([out, retval] long* pVal);
   };

   [
     odl,
     uuid(229A88F4-29B8-40BD-84A5-AE4BBFBB405D),
     helpstring("IWord Interface"),
     dual,
     oleautomation
   ]
   interface IWord : IDispatch {
       [id(0x00000001), propget, helpstring("property WordID")]
       HRESULT WordID([out, retval] long* pVal);
       [id(0x00000002), helpstring("method GetWord")]
       HRESULT GetWord([out, retval] BSTR* bstrWord);
       [id(0x00000003), propget, helpstring("property Count")]
       HRESULT Count([out, retval] long* pVal);
       [id(0x00000004), helpstring("method Item")]
       HRESULT Item(
                       [in] long Index,
                       [out, retval] IExplanation** ppExplanation);
       [id(0x00000005), helpstring("method GetExplanations")]
       HRESULT GetExplanations(
                       [out] SAFEARRAY(BSTR)* ppTagName,
                       [out] SAFEARRAY(BSTR)* ppText,
                       [out, retval] long* lCount);
   };

   [
     odl,
     uuid(BEA69C9B-EAB2-43DD-B2B3-692DE8A3FA17),
     helpstring("IExplanation Interface"),
     dual,
     oleautomation
   ]
   interface IExplanation : IDispatch {
       [id(0x00000001), propget, helpstring("property TagName")]
       HRESULT TagName([out, retval] BSTR* pVal);
       [id(0x00000002), propget, helpstring("property Text")]
       HRESULT Text([out, retval] BSTR* pVal);
   };

   [
     uuid(CDDDE045-19D9-4F24-8B56-8E578CF0DE8A),
     helpstring("DictionaryEx Class")
   ]
   coclass DictionaryEx {
       [default] interface IDictionaryEx;
   };

   [
     uuid(539B6BF1-58FF-4E3D-9DC5-8770A6C1964D),
     helpstring("Word Class")
   ]
   coclass Word {
       [default] interface IWord;
   };

   [
     uuid(AB6D4DB4-F66E-4315-AB52-AE676129F887),
     helpstring("Explanation Class")
   ]
   coclass Explanation {
       [default] interface IExplanation;
   };

   [
     uuid(92DEE298-0BCD-4CD1-BF7B-20348B4B5A2F),
     helpstring("Block Class")
   ]
   coclass Block {
       [default] interface IBlock;
   };

   [
     odl,
     uuid(0B9DB236-70EE-4264-978C-85DAF5237AF8),
     helpstring("IBlock Interface"),
     dual,
     oleautomation
   ]
   interface IBlock : IDispatch {
   };
};

使用方法:

                PrivateFontCollection  pf   =   new   PrivateFontCollection();
              
                 KSENGINELib.DictionariesClass dicts = new KSENGINELib.DictionariesClass();
                 string bst = @"D:\Program Files\Kingsoft\Powerword 2003\DICTS\PWDECMEC.DIC";
                 dicts.Add(bst);
                 KSENGINELib.Word tempW;
                tempW = dicts.Item(0).Lookup("alert", 0);
                Font temFont = this.richTextBox1.Font;               
                for (int i = 0; i < tempW.Count; i++)
                {
                   
                    string tagName = tempW.Item(i).TagName;
                    this.richTextBox1.AppendText(tagName);
                    this.richTextBox1.AppendText(": ");
                    if (tagName == "词典音标")
                    {
                       
                        pf.AddFontFile(filePath);//filepath为金山词霸音标字体文件所在

                     
                        this.richTextBox1.SelectionStart = this.richTextBox1.TextLength;
                        this.richTextBox1.SelectionLength = tempW.Item(i).Text.Length;
                        this.richTextBox1.SelectionFont = new Font(pf.Families[0], 12,
                                                FontStyle.Regular,
                                                 GraphicsUnit.Pixel);
                        this.richTextBox1.AppendText(tempW.Item(i).Text);
                       
                        this.richTextBox1.AppendText("\n");
                        this.richTextBox1.SelectionStart = this.richTextBox1.TextLength;
                        richTextBox1.ScrollToCaret();

                        this.richTextBox1.SelectionFont = temFont;
                       
                        //this.richTextBox1.Font = temFont;
                    }
                    else
                    {
                        this.richTextBox1.AppendText(tempW.Item(i).Text + "\n");
                       
                    }
                }

 欢迎光临http://www.5xiuxiu.cn/  淘宝购物导航 源码开源免费下载

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/tonywangzg/archive/2009/07/24/4378009.aspx

posted @ 2009-07-24 21:43  tonywangzg  阅读(663)  评论(0编辑  收藏  举报