C#winform控件序列化,反序列化
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控件序列化,反序列化的更多相关文章
- 在WPF中使用WinForm控件方法
1. 首先添加对如下两个dll文件的引用:WindowsFormsIntegration.dll,System.Windows.Forms.dll. 2. 在要使用WinForm控 ...
- WPF 调用WinForm控件
WPF可以使用WindowsFormsHost控件做为容器去显示WinForm控件,类似的用法网上到处都是,就是拖一个WindowsFormsHost控件winHost1到WPF页面上,让后设置win ...
- WinForm控件TreeView 只部分节点显示 CheckBox
WinForm控件TreeView 只部分节点显示 CheckBox 用过asp.net的应该知道,要在treeview中实现上述功能可以使用ShowCheckBox 属性指定那些节点显示check ...
- Winform控件重写
Winform控件重写 因为最近的项目中越来越多的遇到了比较特殊的一些控件,有时候我们自己封装一下可能更加方便我们的使用,下面是我们项目中用到的,简单做一个记录. TextBox控件重写 主要的控制代 ...
- 通过WinForm控件创建的WPF控件无法输入的问题
今天把写的一个WPF程序发布到别的机器上执行,发现一个比较奇怪的问题:在那个机器上用英文输入法无法输入数字,非要切换到中文输入法才行:但在我的机器上却是好好的. 最开始以为是输入法的问题,弄了好一阵子 ...
- c#Winform控件总结
1. C# WinForm控件.自定义控件整理(大全) (http://www.cnblogs.com/top5/archive/2010/04/29/1724039.html) 2. c#窗体控件用 ...
- 在WPF中调用Winform控件
最近在项目中用到了人脸识别和指纹识别,需要调用外部设备和接口,这里就用到了在WPF中调用Winform控件. 第一步,添加程序集引用.System.Windows.Forms和WindowsForms ...
- C# 扩展方法奇思妙用高级篇六:WinForm 控件选择器
在Web开发中,jQuery提供了功能异常强大的$选择器来帮助我们获取页面上的对象.但在WinForm中,.Net似乎没有这样一个使用起来比较方便的选择器.好在我们有扩展方法,可以很方便的打造一个. ...
- WinForm控件使用文章收藏整理完成
对C# WinForm开发系列收集的控件使用方面进行整理, 加入了一些文章, 不断补充充实, 完善这方面. 基础 - 常用控件 C# WinForm开发系列 - CheckBox/Button/Lab ...
随机推荐
- M-SOLUTIONS Programming Contest 2021(AtCoder Beginner Contest 232) 题解
目录 G - Modulo Shortest Path H - King's Tour 因为偷懒就只写G和H的题解了. G - Modulo Shortest Path 首先可以观察到对于一条从点\( ...
- SpringBoot 整合es(elasticsearch)使用elasticsearch-rest-high-level-client实现增删改
引入依赖 <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok< ...
- 【LeetCode】339. Nested List Weight Sum 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 dfs 日期 题目地址:https://leetcod ...
- 【LeetCode】908. Smallest Range I 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学计算 日期 题目地址:https://leetc ...
- 【LeetCode】598. Range Addition II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- Windows下安装配置MySQL
Windows下安装配置MySQL的基本步骤 一.MySQL下载 MySQL官方下载地址https://dev.mysql.com/downloads/mysql/5.7.html#downloads ...
- 《MYSQL高级查询与编程》综合机试试卷 - 云南农职美和易思
题目:银行mysql数据库系统管理 目录 题目:银行mysql数据库系统管理 一.语言和环境 二.题目(100分): 功能需求: 要求: 三.提交方式 四.评分标准: 五.实现代码: 创建表结构: 插 ...
- HTML多端适应 响应式布局案例
HTML布局: <div id="one"> <div class="aa"></div> <div class=&q ...
- 【JPA】Spring Data JPA 实现分页和条件查询
文章目录 1.在`Repository`层继承两个接口 2.在Service层进行查询操作 3.Page的方法 1.在Repository层继承两个接口 JpaRepository<Admin, ...
- Ubuntu16.04下,rabbimq集群搭建
rabbitmq作为企业级的消息队列,功能很齐全,既可以作为单一的部署模式,又可以做集群的部署模式 单一部署就不说了,就是在一台服务器上部署rabbitmq消息队列,可以参考我的博客:Ubuntu16 ...