zoukankan      html  css  js  c++  java
  • 记事本插件开发

    对插件开发不太熟,这个例子是最基础的一个

                  

    下面直接贴后台代码

    记事本Form1中

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Reflection;
    using System.IO;
    using 记事本.IEditPlus;
    namespace 记事本
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                //浏览某个路径下的文件夹,寻找dll文件
                //找到当前程序所在的目录并且和这个程序对应的文件夹拼接起来
                string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "LibOne");
                //从这个路径中获取所有的dll文件
                string[] files = Directory.GetFiles(path, "*.dll");
                //遍历所有的文件
                for (int i = 0; i < files.Length; i++)
                {
                    //获取这个路径下的所有的程序集
                   Assembly ass=  Assembly.LoadFile(files[i]);
                    Type Eidt= typeof(IEditp);//获取这个接口的Type
                    //获取这个程序集中公共的类型
                   Type[] tps = ass.GetExportedTypes();
                    //遍历所有公共的类型
                   for (int j = 0; j < tps.Length; j++)
                   {
                       //tps[i]能不能赋值给接口对象,并且不能是抽象
                       if (Eidt.IsAssignableFrom(tps[j])&&!tps[j].IsAbstract)
                       {
                           //创建接口类型的实例,强转了
                          IEditp iedit= (IEditp)Activator.CreateInstance(tps[j]);
                           //把插件的名字获取到
                          string name= iedit.Name;
                           //把插件功能的名字添加到菜单中
                         ToolStripItem tsi=  tsm.DropDownItems.Add(name);//主程序的菜单上就有这个功能的名字了
                           //把接口存到该菜单的tag中
                         tsi.Tag = iedit;
                           //注册一个点击事件
                         tsi.Click += new EventHandler(tsi_Click);
                       }
    
                   }
    
                }
            }
    
            void tsi_Click(object sender, EventArgs e)
            {
                //谁触发了这个方法
                ToolStripItem tsi=  sender as ToolStripItem;
                //从tag属性中获取接口
                IEditp edi= tsi.Tag as IEditp;
                //获取接口的方法,把文本框传进去,并且接收一下
               textBox1.Text= edi.ConvertString(textBox1);
            }
        }
    }
    Form1

    记事本.ConvertStringToupp中

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using 记事本.IEditPlus;
    namespace 记事本.ConvertStringToupp
    {
        public class ConvertToUpp:IEditp
        {
            public string ConvertString(System.Windows.Forms.TextBox tb)
            {
              return  tb.Text.ToUpper();
            }
    
            public string Name
            {
                get { return "转大写"; }
            }
        }
    }
    记事本.ConvertStringToupp

    记事本.IEditPlus中

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace 记事本.IEditPlus
    {
        public interface IEditp
        {
            string Name { get; }
            string ConvertString(TextBox tb);
        }
    }
    记事本.IEditPlus
  • 相关阅读:
    浅析Vue Router中关于路由守卫的应用以及在全局导航守卫中检查元字段
    react-native 项目配置ts运行环境
    #mobx应用在rn项目中
    react-native TextInput输入框输入时关键字高亮
    react-native-亲测可用插件
    nodejs+express实现图片上传
    cordova图片上传,视频上传(上传多个图片,多个视频)
    cordova图片上传,视频上传(上传单个图片,单个视频)
    移动端如何测试(前端,cordova)
    在mac上将apk包安装到android手机上
  • 原文地址:https://www.cnblogs.com/xbblogs/p/4819907.html
Copyright © 2011-2022 走看看