📄 mainform.cs
字号:
using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Diagnostics;using System.Threading;using System.IO;namespace DLTool{ /// <summary> /// Summary description for MainForm. /// </summary> public class MainForm : System.Windows.Forms.Form { [Serializable] private class Options { public System.Drawing.Font Font; public bool AutomaticallyUnMinimize; public bool AlwaysOnTop; public bool ShowBothChars; public Dictionary.MatchMode MatchMode; public string DefaultLanguage; public int width, height; /// <summary> /// Options set in this constructor serve as the defaults /// settings /// </summary> /// <param name="f"></param> public Options(Font f) { Font = f; AutomaticallyUnMinimize = true; AlwaysOnTop = true; MatchMode = Dictionary.MatchMode.Loose; DefaultLanguage = ""; } } private System.ComponentModel.IContainer components; private System.Windows.Forms.MainMenu mainMenu1; private System.Windows.Forms.MenuItem menuItem2; private System.Windows.Forms.MenuItem m_alwaysOnTopMenuItem; private System.Windows.Forms.MenuItem m_quitMenuItem; private System.Windows.Forms.MenuItem menuItem4; private System.Windows.Forms.MenuItem m_aboutMenuItem;
private System.Windows.Forms.RichTextBox m_richTextBox; private System.Windows.Forms.MenuItem m_fontMenuButton; private System.Windows.Forms.MenuItem m_exactMatchMenuItem; private System.Windows.Forms.MenuItem menuItem6; private System.Windows.Forms.MenuItem m_looseMatchMenuItem; private System.Windows.Forms.MenuItem m_subMatchMenuItem; private System.Windows.Forms.MenuItem m_subphraseMatchMenuItem; private System.Windows.Forms.MenuItem m_saveOptionsMenuItem; private System.Windows.Forms.MenuItem m_reverseLookupMenuItem;
private System.Windows.Forms.MenuItem m_dictsMenuItem;
private System.Windows.Forms.MenuItem m_manageDictionariesMenuItem; private System.Windows.Forms.MenuItem menuItem8;
private System.Windows.Forms.MenuItem m_enableMenuItem; private System.Runtime.Serialization.IFormatter m_formatter;
/// <summary>
/// Keeps track of the dictionary definitions loaded. Used when
/// changing dictionaries
/// </summary> private ArrayList m_dictDefs;
private DictionaryDefinition m_currentDictDef; private Dictionary m_dictionary; private Dictionary.MatchMode m_matchMode = Dictionary.MatchMode.Loose; private Thread m_pollingThread; private bool m_enabled = true;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem m_pronounciationMenuItem;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.MenuItem df_menuShowBothToggle;
private System.Windows.Forms.MenuItem m_popupOnLookupMenuItem; private int m_pollRate = 500; // ================= FUNCTIONAL STUFF ===================== /// <summary> /// Entry point for a thread that periodically polls the clipboard /// for new words to lookup. Keeps track of the last word it looked /// up and only looks up a new word when the contents of the clipboard /// have changed. /// </summary> private void PollClipboard() { string lastText = ""; while(true) { if( m_enabled ) { string text = GetClipText(); if( text != lastText && text != string.Empty ) { Lookup(text); // If the user has enabled the automatic-un-minimize // option, then un-minimize the DLTool window to // display the results of the lookup if( this.WindowState == FormWindowState.Minimized && m_popupOnLookupMenuItem.Checked == true ) { this.WindowState = FormWindowState.Normal; } } lastText = text; } Thread.Sleep(m_pollRate); } } public string GetClipText() { // Create a new instance of the DataObject interface. IDataObject data = Clipboard.GetDataObject(); // If the data is text, then set the text of the // TextBox to the text in the Clipboard. if ( data.GetDataPresent(DataFormats.UnicodeText)) { string clipText; clipText = data.GetData(DataFormats.UnicodeText).ToString(); clipText = clipText.Trim(); return clipText; } return string.Empty; } public void Lookup(string text) { if( ! m_enabled ) return; if( ! m_dictionary.CanLookup ) { Trace.WriteLine("Dictionary not ready. can't lookup [" + text + "]", "DLTool"); return; } Trace.WriteLine("Looking up [" + text + "]", "DLTool"); if( text.Equals( string.Empty ) ) return; ArrayList results = m_dictionary.Lookup(text, m_matchMode); string toPrint = text + " -- not found in dictionary"; if( results.Count > 0 ) { toPrint = ""; foreach( object o in results ) { toPrint += o.ToString() + "\n"; } } m_richTextBox.Text = toPrint; } private void LoadDictionaries() { // first, empty the dictionary menu in case we've already loaded // some dictionaries before. while( this.m_dictsMenuItem.MenuItems[0].Text != "-" ) { this.m_dictsMenuItem.MenuItems.RemoveAt(0); } FileStream fs = null; try { Trace.WriteLine("Loading dictionary definition file dicts.ini", "DLTool"); fs = new FileStream("dicts.ini", FileMode.Open); m_dictDefs = m_formatter.Deserialize(fs) as ArrayList; } catch(Exception xcp) { string err = "Error opening dictionary definition file dicts.ini!"; Trace.WriteLine(err + "\n" + xcp.ToString(), "DLTool"); System.Windows.Forms.MessageBox.Show(err); m_dictDefs = new ArrayList(); } finally { if( fs != null ) fs.Close(); } Shortcut[] shortcuts = { Shortcut.Ctrl1, Shortcut.Ctrl2, Shortcut.Ctrl3, Shortcut.Ctrl4, Shortcut.Ctrl5, Shortcut.Ctrl6, Shortcut.Ctrl7, Shortcut.Ctrl8, Shortcut.Ctrl9, Shortcut.Ctrl0 }; bool errorShown = false; int count = 0; // okay, now we have our dictionary definitions. Check that they // are all valid and create the corresponding menu options foreach( object o in m_dictDefs ) { DictionaryDefinition def = o as DictionaryDefinition; if ( def == null && ! errorShown ) { string err = "Corrupt dictionary definition file! Trying to recover..."; System.Windows.Forms.MessageBox.Show(err); continue; } Trace.WriteLine( "Loading dictionary definition [" + def.Language + ", " + def.Filename + "]", "DLTool"); if( ! File.Exists( def.Filename ) ) { string err = "Couldn't find dictionary file " + def.Filename + "! Ignoring..."; System.Windows.Forms.MessageBox.Show( err ); continue; } MenuItem newMI = new System.Windows.Forms.MenuItem();
if (count < shortcuts.Length)
{
newMI.Shortcut = shortcuts[count];
newMI.ShowShortcut = true;
}
newMI.Text = def.Language;
newMI.Click += new EventHandler( this.DictionaryMenuItem_Click );// this.m_dictDefs.Add( def ); this.m_dictsMenuItem.MenuItems.Add( count, newMI ); count++; } } private void LoadOptions() { Options options; FileStream fs = null; try { Trace.WriteLine("Loading options file dltool.ini", "DLTool"); fs = new FileStream("dltool.ini", FileMode.Open); options = m_formatter.Deserialize(fs) as Options; } catch(FileNotFoundException) { // ignore Trace.WriteLine("Options file does not exist", "DLTool"); options = new Options(m_richTextBox.Font); } catch(System.Runtime.Serialization.SerializationException) { // ignore options = new Options(m_richTextBox.Font); } finally { if( fs != null ) fs.Close(); } m_richTextBox.Font = options.Font; m_alwaysOnTopMenuItem.Checked = options.AlwaysOnTop; this.TopMost = options.AlwaysOnTop; m_popupOnLookupMenuItem.Checked = options.AutomaticallyUnMinimize; df_menuShowBothToggle.Checked = options.ShowBothChars; m_dictionary.SetBothChars(df_menuShowBothToggle.Checked); this.SetMatchMode(options.MatchMode);
//On first run, it doesn't look too pretty if the dimensions are all Zero.
if (options.width < 100 || options.height < 100)
{ this.Width = 400; this.Height = 175; }
else
{ this.Width = options.width; this.Height = options.height; } bool found = false; // load the default dictionary, if there is one if( options.DefaultLanguage != null && options.DefaultLanguage.Length > 0 ) { for( int i=0; i<m_dictDefs.Count && ! found; i++ ) { DictionaryDefinition def = m_dictDefs[i] as DictionaryDefinition; Debug.Assert( def != null ); if( def.Language.Equals( options.DefaultLanguage ) ) { this.SetDictionary( def ); found = true; } } } else { // otherwise, just load the first dictionary, if there is one if( m_dictDefs.Count > 0 ) this.SetDictionary( m_dictDefs[0] as DictionaryDefinition ); } Trace.WriteLine("Font: " + options.Font.ToString(), "DLTool"); Trace.WriteLine("AlwaysOnTop: " + options.AlwaysOnTop.ToString(), "DLTool"); } private void SaveOptions() { Options options = new Options(m_richTextBox.Font); options.AlwaysOnTop = this.TopMost; options.AutomaticallyUnMinimize = m_popupOnLookupMenuItem.Checked; options.ShowBothChars = df_menuShowBothToggle.Checked; options.height = this.Height; options.width = this.Width; if( m_currentDictDef != null ) options.DefaultLanguage = m_currentDictDef.Language; else options.DefaultLanguage = ""; options.MatchMode = m_matchMode; try { FileStream fs = new FileStream("dltool.ini", FileMode.Create); m_formatter.Serialize(fs, options); fs.Close(); } catch(System.Runtime.Serialization.SerializationException) { Trace.WriteLine("Error saving options!", "DLTool"); } } private void SetDictionary( DictionaryDefinition def ) { if( m_currentDictDef == def ) return; Debug.Assert( m_dictionary != null ); int index = 0; for( index = 0; index < m_dictDefs.Count; index++ ) if( m_dictDefs[index] == def ) break; Debug.Assert( m_dictDefs.Count != index ); try { m_dictionary.LoadFile( def.Filename, def.CodePage ); for( int i = 0; i < this.m_dictDefs.Count; i++ ) { if( i == index ) m_dictsMenuItem.MenuItems[ i ].Checked = true; else m_dictsMenuItem.MenuItems[ i ].Checked = false; } } catch( FileLoadException ) { MessageBox.Show("Bad dictionary file - " + def.Filename ); } if (m_dictionary.CodePage == 65001) df_menuShowBothToggle.Enabled = true; else df_menuShowBothToggle.Enabled = false; this.m_currentDictDef = def; } private void SetMatchMode(Dictionary.MatchMode m) { m_matchMode = m; m_exactMatchMenuItem.Checked = (m == Dictionary.MatchMode.Exact); m_looseMatchMenuItem.Checked = (m == Dictionary.MatchMode.Loose); m_subphraseMatchMenuItem.Checked = (m == Dictionary.MatchMode.Subphrases); m_subMatchMenuItem.Checked = (m == Dictionary.MatchMode.Subchars); m_reverseLookupMenuItem.Checked = (m == Dictionary.MatchMode.ReverseLookup); m_pronounciationMenuItem.Checked = (m == Dictionary.MatchMode.Pronounciation); } // ============= AUTO GENERATED AND SETUP =============== /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } m_pollingThread.Abort(); m_pollingThread.Join(); } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MainForm));
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.m_dictsMenuItem = new System.Windows.Forms.MenuItem();
this.menuItem8 = new System.Windows.Forms.MenuItem();
this.m_manageDictionariesMenuItem = new System.Windows.Forms.MenuItem();
this.m_quitMenuItem = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.m_alwaysOnTopMenuItem = new System.Windows.Forms.MenuItem();
this.m_popupOnLookupMenuItem = new System.Windows.Forms.MenuItem();
this.m_fontMenuButton = new System.Windows.Forms.MenuItem();
this.menuItem6 = new System.Windows.Forms.MenuItem();
this.m_exactMatchMenuItem = new System.Windows.Forms.MenuItem();
this.m_looseMatchMenuItem = new System.Windows.Forms.MenuItem();
this.m_subphraseMatchMenuItem = new System.Windows.Forms.MenuItem();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -