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. 两阶提交、三阶提交、TCC框架

    首先介绍一下分布式事务,分布式事务是指会涉及到操作多个数据库的事务.其实就是将对同一库事务的概念扩大到了对多个库的事务.目的是为了保证分布式系统中的数据一致性.分布式事务处理的关键是必须有一种方法可以 ...

  2. JENKINS中创建全局变量并在JOB中使用

    配置了一个 "PASSWORD"的变量值 然后再脚本里面使用   注意这里必须要用双引号 不然不行

  3. C语言读写二进制文件

    fseek用法 fseek用来移动文件指针.函数原型 int fseek(FILE * stream, long offset, int fromwhere); 参数解释: stream 是文件流指针 ...

  4. nim_duilib(9)之RichEdit

    introduction 更多控件用法,请参考 here 和 源码. 本文的代码基于这里 RichEdit的更多用法,请参考源码中RichEdit.h提供的函数,RichEdit控件,可以定制为多种多 ...

  5. 【LeetCode】104. Maximum Depth of Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 参考资料 日期 题目 ...

  6. 【LeetCode】1024. Video Stitching 解题报告(Python)

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

  7. 【九度OJ】题目1442:A sequence of numbers 解题报告

    [九度OJ]题目1442:A sequence of numbers 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1442 ...

  8. Gumbel distribution

    目录 概 主要内容 定义 Gumbel-Max trick Gumbel trick 用于归一化 代码 概 感觉这个分布的含义很有用啊, 能预测'最大', 也就是自然灾害, 太牛了. 主要内容 定义 ...

  9. <数据结构>XDOJ326.网络延时

    问题与解答 问题描述 有N个网络节点,标记为1到N. 给定一个二维数组times[M][3],表示信号经过有向边的传递时间.times[i][3] = {u, v, w}, 其中u是源节点,v是目标节 ...

  10. 编写Java程序,使用JDialog构造登录窗体

    返回本章节 返回作业目录 需求说明: 实现思路: 定义用户信息实体类User. 创建LoginDemoStart主类,初始化UI. 从UI获取用户信息并保存到User实体. 实现代码: