root/lang/csharp/Translater/Form1.cs

Revision 15264, 5.7 kB (checked in by seasons, 6 months ago)

翻訳エンジンプラグイン作成サンプル
実際に使ってみたい人は、bin/Debug/Jisho.exeを実行するとよろし。

Line 
1using System;
2using System.Collections.Generic;
3using System.Drawing;
4using System.IO;
5using System.Reflection;
6using System.Windows.Forms;
7using JishoPlugin;
8
9namespace Jisho
10{
11    public partial class Form1 : Form , IPluginHost
12    {
13        private List<JishoBase> engines_;
14        public Form1()
15        {
16            InitializeComponent();
17        }
18
19        private void Form1_Load(object sender, EventArgs e)
20        {
21            initializeWordControl();
22            loadPlugins();
23            setTranslateEngineList();
24        }
25
26        /// <summary>
27        /// プラグインが登録された時
28        /// </summary>
29        /// <param name="ipi"></param>
30        /// <returns></returns>
31        public bool Register(JishoBase ipi)
32        {
33            resultTextBox.AppendText( string.Format(@"Regist:{0}", ipi.Name) ) ;
34            resultTextBox.AppendText(Environment.NewLine);
35            return true;
36        }
37
38        #region 検索用語関係
39
40        /// <summary>
41        /// 用語の初期化
42        /// </summary>
43        void initializeWordControl()
44        {
45            wordTextBox.ForeColor = Color.Gray;
46            wordTextBox.Click += wordTextBox_Click;
47            wordTextBox.Leave += wordTextBox_Leave;
48            wordTextBox.Enter += wordTextBox_Enter;
49            wordTextBox.KeyDown += wordTextBox_KeyDown;
50        }
51
52        /// <summary>
53        /// 和訳作業
54        /// </summary>
55        /// <param name="sender"></param>
56        /// <param name="e"></param>
57        void wordTextBox_KeyDown(object sender, KeyEventArgs e)
58        {
59            if(e.KeyCode == Keys.Return)
60            {
61                resultTextBox.Clear();
62
63                if( String.IsNullOrEmpty(wordTextBox.Text))
64                    return;
65
66                // 取得した和訳結果をテキストボックスへ
67                foreach (string result in translate(wordTextBox.Text))
68                {
69                    resultTextBox.AppendText(result);
70                    resultTextBox.AppendText(Environment.NewLine);
71                }
72            }
73        }
74
75        /// <summary>
76        /// テキストボックスにフォーカス入った時
77        /// </summary>
78        /// <param name="sender"></param>
79        /// <param name="e"></param>
80        void wordTextBox_Enter(object sender, EventArgs e)
81        {
82            wordTextBox.ForeColor = Color.Black;
83        }
84
85        /// <summary>
86        /// テキストボックスからフォーカス外れた時
87        /// </summary>
88        /// <param name="sender"></param>
89        /// <param name="e"></param>
90        void wordTextBox_Leave(object sender, EventArgs e)
91        {
92            wordTextBox.ForeColor = Color.Gray;
93        }
94
95        /// <summary>
96        /// テキストボックスをクリックした時
97        /// </summary>
98        /// <param name="sender"></param>
99        /// <param name="e"></param>
100        void wordTextBox_Click(object sender, EventArgs e)
101        {
102            wordTextBox.SelectAll();
103        }
104
105        #endregion
106
107        #region 翻訳処理
108
109        /// <summary>
110        /// プラグインをロードする
111        /// </summary>
112        void loadPlugins()
113        {
114            string pwd = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
115            string loadpath = pwd + Path.DirectorySeparatorChar + "dll";
116
117            string[] dlls = Directory.GetFiles(loadpath, "*.dll");
118            string pluginInterface = typeof (IWebJisho).FullName;
119
120            engines_ = new List<JishoBase>();
121            foreach (string name in dlls)
122            {
123                Assembly asm = Assembly.LoadFrom(name);
124                Type[] types = asm.GetTypes();
125                foreach (Type t in types)
126                {
127                    if (t.IsClass && t.GetInterface(pluginInterface) != null)
128                    {
129                        JishoBase ipi = (JishoBase) asm.CreateInstance(t.FullName);
130                        engines_.Add(ipi);
131                        // ここでRegistメソッドが呼ばれて、ホスト側で実装した
132                        // Registが呼び出される。
133                        ipi.Host = this;
134                    }
135                }
136            }
137        }
138
139        /// <summary>
140        /// 翻訳
141        /// </summary>
142        /// <param name="word"></param>
143        /// <returns></returns>
144        private List<string> translate(string word)
145        {
146            JishoBase dic = engines_[engineLists.SelectedIndex];
147            if ( !dic.Translate(word) )
148                return new List<string>();
149            return dic.Means;
150        }
151
152        /// <summary>
153        /// 翻訳エンジンの設定
154        /// </summary>
155        void setTranslateEngineList()
156        {
157            foreach (JishoBase engine in engines_)
158            {
159                engineLists.Items.Add(engine);
160            }
161            if (engines_.Count > 0)
162                engineLists.SelectedIndex = 0;
163        }
164
165        #endregion
166
167        /// <summary>
168        /// 選択したときにアイコンを設定
169        /// </summary>
170        /// <param name="sender"></param>
171        /// <param name="e"></param>
172        private void engineLists_SelectedIndexChanged(object sender, EventArgs e)
173        {
174            if(engines_==null)
175                return;
176            if (engines_ != null)
177            {
178                // imageからiconへの変換
179                Bitmap bmp = (Bitmap) engines_[engineLists.SelectedIndex].Images[0];
180                Icon = Icon.FromHandle(bmp.GetHicon());
181            }
182        }
183    }
184}
Note: See TracBrowser for help on using the browser.