110,536
社区成员
发帖
与我相关
我的任务
分享
namespace WangDa.LeasedLine.WinForm.Print
{
/// <summary>
///保存设计器中的信息
///在保存文件的时候反它序列化后保存
/// </summary>
[Serializable()]
public class ObjectSerializer
{
/// <summary>
/// 要将设计的对像保存到文件的信息
/// </summary>
/// <param name="objectSave">设计器中的对像列表</param>
/// <param name="width">设计器的宽度</param>
/// <param name="height">设计器的高度</param>
/// <param name="pconfig">打印设置</param>
public ObjectSerializer(DrawItemList objectSave, int width, int height, PrintConfig pconfig, List<PrintItem> printItems, int rowcount)
{
itemList = objectSave;
des_W = width;
des_H = height;
printconfig = pconfig;
_printItems = printItems;
_rowcount = rowcount;
}
private DrawItemList itemList;
private int des_W, des_H, _rowcount;
private PrintConfig printconfig;
private List<PrintItem> _printItems;
/// <summary>
/// 取得或设置每页有多少行
/// </summary>
public int RowCount
{
get
{
return _rowcount;
}
set
{
_rowcount = value;
}
}
/// <summary>
/// 取得或设计设计器中的对像列表
/// </summary>
public DrawItemList ItemList
{
get
{
return itemList;
}
set
{
itemList = value;
}
}
/// <summary>
/// 取得或设置设计器的宽度
/// </summary>
public int Width
{
get
{
return des_W;
}
set
{
des_W = value;
}
}
/// <summary>
/// 取得或设置设计器的高度
/// </summary>
public int Height
{
get
{
return des_H;
}
set
{
des_H = value;
}
}
/// <summary>
/// 存储打印设置
/// </summary>
public PrintConfig PrinterConfig
{
get
{
return printconfig;
}
set
{
printconfig = value;
}
}
/// <summary>
/// 存储打印列表
/// </summary>
public List<PrintItem> PrintItems
{
get
{
return _printItems;
}
set
{
_printItems = value;
}
}
}
}
namespace WangDa.LeasedLine.WinForm.Print
{
[Serializable()]
public class PrintItem : ISerializable
{
public PrintItem()
{ }
public bool Check
{
get;
set;
}
public string ColumnName
{
get;
set;
}
public string Caption
{
get;
set;
}
public object DrawItem
{
get;
set;
}
new public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("Check", this.Check);
info.AddValue("ColumnName", this.ColumnName);
info.AddValue("Caption", Caption);
info.AddValue("DrawItem", DrawItem);
}
public PrintItem(SerializationInfo info, StreamingContext context)
: this()
{
Check = (bool)info.GetValue("Check", typeof(bool));
ColumnName = info.GetString("ColumnName");
ColumnName = info.GetString("Caption");
ColumnName = info.GetString("DrawItem");
}
}
[code=C#]
namespace WangDa.LeasedLine.WinForm.Print.IO
{
public class InOutPut
{
public class UBinder : SerializationBinder
{
public override Type BindToType(string assemblyName, string typeName)
{
// 取得当前的程序集名称,取得传递过来,类型名称不修改,都是"TestPro.CSFTcpMsg"
string strCurAssembly = Assembly.GetExecutingAssembly().FullName;
// 返回当前对应的类型
Type CurType = Type.GetType(String.Format("{0}, {1}", typeName, strCurAssembly));
return CurType;
}
}
/// <summary>
/// 将设计器中的对像列表保存到文件中
/// </summary>
/// <param name="designer">所要保存的设计器</param>
/// <param name="fileName">要保存的文件名称</param>
public static byte[] SaveFile(Designer designer)
{
ObjectSerializer osave = new ObjectSerializer(designer.Items, designer.Width, designer.Height, designer.PrinterConfig, designer.PrintItems, designer.PageRows);
return SerializableObj<ObjectSerializer>.Serialize(osave);
}
/// <summary>
/// 打开保存的文件
/// 在打开保存的文件,此函数会返回保存的类
/// 在将类的数据返回给设计器的时候需刷新
/// 设计器和放在设计器的容器
/// 刷新设计器是将对像显示出来
/// 刷新设计器容器是重绘标尺
/// </summary>
/// <param name="file">打开的文件名称</param>
/// <returns>文件是否成功打开</returns>
public static bool OpenFile(Designer designer, byte[] fs)
{
bool opensuccess = false;
ObjectSerializer saveObject;
try
{
object o = (object)Deserizlize<ObjectSerializer>.Deserilize(fs);
saveObject = Deserizlize<ObjectSerializer>.Deserilize(fs) as ObjectSerializer;
designer.Items = saveObject.ItemList;
designer.Width = saveObject.Width;
designer.Height = saveObject.Height;
designer.PrinterConfig = saveObject.PrinterConfig;
designer.PrintItems = saveObject.PrintItems;
designer.PageRows = saveObject.RowCount;
designer.Refresh();
opensuccess = true;
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message, "出错提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
}
//formatter = null;
return opensuccess;
}
/// <summary>
/// 二进制序列化
/// </summary>
public class SerializableObj<T>
{
public static Byte[] Serialize(T obj)
{
try
{
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, obj);
ms.Position = 0;
byte[] buffers = ms.ToArray();
ms.Close();
ms.Dispose();
return Compress(buffers);
}
catch (Exception ex)
{
throw ex;
}
}
public static byte[] Compress(byte[] buffer)
{
MemoryStream ms = new MemoryStream();
GZipStream gzip = new GZipStream(ms, CompressionMode.Compress, true);
gzip.Write(buffer, 0, buffer.Length);
gzip.Close();
ms.Position = 0;
byte[] rebuffer = new byte[ms.Length];
ms.Read(rebuffer, 0, int.Parse(ms.Length.ToString()));
return rebuffer;
}
}
/// <summary>
/// 二进制反序列化
/// </summary>
public class Deserizlize<T>
{
public static byte[] Decompress(byte[] buffer)
{
try
{
MemoryStream ms = new MemoryStream(buffer);
ms.Position = 0;
GZipStream gzip = new GZipStream(ms, CompressionMode.Decompress);
byte[] rebuffer = new byte[100];
MemoryStream msre = new MemoryStream();
int readcont = gzip.Read(rebuffer, 0, rebuffer.Length);
while (readcont > 0)
{
msre.Write(rebuffer, 0, readcont);
readcont = gzip.Read(rebuffer, 0, rebuffer.Length);
}
return msre.ToArray();
}
catch (Exception ex)
{
throw ex;
}
}
public static object Deserilize(byte[] buffer)
{
try
{
byte[] debuffer = Decompress(buffer);
BinaryFormatter bf = new BinaryFormatter();
bf.Binder = new UBinder();
MemoryStream ms = new MemoryStream(debuffer);
ms.Position = 0;
object obj = (object)bf.Deserialize(ms);
return obj;
}
catch (Exception ex)
{
throw ex;
}
}
}
}