下面这些实例都可以拷下直接用

总体思路:保存打印设置信息到本地文件,下次打印的时候直接读取文件信息,通过序列化与反序列化来获取值。本例只是针对打印的横纵向进行设置,读者也可以增加其他设置信息进行保存读取。


主方法MemoryPrint


using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.IO;
using System.Drawing; namespace VistaRenderer
{
class MemoryPrint
{
public MemoryPrint() { }
/// <summary>
/// 系统当前路径
/// </summary>
public static string GLOBALPATH = Application.StartupPath; /// <summary>
/// 是否横向
/// </summary>
public bool pagesetIsHorizon; /// <summary>
/// 是否第一次打印
/// </summary>
public bool isFirstP=true; /// <summary>
/// 主方法
/// </summary>
public void memoryPrint()
{
this.GetDate();
PrintDocument fPrintDocument = new PrintDocument();
fPrintDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage);// 打印内容
try
{
/** 第一次打印弹出设置框,以后都是从配置文件中读取 */
if (!isFirstP)
{
fPrintDocument.DefaultPageSettings.Landscape = pagesetIsHorizon;
}
else
{
//申明并实例化PrintDialog
PrintDialog pDlg = new PrintDialog();
//可以选定页
pDlg.AllowSomePages = true;
//指定打印文档
pDlg.Document = fPrintDocument;
//显示对话框
DialogResult result = pDlg.ShowDialog();
if (result == DialogResult.OK)
{
//保存打印设置
fPrintDocument = pDlg.Document;
pagesetIsHorizon = fPrintDocument.DefaultPageSettings.Landscape; }
}
//打印预览
PrintPreviewDialog ppd = new PrintPreviewDialog();
ppd.Document = fPrintDocument;
ppd.ShowDialog();
//打印
fPrintDocument.Print();
//保存设置到本地
this.SaveDate();
}
catch (System.Drawing.Printing.InvalidPrinterException e1)
{
MessageBox.Show("未安装打印机,请进入系统控制面版添加打印机!", "打印", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "打印", MessageBoxButtons.OK, MessageBoxIcon.Warning);
} } /// <summary>
/// 打印内容
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
Graphics g = e.Graphics; //获得绘图对象
e.Graphics.DrawString("打印啦。。。。",
new Font("Arial", 20, FontStyle.Bold), Brushes.Black, 150, 125);
} /// <summary>
/// 保存用户选择信息到文件data/data.dat中
/// </summary>
public void SaveDate()
{ ClsSaveData csd = null;
if (File.Exists(GLOBALPATH + "\\data\\data.dat"))
{
csd = ClsSaveData.deSerialize("data.dat", GLOBALPATH + "\\data\\");
}
else
{
csd = new ClsSaveData();
}
csd.pagesetIsHorizon = pagesetIsHorizon;
csd.isFirstP = false;
ClsSaveData.serialize("data.dat", GLOBALPATH + "\\data\\", csd);
} /// <summary>
/// 从data.dat文件取出用户选择项
/// </summary>
public void GetDate()
{
if (File.Exists(GLOBALPATH + "\\data\\data.dat"))
{
ClsSaveData csd = ClsSaveData.deSerialize("data.dat", GLOBALPATH + "\\data\\");
if (csd == null || csd.pagesetIsHorizon == null)
{ }
else
{
isFirstP = csd.isFirstP;
pagesetIsHorizon = csd.pagesetIsHorizon;
} } }
}
}

序列化的对象,保存纵横向设置信息


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections; namespace VistaRenderer
{
[Serializable]
class ClsSaveData
{
public bool pagesetIsHorizon;//是否横向 /// <summary>
/// 是否第一次打印
/// </summary>
public bool isFirstP = true; /// <summary>
/// 序列化该对象
/// </summary>
/// <param name="fileName"></param>
/// <param name="path"></param>
/// <param name="obj"></param>
public static void serialize(string fileName, string path, object obj)
{
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
ClsSerial serial = new ClsSerial(path + fileName, obj);
serial.SerializeNow();
} /// <summary>
/// 反序列化
/// </summary>
/// <param name="fileName"></param>
/// <param name="path"></param>
/// <returns></returns>
public static ClsSaveData deSerialize(string fileName, string path)
{
ClsSaveData csd = null;
if (File.Exists(path + fileName))
{
ClsSerial serial = new ClsSerial();
csd = (ClsSaveData)serial.DeSerializeNow(path + fileName);
}
return csd;
}
} }

序列化工具类


using System;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization; namespace VistaRenderer
{
class ClsSerial
{
/// <summary>
/// 路径
/// </summary>
private string path = "";
/// <summary>
/// 对象
/// </summary>
private object obj = null;
/// <summary>
/// 类型
/// </summary>
private Type type = null; public ClsSerial()
{
}
/// <summary>
/// 构造器
/// </summary>
/// <param name="path"></param>
/// <param name="obj"></param>
public ClsSerial(string path, object obj)
{
this.path = path;
this.obj = obj;
}
/// <summary>
/// 检查类型
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public bool checkObj(object obj)
{
bool check = false;
type = obj.GetType();
if (type.IsClass || type.Name == "struct" || type.IsEnum || type.Name == "delegate")
check = true;
return check;
}
/// <summary>
/// 序列化
/// </summary>
/// <param name="path"></param>
/// <param name="obj"></param>
public void SerializeNow(string path, object obj)
{
try
{
this.path = path;
this.obj = obj;
SerializeNow();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
/// <summary>
/// 序列化
/// </summary>
public void SerializeNow()
{
try
{
if (!checkObj(obj))
{
MessageBox.Show("该对象不可序列化!", "系统错误");
return;
}
FileStream fileStream = new FileStream(path, FileMode.Create);
BinaryFormatter b = new BinaryFormatter();
b.Serialize(fileStream, obj);
fileStream.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
/// <summary>
/// 反序列化
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public object DeSerializeNow(string path)
{
try
{
this.path = path;
return DeSerializeNow();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
/// <summary>
/// 反序列化
/// </summary>
/// <returns></returns>
public object DeSerializeNow()
{
try
{
object obj = null;
FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
BinaryFormatter b = new BinaryFormatter();
obj = b.Deserialize(fileStream);
fileStream.Close();
return obj;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
/// <summary>
/// xml序列化
/// </summary>
/// <param name="path"></param>
/// <param name="obj"></param>
public void SerializeXMLNow(string path, object obj)
{
try
{
this.path = path;
this.obj = obj;
SerializeXMLNow();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
/// <summary>
/// xml序列化
/// </summary>
public void SerializeXMLNow()
{
try
{
if (!checkObj(obj))
{
MessageBox.Show("该对象不可序列化!", "系统错误");
return;
}
FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
XmlSerializer xmlFormatter = new XmlSerializer(type);
xmlFormatter.Serialize(fileStream, obj);
fileStream.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
/// <summary>
/// xml反序列化
/// </summary>
/// <param name="path"></param>
/// <param name="obj"></param>
/// <returns></returns>
public object DeSerializeXMLNow(string path, object obj)
{
try
{
this.path = path;
this.obj = obj;
return DeSerializeXMLNow();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
/// <summary>
/// xml反序列化
/// </summary>
/// <returns></returns>
public object DeSerializeXMLNow()
{
try
{
if (!checkObj(obj))
{
MessageBox.Show("该对象不可反序列化!", "系统错误");
return null;
}
object objXml = null;
FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
XmlSerializer formatter = new XmlSerializer(type);
obj = formatter.Deserialize(fileStream);
fileStream.Close();
return objXml;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
}
}

c#打印记忆功能的更多相关文章

  1. 【验证】C# dataSource 的记忆功能

    做项目时遇到的问题:dataSource被ComboBox引用过一次,会记忆最后一次选中的值,然后下一次再用时这个值会直接呈现在ComboBox中. 为验证是dataSource还是ComboBox自 ...

  2. c# winforms TextBox的记忆功能

    c# winforms TextBox的记忆功能 1:在项目上点右键  sproperties-settings 添加项目 如 MyText  类型 String 2: 获取值:   string l ...

  3. 让你的javascript函数拥有记忆功能,降低全局变量的使用

    考虑例如以下场景:假如我们须要在界面上画一个圆,初始的时候界面是空白的.当鼠标移动的时候,圆须要尾随鼠标移动.鼠标的当前位置就是圆心.我们的实现方案是:假设界面上还没有画圆,那么就新创建一个:假设已经 ...

  4. Extjs grid分页多选记忆功能

    很多同事在用extjs grid做分页的时候,往往会想用grid的多选功能来实现导出Excel之类的功能(也就是所谓的多选记忆功能),但在选选择下一页的时候 上一页选中的已经清除 这是因为做分页的时候 ...

  5. 如何取消input记忆功能

    默认情况下,input会有这个记忆功能,如果不想让它记忆,可以在input上加上autocomplete="off"即可.

  6. springMVC 复选框带有选择项记忆功能的处理

    前言:由于jsp管理页面经常会遇到复选框提交到JAVA后台,后台处理逻辑完成后又返回到jsp页面,此时需要记住jsp页面提交时复选框的选择状态,故编写此功能! 一.复选框的初始化 1.1.jsp页面 ...

  7. js之表单记忆功能

    在项目中,我们难免会遇到希望相同用户操作本次打开页面时可以展现或者自动记录上次登录系统点击过的的复选框,单选按钮等操作的状态,也就是表单记忆功能,这时,一个很重要的技术便派上了用场,即cookie. ...

  8. Excel 2003-单元格输入中带记忆功能

    最近有个同事问我,如何在Excel单元格输入中带记忆功能?其实很简单: 工具ó选项ó编辑ó将“记忆式键入”项选中ó确定: //附图[效果图]:

  9. 动态渲染的input怎么取消记忆功能

    方法1 :自定义去除记忆功能属性: $('#index_table_filter > label > input[type="search"]').attr('auto ...

随机推荐

  1. 防抖(Debouncing)和节流(Throttling)

    onscoll防抖封装函数 scroll 事件本身会触发页面的重新渲染,同时 scroll 事件的 handler 又会被高频度的触发, 因此事件的 handler 内部不应该有复杂操作,例如 DOM ...

  2. genToken- Php file

    <?php public function genToken($len = 32, $md5 = true) { # Seed random number generator # Only ne ...

  3. NET Core 的 Views

    NET Core 十种方式扩展你的 Views 原文地址:http://asp.net-hacker.rocks/2016/02/18/extending-razor-views.html作者:Jür ...

  4. 1.org.hibernate.MappingException: No Dialect mapping for JDBC type: -9

    org.hibernate.MappingException: No Dialect mapping for JDBC type: -9 原因:Hibernate框架的方言(Dialect )没有数据 ...

  5. 转:CSS3 Flexbox 布局介绍

    转:CSS3 Flexbox 布局介绍 Flexbox是一个用于页面布局的全新CSS3模块功能.它可以把列表放在同一个方向(从左到右或从上到下排列),并且让这些列表能延伸到占用可用的空间.较为复杂的布 ...

  6. #pragma anon_unions, #pragma no_anon_unions

    #pragma anon_unions, #pragma no_anon_unions 这些编译指示启用和禁用对匿名结构和联合的支持. 缺省设置 缺省值为 #pragma no_anon_unions ...

  7. Delphi组件开发-在窗体标题栏添加按钮(使用MakeObjectInstance(NewWndProc),并处理好多消息)

    这是一个在窗体标题栏添加自定义按钮的组件(TTitleBarButton)开发实例,标题栏按钮组件TTitleBarButton以TComponent为直接继承对象,它是一个可以在窗体标题栏上显示按钮 ...

  8. 你会用shuffle打乱列表吗?

    在网站上我们经常会看到关键字云(Word Cloud)和标签云(Tag Cloud),用于表明这个关键字或标签是经常被查阅的,而且还可以看到这些标签的动态运动,每次刷新都会有不一样的关键字或便签,让浏 ...

  9. Microsoft Azure 在北美 TechEd 大会上发布令人振奋的更新,帮助客户开始使用云服务

    云计算因其速度.规模和成本节省等优势而备受众多企业青睐.但企业需帮助,才能以安全可靠的方式使用云,同时还要利用企业的现有投资, 才能实现这些优势.因此,在TechEd 大会上,我们推出了一些新的服务, ...

  10. 原来ipad的浏览器也可以直接clip到evernote

    今天才发现是有方法通过邮件方式保存ipad上浏览的内容到evernote,之前以为要反复切换app来做到. 只要在toread.cc登记evernote对应帐号的邮箱,就可以根据toread返回到ev ...