using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonLib;
using System.Windows.Forms;
using DevExpress.XtraTab;
using DevExpress.Utils;
using System.Runtime.InteropServices;
using CommonLib.utils;
namespace Lskj.Main
{
public class ModuleManager
{
#region C#DLL相关调用
///
/// 添加到选项卡
///
///
///
///
///
public static void AddModuleToTab(XtraTabControl TabMain, DllModule module, string[] obj, bool IsShowClose)
{
//if (PubUtil.ModuleForms.ContainsKey(module.Id))
//{
// foreach (XtraTabPage item in TabMain.TabPages)
// {
// string id = item.Tag + "";
// if (id == module.Id)
// {
// TabMain.SelectedTabPage = item;
// break;
// }
// }
//}
//else
//{
try
{
IForm iform = PubUtil.LoadDllForm(module.DllName, obj);
Form form = iform.SubForm;
module.ModuleForm = form;
string guid = Guid.NewGuid().ToString();
XtraTabPage tp = new XtraTabPage();
tp.Text = module.Name;
//tp.Tag = module.Id;
tp.Tag = guid;
form.FormBorderStyle = FormBorderStyle.None;
//这个必须有不然会提示:"不能向tabControl中添加顶级控件"
form.TopLevel = false;
form.Dock = DockStyle.Fill;
//这个必须有,不然显示不出来
tp.Controls.Add(form);
if (IsShowClose)
tp.ShowCloseButton = DefaultBoolean.True;
TabMain.TabPages.Add(tp);
TabMain.SelectedTabPage = tp;
module.Tag = "1";
form.Show();
PubUtil.ModuleForms.Add(guid, module);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
//}
}
///
/// Show模式打开
///
/// 模块信息
/// 参数
public static void AddModuleToShow(DllModule module, string[] args)
{
if (PubUtil.ModuleForms.ContainsKey(module.Id))
{
Form form = PubUtil.ModuleForms[module.Id].ModuleForm;
if (form == null)
return;
form.Text = module.Name;
form.WindowState = FormWindowState.Normal;
form.Activate();
form.BringToFront();
form.Focus();
form.Show();
}
else
{
IForm iform = PubUtil.LoadDllForm(module.DllName, args);
if (iform == null)
return;
Form form = iform.SubForm;
module.Tag = "1";
module.ModuleForm = form;
form.Show();
PubUtil.ModuleForms.Add(module.Id, module);
}
}
#endregion
#region Delphi相关调用
///
/// Delphi-Show模式打开
///
///
public static void AddModuleDelphiToShow(DllModule module)
{
if (PubUtil.ModuleForms.ContainsKey(module.Id))
{
Form iform = PubUtil.ModuleForms[module.Id].ModuleForm;
iform.Text = module.Name;
iform.Activate();
iform.BringToFront();
iform.Focus();
iform.Show();
}
else
{
// StringBuilder result = PubUtil.LoadDelphiDllEx(module.DllName);
// if (result.ToString().IndexOf('@') != -1)
// {
// string[] str = result.ToString().Split('@');
// module.ModuleInPtr = new IntPtr(Convert.ToInt32(str[1]));
// module.Result = result;
// FrmWindow frm = new FrmWindow();
// frm.Text = module.Name;
// frm.ModuleIntPtr = module.ModuleInPtr;
// PubUtil.SetParent(module.ModuleInPtr, frm.Handle);
// frm.DModule = module;
// module.ModuleForm = frm;
// frm.Show();
// PubUtil.SetWindowPos(module.ModuleInPtr, 0, 0, 0, frm.Width, frm.Height - 36, 0);
// PubUtil.ModuleForms.Add(module.Id, module);
// }
}
}
///
/// Delphi-添加到选项卡
///
///
///
///
public static void AddModuleDelphiToTab(XtraTabControl TabMain, DllModule module, bool IsShowClose)
{
try
{
if (PubUtil.ModuleForms.ContainsKey(module.Id))
{
foreach (XtraTabPage item in TabMain.TabPages)
{
string id = item.Tag.ToString();
if (id == module.Id)
{
TabMain.SelectedTabPage = item;
break;
}
}
}
else
{
XtraTabPage tp1 = TabMain.TabPages[0];
XtraTabPage tp2 = new XtraTabPage();
tp2.Text = module.Name;
tp2.Tag = module.Id;
//StringBuilder result = PubUtil.LoadDelphiDllEx(module.DllName);
//if (result.ToString().IndexOf('@') != -1)
//{
// string[] str = result.ToString().Split('@');
// module.ModuleInPtr = new IntPtr(Convert.ToInt32(str[1]));
// module.Result = result;
// module.Tag = "2";
// if (!string.IsNullOrEmpty(result + ""))
// {
// TabMain.TabPages.Add(tp2);
// PubUtil.SetParent(module.ModuleInPtr, tp2.Handle);
// PubUtil.SetWindowPos(module.ModuleInPtr, 0, 0, 0, tp1.Width, tp1.Height, 0);
// if (IsShowClose)
// tp2.ShowCloseButton = DefaultBoolean.True;
// TabMain.SelectedTabPage = tp2;
// PubUtil.ModuleForms.Add(module.Id, module);
// }
//}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
///
/// 释放Delphi资源
///
///
public static void FreeDll(DllModule m)
{
try
{
if (m != null)
{
PubUtil.FreeDll(m.Result);
m.ModuleForm = null;
PubUtil.ModuleForms.Remove(m.Id);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
}
}