using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Windows.Forms;
using System.Threading.Tasks; namespace WF.Serialize
{
/*
* 序列化控件核心代码
*
*/
[Serializable]
public class Label_x : Label, ISerializable
{
public Label_x() { } /// <summary>
/// 反序列化构造函数
/// </summary>
/// <param name= "info "> </param>
/// <param name= "context "> </param>
public Label_x(SerializationInfo info, StreamingContext context)
{
this.SetObjectDataEx(info, context);
this.BorderStyle = (BorderStyle)info.GetValue("BorderStyle", typeof(BorderStyle)); //新加的属性放新线程里,因为缓存数据没有该字段GetValue会报错
//放新线程里面,报错不会影响主线程
//可能会照成加载变慢,该控件越多越慢
//打印或保存模板,会存到缓存,下次打开就不会慢
//Task.Factory.StartNew(new Action(() => { try
{
this.TextAlign = (ContentAlignment)info.GetValue("TextAlign", typeof(ContentAlignment));
}
catch
{
} //})); }
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
this.GetObjectDataEx(info, context);
info.AddValue("BorderStyle", this.BorderStyle);
info.AddValue("TextAlign", this.TextAlign);
}
} [Serializable]
public class TextBox_x : TextBox, ISerializable
{
public TextBox_x() { }
public TextBox_x(SerializationInfo info, StreamingContext context)
{
this.SetObjectDataEx(info, context);
this.BorderStyle = (BorderStyle)info.GetValue("BorderStyle", typeof(BorderStyle));
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
this.GetObjectDataEx(info, context);
info.AddValue("BorderStyle", this.BorderStyle);
}
} [Serializable]
public class Pane_x : Panel, ISerializable
{
public Pane_x() { }
public Pane_x(SerializationInfo info, StreamingContext context)
{
this.SetObjectDataEx(info, context); }
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
this.GetObjectDataEx(info, context); info.AddValue("BorderStyle", this.BorderStyle); }
} /// <summary>
/// 序列化管理类
/// </summary>
public static class SerializeManager
{ public static List<Control> _controls = new List<Control>(); static BinaryFormatter form = new BinaryFormatter();
/// <summary>
/// 序列化保存到文件
/// </summary>
/// <param name="serializable"></param>
/// <param name="path"></param>
public static void SerializeSave(this ISerializable serializable, string path)
{
using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write))
{
form.Serialize(fs, serializable);
}
} /// <summary>
/// 反序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="path"></param>
/// <returns></returns>
public static T DeSerialize<T>(string path) where T : class, ISerializable
{
_controls.Clear();
using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
return form.Deserialize(fs) as T;
}
}
public static void SetObjectDataEx(this ISerializable serializable, SerializationInfo info, StreamingContext context)
{
var control = serializable as Control;
control.Name = info.GetString("Name")==""? Guid.NewGuid().ToString(): info.GetString("Name");
control.Size = (Size)info.GetValue("Size", typeof(Size));
control.Location = (Point)info.GetValue("Location", typeof(Point));
control.Font = (Font)info.GetValue("Font", typeof(Font));
control.Text = (string)info.GetValue("Text", typeof(string));
control.Tag = info.GetValue("Tag", typeof(object));
control.BackColor = (Color)info.GetValue("BackColor", typeof(Color));
control.ForeColor = (Color)info.GetValue("ForeColor", typeof(Color));
var controls = (List<ISerializable>)info.GetValue("Controls", typeof(List<ISerializable>));
control.Controls.AddRange(controls.Cast<Control>().ToArray());
_controls.Add(control);
} public static void GetObjectDataEx(this ISerializable serializable, SerializationInfo info, StreamingContext context)
{
var control = serializable as Control;
info.AddValue("Name", control.Name);
info.AddValue("Size", control.Size);
info.AddValue("Location", control.Location);
info.AddValue("Font", control.Font);
info.AddValue("Text", control.Text);
info.AddValue("Tag", control.Tag);
info.AddValue("BackColor", control.BackColor);
info.AddValue("ForeColor", control.ForeColor); var controls = new List<ISerializable>();
foreach (var item in control.Controls.Cast<Control>())
{
if (item is ISerializable)
{
controls.Add((item as ISerializable));
}
}
info.AddValue("Controls", controls);
}
}
}

