📄 23.9.txt
字号:
Listing 23.9 Creating a Text Editor with Plug-in Support
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Reflection;
using _11_NotepadPluginSDK;
namespace _11_NotepadWithPlugins
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem mnuNew;
private System.Windows.Forms.MenuItem mnuOpen;
private System.Windows.Forms.MenuItem mnuSave;
private System.Windows.Forms.MenuItem mnuSaveAs;
private System.Windows.Forms.MenuItem mnuExit;
private System.Windows.Forms.MenuItem mnuPlugins;
private System.Windows.Forms.MenuItem menuItem8;
private System.Windows.Forms.TextBox tbContent;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.ComponentModel.Container components = null;
private string fileName = null;
private ArrayList plugins;
public Form1()
{
InitializeComponent();
plugins = new ArrayList();
// enumerate files in plugin directory
foreach( string fileName in Directory.GetFiles(
Environment.CurrentDirectory + @”\plugins”, “*.dll” ))
{
// load assembly
Assembly asm = Assembly.LoadFile( fileName );
// get all modules in assembly
foreach( Module module in asm.GetModules(false) )
{
// for all types
foreach( Type t in module.GetTypes() )
{
// check to see if it supports the
// INotepadPlugin interface
foreach (Type iface in t.FindInterfaces(
new TypeFilter(PluginInterfaceFilter) ,
“INotepadPlugin” ))
{
// create plug-in and add to menu
INotepadPlugin plugin = (
INotepadPlugin)Activator.CreateInstance(t);
plugins.Add(plugin);
mnuPlugins.MenuItems.Add( plugin.Name,
new EventHandler(mnuPlugin_Click) );
}
}
}
}
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.tbContent = new System.Windows.Forms.TextBox();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.mnuNew = new System.Windows.Forms.MenuItem();
this.mnuOpen = new System.Windows.Forms.MenuItem();
this.mnuSave = new System.Windows.Forms.MenuItem();
this.mnuSaveAs = new System.Windows.Forms.MenuItem();
this.menuItem8 = new System.Windows.Forms.MenuItem();
this.mnuExit = new System.Windows.Forms.MenuItem();
this.mnuPlugins = new System.Windows.Forms.MenuItem();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.SuspendLayout();
// NOTE: Form initialization code removed for brevity
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
// callback used when searching for implemented interfaces
public static bool PluginInterfaceFilter( Type typeObj, object criteria )
{
if( typeObj.Name == (string) criteria )
return true;
return false;
}
// Called when user clicks on a plug-in menu item
private void mnuPlugin_Click(object sender, System.EventArgs e)
{
MenuItem item = (MenuItem) sender;
INotepadPlugin plugin = (INotepadPlugin) plugins[item.Index];
if( tbContent.SelectionLength > 0 )
tbContent.SelectedText = plugin.Execute( tbContent.SelectedText );
else
tbContent.Text = plugin.Execute( tbContent.Text );
}
// the following methods perform normal notepad menu functionality
private void mnuNew_Click(object sender, System.EventArgs e)
{
tbContent.Text = “”;
fileName = null;
}
private void mnuOpen_Click(object sender, System.EventArgs e)
{
if( openFileDialog1.ShowDialog() == DialogResult.Cancel )
return;
tbContent.Text = “”;
fileName = openFileDialog1.FileName;
OpenText( fileName );
}
private void mnuSave_Click(object sender, System.EventArgs e)
{
if( fileName == null )
{
mnuSaveAs_Click( sender, e );
return;
}
SaveText( fileName );
}
private void mnuSaveAs_Click(object sender, System.EventArgs e)
{
if( saveFileDialog1.ShowDialog() == DialogResult.OK )
{
SaveText( saveFileDialog1.FileName );
fileName = saveFileDialog1.FileName;
}
}
private void mnuExit_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void OpenText( string fileName )
{
StreamReader sr = File.OpenText(fileName);
tbContent.Text = sr.ReadToEnd();
sr.Close();
}
private void SaveText( string fileName )
{
StreamWriter sw = File.CreateText(fileName);
sw.Write( tbContent.Text);
sw.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -