using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using System.Reflection;
namespace menuTest
{
public partial class frmMain : Form
{
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath);
private Control AppForm;
private string PATH = "d:/ERP.ini";
public frmMain()
{
InitializeComponent();
}
//public string iniRead(string section, string key, string path)
//{
// StringBuilder temstring = new StringBuilder(255);
// int i = GetPrivateProfileString(section, key, "", temstring, 255, path);
// return temstring.ToString();
//}
private void ssToolStripMenuItem_Click(object sender, EventArgs e)
{
NameValueCollection values = new NameValueCollection();
ReadSectionValues("MENU", values);
ToolStripMenuItem tsi = new ToolStripMenuItem();
System.Reflection.Assembly assembly = null;
ToolStripMenuItem tsitem;
for (int i = 0; i < values.Count; i++)
{
string [] value = values.GetValues(i);
assembly = System.Reflection.Assembly.LoadFile("d:/" + value[0]);
AppForm = (Control)assembly.CreateInstance("ctlMenu.uControl1");
tsitem = (ToolStripMenuItem)(((MenuStrip)AppForm.Controls[0]).Items[0]);
tsi.DropDownItems.Add(tsitem);
}
tsi.Text = "MENU";
this.menuStrip1.Items.Add(tsi);
生成个菜单项实例,作为主菜单项。
//ToolStripMenuItem tsi = new ToolStripMenuItem();
//System.Reflection.Assembly assembly = null;
存放读取的INI文件内容
//string value = "";
读取INI文件
//value = iniRead("MENU", "TEST1", "d:/ERP.ini");
添加引用
//assembly = System.Reflection.Assembly.LoadFile("d:/" + value);
创建实例
//AppForm = (Control)assembly.CreateInstance("ctlMenu.uControl1");
将DLL中的MenuStrip作为单个菜单项
//ToolStripMenuItem tsitem = (ToolStripMenuItem)(((MenuStrip)AppForm.Controls[0]).Items[0]);
将菜单项放入子菜单中
tsi.DropDownItems.Add(((MenuStrip)AppForm.Controls[0]).Items[0].Text.ToString());
//tsi.DropDownItems.Add(tsitem);
//tsi.Text = "MENU";
//this.menuStrip1.Items.Add(tsi);
}
//读取指定INI文件
private string ReadString(string Section, string Ident, string Default,string path)
{
Byte[] Buffer = new Byte[65535];
int bufLen = GetPrivateProfileString(Section, Ident, Default, Buffer, Buffer.GetUpperBound(0), path);
//必须设定0(系统默认的代码页)的编码方式,否则无法支持中文
string s = Encoding.GetEncoding(0).GetString(Buffer);
s = s.Substring(0, bufLen);
return s.Trim();
}
private void GetStringsFromBuffer(Byte[] Buffer, int bufLen, StringCollection Strings)
{
Strings.Clear();
if (bufLen != 0)
{
int start = 0;
for (int i = 0; i < bufLen; i++)
{
if ((Buffer[i] == 0) && ((i - start) > 0))
{
String s = Encoding.GetEncoding(0).GetString(Buffer, start, i - start);
Strings.Add(s);
start = i + 1;
}
}
}
}
//从Ini文件中,将指定的Section名称中的所有Ident添加到列表中
private void ReadSection(string Section, StringCollection Idents)
{
Byte[] Buffer = new Byte[16384];
int bufLen = GetPrivateProfileString(Section, null, null, Buffer, Buffer.GetUpperBound(0), PATH);
//对Section进行解析
GetStringsFromBuffer(Buffer, bufLen, Idents);
}
//读取指定的Section的所有Value到列表中
private void ReadSectionValues(string Section, NameValueCollection Values)
{
StringCollection KeyList = new StringCollection();
ReadSection(Section, KeyList);
Values.Clear();
foreach (string key in KeyList)
{
Values.Add(key, ReadString(Section, key, "", PATH));
}
}
private void ddToolStripMenuItem_Click(object sender, EventArgs e)
{
//调用DLL中函数
//Form2 frm2 = new Form2();
//string value = "";
读取INI文件
//value = iniRead("MENU", "TEST1", "d:/ERP.ini");
加引用
//Assembly ass = Assembly.LoadFrom("d:/" + value);
取类的类型
//Type type = ass.GetType("ctlMenu.uControl1");
实例此类
//Object obj = Activator.CreateInstance(type);
获取此类的方法,frmShow为类的方法名称
//MethodInfo mi = type.GetMethod("frmShow");
//Object[] obj1 ={ (Object)frm2 };
//frm2.MdiParent = this;
使用方法
//mi.Invoke(obj, obj1);
}
}
}
***********************************************************************************************************
INI文件:
[MENU]
TEST1=ctlMenu.dll
TEST2=aa.dll