C#winform控件序列化,反序列化的更多相关文章

  1. 在WPF中使用WinForm控件方法

    1.      首先添加对如下两个dll文件的引用:WindowsFormsIntegration.dll,System.Windows.Forms.dll. 2.      在要使用WinForm控 ...

  2. WPF 调用WinForm控件

    WPF可以使用WindowsFormsHost控件做为容器去显示WinForm控件,类似的用法网上到处都是,就是拖一个WindowsFormsHost控件winHost1到WPF页面上,让后设置win ...

  3. WinForm控件TreeView 只部分节点显示 CheckBox

    WinForm控件TreeView 只部分节点显示  CheckBox 用过asp.net的应该知道,要在treeview中实现上述功能可以使用ShowCheckBox 属性指定那些节点显示check ...

  4. Winform控件重写

    Winform控件重写 因为最近的项目中越来越多的遇到了比较特殊的一些控件,有时候我们自己封装一下可能更加方便我们的使用,下面是我们项目中用到的,简单做一个记录. TextBox控件重写 主要的控制代 ...

  5. 通过WinForm控件创建的WPF控件无法输入的问题

    今天把写的一个WPF程序发布到别的机器上执行,发现一个比较奇怪的问题:在那个机器上用英文输入法无法输入数字,非要切换到中文输入法才行:但在我的机器上却是好好的. 最开始以为是输入法的问题,弄了好一阵子 ...

  6. c#Winform控件总结

    1. C# WinForm控件.自定义控件整理(大全) (http://www.cnblogs.com/top5/archive/2010/04/29/1724039.html) 2. c#窗体控件用 ...

  7. 在WPF中调用Winform控件

    最近在项目中用到了人脸识别和指纹识别,需要调用外部设备和接口,这里就用到了在WPF中调用Winform控件. 第一步,添加程序集引用.System.Windows.Forms和WindowsForms ...

  8. C# 扩展方法奇思妙用高级篇六:WinForm 控件选择器

    在Web开发中,jQuery提供了功能异常强大的$选择器来帮助我们获取页面上的对象.但在WinForm中,.Net似乎没有这样一个使用起来比较方便的选择器.好在我们有扩展方法,可以很方便的打造一个. ...

  9. WinForm控件使用文章收藏整理完成

    对C# WinForm开发系列收集的控件使用方面进行整理, 加入了一些文章, 不断补充充实, 完善这方面. 基础 - 常用控件 C# WinForm开发系列 - CheckBox/Button/Lab ...

随机推荐

  1. CF200B Drinks 题解

    Content 有 \(n\) 杯饮料,第 \(i\) 杯饮料中橙汁占 \(a_i\%\).现在请求出这 \(n\) 杯饮料混合成一杯饮料后的橙汁所占百分比. 数据范围:\(1\leqslant n\ ...

  2. CF918B Radio Station 题解

    Content 有 \(n\) 个形如 \(a_i.b_i.c_i.d_i\) 的 IP 地址.有 \(m\) 条命令,每条命令由一条字符串 \(s\) 和一个形如 \(p.q.r.s\) 的 IP ...

  3. CF1497A Meximization 题解

    Content 给定 \(n\) 个数 \(a_1,a_2,\dots,a_n\),你需要将这些数重新排列,使得 \(\sum\limits_{i=1}^n\operatorname{mex}(a_1 ...

  4. Chrome的强大搜索功能

    前言 前几天一个好朋友求助我,大概问题是他的电脑QQ啥都能上网,就浏览器上不了网不是IE而是chrome,我第一反应可能是dns问题.后来发甩过来一张图,好家伙把我吓得,类似于下面这张图 这图是我自己 ...

  5. 【LeetCode】912. Sort an Array 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数排序 桶排序 红黑树排序 归并排序 快速排序 ...

  6. 【九度OJ】题目1078:二叉树遍历 解题报告

    [九度OJ]题目1078:二叉树遍历 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1078 题目描述: 二叉树的前序.中序.后序遍历 ...

  7. 【LeetCode】472. Concatenated Words 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  8. 【LeetCode】908. Smallest Range I 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学计算 日期 题目地址:https://leetc ...

  9. wordpress中遇到的问题

    在博客园申请了账号,也已经开始写了两篇内容,但还是想要有属于自己的小站.于是将域名续费了几年,又在我之前买的vps上搭建了一个wordpress博客站点,这样以后我就可以同时发布到两个地方. 根据教程 ...

  10. 如何在 Go 中将 []byte 转换为 io.Reader?

    原文链接: 如何在 Go 中将 []byte 转换为 io.Reader? 在 stackoverflow 上看到一个问题,题主进行了一个网络请求,接口返回的是 []byte.如果想要将其转换成 io ...