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 ...
随机推荐
- MLNX网卡驱动安装
安装/升级MLNX驱动 1. 安装准备 驱动下载地址:https://www.mellanox.com/products/ethernet-drivers/linux/mlnx_en 选择和系统版本匹 ...
- mail如何在linux中发送邮件,使用163邮箱发信。
如何在linux中发送邮件,使用163邮箱发信. linux中,可以使用mail命令往外发送邮件,在使用前,只需要指定如下简单配置即可,这里演示用 163.com 邮箱发送至 qq.com ...
- C# 金额数字转中文的方法
/// <summary> /// 金额数字转大写(带小数点) /// </summary> public static string PriceToCn(decimal pr ...
- IDEA配置连接(自建Maven仓库)私服并打包上传
maven的setting.xml文件配置 在servers标签里配置 <server> <id>privete_maven</id> <!--账号密码需要与 ...
- 逆波兰(非与或)表达式原理及C++代码实现
p.p1 { margin: 0; font: 11px Menlo; color: rgba(209, 47, 27, 1); background-color: rgba(255, 255, 25 ...
- 【LeetCode】311. Sparse Matrix Multiplication 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 科学计算库numpy 日期 题目地址:https ...
- 【九度OJ】题目1190:大整数排序 解题报告
[九度OJ]题目1190:大整数排序 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1190 题目描述: 对N个长度最长可达 ...
- Capstone CS5265规格书|CS5265参数|TYPEC转HDMI音视频转换拓展坞芯片
一.CS5265总概 Capstone CS5265 USB Type-C到HDMI转换器结合了USB Type-C输入接口和数字高清多媒体接口(HDMI)输出.嵌入式微控制器(MCU)基于工业标准8 ...
- Redisson分布式锁学习总结:可重入锁 RedissonLock#lock 获取锁源码分析
原文:Redisson分布式锁学习总结:可重入锁 RedissonLock#lock 获取锁源码分析 一.RedissonLock#lock 源码分析 1.根据锁key计算出 slot,一个slot对 ...
- 使用子查询获取,使用 all 关键字获取比所有“国内短线游”价格高的线路信息,按照线路类型、线路价格升序显示线路编号、线路名和价格
查看本章节 查看作业目录 需求说明: 使用子查询获取"国内短线游"及"国内长线游"的线路信息,按照线路类型.线路价格升序显示线路编号.线路名和价格 使用 all ...