WinForm票据套打重定位

来源:互联网 发布:淘宝一元秒杀网址 编辑:程序博客网 时间:2024/04/30 22:55

   项目中经常会遇到客户打印票据或证件等套打的功能需求,每次做好的打印窗体, 随着客户运行环境的不同或者打印设备参数等设置不同,都会造成最终的打印位置偏离许多。 每次都得和客户解释半天,甚至还得到现场一个一个的帮客户调试好环境,这样的事情发生了好多次,一年又一年重复着......
      当第N+1个客户打N+1个电话问起同样的问题:“怎么打印又偏移了”,终于忍无可忍,实现一个运行时可动态拖动控件并将打印控件变量的位置保存到文件中这样的功能,以后,如果客户再烦我,终于可以一句话回复他:“(你大爷的)自己调去”。。。    

      ok,思路很简单,需要考虑以下几个方面的处理:
     1.控件拖动事件处理
     2.控件拖动结束后,提取位置信息
     3.读取配置文件,并将位置信息保存到文件中

     那么,下次窗体加载时,应该首先读取配置文件,然后加载控件位置信息,流程如下:
     1.初始化配置文件
     2.窗体加载后,根据配置文件,初始化控件位置,如果窗体首次加载且尚未调整过控件位置(既配置文件为空),
        则默认将当前控件的位置信息保存到配置文件中
     3.拖动控件,精准定位打印位置
     4.窗体关闭时,将配置信息写入文件
  

     根据以上需求,定义如下几个类文件

     1.ConfigHelper : 配置缓存对象,用于读取及写入配置文件

     2.FormConfig :用于窗体配置对象

     3.ControlConfig:用于控件配置对象

     4.HigoMovingControl:用于控件拖动,其构造函数需要提供当前控件,及控件所在窗体两个参数

     过程非常简单,这里我还是写了几个配置缓存对象,代码如下:

ConfigHelper.cs

View Code   1 using System;
  2 using System.Collections.Generic;
  3 using System.Text;
  4 using System.Xml;
  5 
  6 namespace Higo.Controls.Config
  7 {
  8     /// <summary>
  9     /// 配置文件辅助类
 10     /// </summary>
 11     public class ConfigHelper
 12     {
 13         #region 私有域
 14 
 15         private readonly string _filePath = null;
 16         private Dictionary<string, FormConfig> _configData = new Dictionary<string, FormConfig>();
 17         
 18         #endregion
 19 
 20         #region 构造函数
 21 
 22         ConfigHelper()
 23         {
 24             _filePath = System.IO.Path.Combine(System.Windows.Forms.Application.StartupPath, "control.config");
 25             InitConfig();
 26         }
 27         
 28         #endregion
 29 
 30         #region 自定义方法
 31 
 32         private void InitConfig()
 33         {
 34             try
 35             {
 36                 // 读取系统配置文件,获取系统配置信息
 37                     XmlDocument doc = new XmlDocument();
 38                     doc.Load(_filePath);
 39                     if (doc != null)
 40                     {
 41                         XmlNodeList nodes = doc.SelectNodes("//forms//form");
 42                         foreach (XmlNode formNode in nodes)
 43                         {
 44                             FormConfig form = new FormConfig();
 45                             foreach (XmlAttribute attr in formNode.Attributes) 
 46                             {
 47                                 form.Add(attr.Name, attr.Value);
 48                             }
 49 
 50                             XmlNodeList controlsNodes = formNode.SelectNodes("//control");
 51                             foreach (XmlNode detailNode in controlsNodes)
 52                             {
 53                                 ControlConfig control = new ControlConfig(form);
 54                                 foreach (XmlAttribute attr in detailNode.Attributes)
 55                                 {
 56                                     control.Add(attr.Name, attr.Value);
 57                                 }
 58 
 59                                 form.Controls.Add(control);
 60                             }
 61 
 62                             _configData.Add(form.GetValue("Name"), form);
 63                         }
 64                     }
 65             }
 66             catch (Exception ex)
 67             {
 68                 // TODO:记录日志
 69             }
 70         }
 71 
 72         #endregion
 73 
 74         #region 单例模式辅助类
 75 
 76         class ConfigHelperer
 77         {
 78             static ConfigHelperer()
 79             {
 80             }
 81 
 82             internal static readonly ConfigHelper instance = new ConfigHelper();
 83         }
 84 
 85         /// <summary>
 86         /// 通过单例调用配置访问接口
 87         /// </summary>
 88         public static ConfigHelper Instance
 89         {
 90             get
 91             {
 92                 return ConfigHelperer.instance;
 93             }
 94         }
 95 
 96         #endregion
 97 
 98         #region 索引器
 99 
100         /// <summary>
101         /// 根据窗体名称,获取窗体配置
102         /// </summary>
103         /// <param name="Name">窗体名称</param>
104         /// <returns>窗体配置对象</returns>
105         public FormConfig this[string name]
106         {
107             get
108             {
109                 if (_configData.ContainsKey(name))
110                 {
111                     return _configData[name];
112                 }
113 
114                 return null;
115             }
116         }
117 
118         #endregion
119 
120         #region 公有方法
121 
122         public void SaveConfig()
123         {
124             XmlDocument xDoc = new XmlDocument();
125             xDoc.Load(_filePath);
126 
127             XmlNode xNode;
128             XmlElement xElem;
129             XmlNodeList xNodeList;
130 
131             foreach (KeyValuePair<string, FormConfig> kp in _configData)
132             {
133                 string name = kp.Key;
134                 FormConfig form = kp.Value;
135 
136                 xElem = (XmlElement)xDoc.SelectSingleNode("//forms//form[@Name='" + name + "']");
137                 if (xElem != null)
138                 {
139                     foreach (KeyValuePair<stringstring> formKP in form.Config)
140                     {
141                         xElem.SetAttribute(formKP.Key, formKP.Value);
142                     }
143 
144                     foreach (ControlConfig control in form.Controls)
145                     {
146                         xElem = (XmlElement)xDoc.SelectSingleNode("//forms//form//control[@Name='" + control["Name"+ "']");
147                         if (xElem != null)
148                         {
149                             foreach (KeyValuePair<stringstring> controlKP in control.Config)
150                             {
151                                 xElem.SetAttribute(controlKP.Key, controlKP.Value);
152                             }
153                         }
154                     }
155                 }
156             }
157 
158             xDoc.Save(_filePath);
159         }
160 
161         #endregion
162     }
163 }

FormConfig.cs    

View Code   1 /*******************************************************************************
  2 #                    FormConfig.cs
  3 #  Comment:
  4 #                    
  5 #  Current Version: V1.0
  6 #  Author: 
  7 #
  8 #  History List:
  9 #  V1.0    Created by CodeBuilder@2010/10/29
 10 #
 11 #******************************************************************************/
 12 using System;
 13 using System.Text;
 14 using System.Collections.Generic;
 15 
 16 namespace Higo.Controls.Config
 17 {
 18     /// <summary>
 19     /// 窗体配置对象
 20     /// </summary>
 21     public class FormConfig
 22     {
 23         #region 私有域
 24 
 25         private IList<ControlConfig> _controls = new List<ControlConfig>();
 26         private Dictionary<stringstring> _configs = new Dictionary<stringstring>();
 27 
 28         #endregion
 29 
 30         #region 构造函数
 31         
 32         internal FormConfig()
 33         {
 34         }
 35 
 36         #endregion
 37 
 38 
 39         #region 公有属性
 40 
 41         public IList<ControlConfig> Controls
 42         {
 43             get 
 44             {
 45                 return _controls;
 46             }
 47         }
 48 
 49         public Dictionary<stringstring> Config
 50         {
 51             get
 52             {
 53                 return _configs;
 54             }
 55         }
 56 
 57         #endregion
 58 
 59         #region 公有方法
 60 
 61         public void Add(string key, string value)
 62         {
 63             if (_configs.ContainsKey(key))
 64             {
 65                 _configs[key] = value;
 66             }
 67             else
 68             {
 69                 _configs.Add(key,value);
 70             }
 71         }
 72 
 73         public void Remove(string key)
 74         {
 75             if (_configs.ContainsKey(key))
 76             {
 77                 _configs[key] = null;
 78                 _configs.Remove(key);
 79             }
 80         }
 81 
 82         public string GetValue(string key)
 83         {
 84             if (_configs.ContainsKey(key))
 85             {
 86                 return _configs[key];
 87             }
 88 
 89             return string.Empty;
 90         }
 91 
 92         #endregion
 93 
 94         #region 索引器
 95 
 96         /// <summary>
 97         /// 根据字段代码,获取配置明细对象
 98         /// </summary>
 99         /// <param name="fieldCode">配置字段代码</param>
100         /// <returns></returns>
101         public ControlConfig this[string key]
102         {
103             get
104             {
105                 if (_controls.Count <= 0)
106                 {
107                     return null;
108                 }
109 
110                 foreach (ControlConfig c in _controls)
111                 {
112                     if(c["Name"== key )
113                     {
114                         return c;
115                     }
116                 }
117 
118                 return null;
119             }
120         } 
121 
122         #endregion
123     }
124 }

ControlConfig.cs

View Code   1 /*******************************************************************************
  2 #                    ControlConfig.cs
  3 #  Comment:
  4 #                    
  5 #  Current Version: V1.0
  6 #  Author: 
  7 #
  8 #  History List:
  9 #  V1.0    Created by wdong
 10 #
 11 #******************************************************************************/
 12 using System;
 13 using System.Text;
 14 using System.Collections.Generic;
 15 
 16 namespace Higo.Controls.Config
 17 {
 18     /// <summary>
 19     /// Control配置对象
 20     /// </summary>
 21     public class ControlConfig
 22     {
 23         #region 私有域
 24 
 25         private FormConfig _belongto;
 26         private Dictionary<stringstring> _configs = new Dictionary<stringstring>();
 27 
 28         #endregion
 29 
 30         #region 构造函数
 31 
 32         internal ControlConfig(FormConfig form)
 33         {
 34             _belongto = form;
 35         }
 36 
 37         #endregion
 38 
 39         #region 公有属性
 40 
 41         public Dictionary<stringstring> Config
 42         {
 43             get
 44             {
 45                 return _configs;
 46             }
 47         }
 48 
 49         #endregion
 50 
 51         #region 公有方法
 52 
 53         public void Add(string key, string value)
 54         {
 55             if (_configs.ContainsKey(key))
 56             {
 57                 _configs[key] = value;
 58             }
 59             else
 60             {
 61                 _configs.Add(key, value);
 62             }
 63         }
 64 
 65         public void Remove(string key)
 66         {
 67             if (_configs.ContainsKey(key))
 68             {
 69                 _configs[key] = null;
 70                 _configs.Remove(key);
 71             }
 72         }
 73 
 74         public string GetValue(string key)
 75         {
 76             if (_configs.ContainsKey(key))
 77             {
 78                 return _configs[key];
 79             }
 80 
 81             return string.Empty;
 82         }
 83 
 84         #endregion
 85 
 86         /// <summary>
 87         /// 根据字段代码,获取配置明细对象
 88         /// </summary>
 89         /// <param name="fieldCode">配置字段代码</param>
 90         /// <returns></returns>
 91         public string this[string key]
 92         {
 93             get
 94             {
 95                 return GetValue(key);
 96             }
 97             set
 98             {
 99                 Add(key, value);
100             }
101         } 
102     }
103 } 

HigoMovingControl.cs

View Code   1 using System;
  2 using System.Collections.Generic;
  3 using System.Text;
  4 using System.Drawing;
  5 using System.Windows.Forms;
  6 using Higo.Controls.Config;
  7 
  8 namespace Higo.Controls
  9 {
 10     /// <summary>
 11     /// 使窗口的中的指定控件支持运行时移动
 12     /// TODO:运行时缩放
 13     /// </summary>
 14     public class HigoMovingControl
 15     {
 16         #region 私有成员
 17         
 18         bool _isMoving = false;
 19         Point _pCtrlLastCoordinate = new Point(0,0);
 20         Point _pCursorOffset = new Point(00);
 21         Point _pCursorLastCoordinate = new Point(00);
 22         private Control _ctrl = null;
 23         private ScrollableControl _container = null;
 24 
 25         #endregion
 26         
 27         #region 私有方法
 28         
 29         /// <summary>
 30         /// 在鼠标左键按下的状态记录鼠标当前的位置,以及被移动组件的当前位置
 31         /// </summary>
 32         /// <param name="sender"></param>
 33         /// <param name="e"></param>
 34         private void MouseDown(object sender, MouseEventArgs e)
 35         {
 36             if (_container == null)
 37             {
 38                 return;
 39             }
 40             if (e.Button == MouseButtons.Left)
 41             {
 42                 _isMoving = true;
 43                 _pCtrlLastCoordinate.X = _ctrl.Left;
 44                 _pCtrlLastCoordinate.Y = _ctrl.Top;
 45                 _pCursorLastCoordinate.X = Cursor.Position.X;
 46                 _pCursorLastCoordinate.Y = Cursor.Position.Y;
 47             }
 48         }
 49         private void MouseMove(object sender, MouseEventArgs e)
 50         {
 51             if (_container == null)
 52             {
 53                 return;
 54             }
 55                
 56             if (e.Button == MouseButtons.Left)
 57             {
 58                 if (this._isMoving)
 59                 {
 60                     Point pCursor = new Point(Cursor.Position.X, Cursor.Position.Y);
 61                  
 62                     _pCursorOffset.X = pCursor.X - _pCursorLastCoordinate.X;
 63                     _pCursorOffset.Y = pCursor.Y - _pCursorLastCoordinate.Y;
 64                     _ctrl.Left = _pCtrlLastCoordinate.X + _pCursorOffset.X;
 65                     _ctrl.Top = _pCtrlLastCoordinate.Y + _pCursorOffset.Y;
 66                 }
 67 
 68             }
 69         }
 70  
 71         private void MouseUp(object sender, MouseEventArgs e)
 72         {
 73             if (_container == null)
 74             {
 75                 return;
 76             }
 77 
 78             if (this._isMoving)
 79             {
 80                 if (_pCursorOffset.X == 0 && _pCursorOffset.Y == 0)
 81                 {
 82                     return;
 83                 }
 84 
 85                 if ((_pCtrlLastCoordinate.X + _pCursorOffset.X + _ctrl.Width) > 0)
 86                 {
 87                     _ctrl.Left = _pCtrlLastCoordinate.X + _pCursorOffset.X;
 88                 }
 89                 else
 90                 {
 91                     _ctrl.Left = 0;
 92                 }
 93 
 94                 if ((_pCtrlLastCoordinate.Y + _pCursorOffset.Y + _ctrl.Height) > 0)
 95                 {
 96                     _ctrl.Top = _pCtrlLastCoordinate.Y + _pCursorOffset.Y;
 97                 }
 98                 else
 99                 {
100                     _ctrl.Top = 0;
101                 }
102 
103                 _pCursorOffset.X = 0;
104                 _pCursorOffset.Y = 0;
105 
106                 ConfigHelper.Instance[_container.GetType().FullName][_ctrl.Name]["Left"= _ctrl.Left.ToString();
107                 ConfigHelper.Instance[_container.GetType().FullName][_ctrl.Name]["Top"= _ctrl.Top.ToString();
108             }
109         }
110         #endregion
111         #region 构造函数
112         /// <summary>
113         /// 获取被移动控件对象和容器对象
114         /// </summary>
115         /// <param name="c">被设置为可运行时移动的控件</param>
116         /// <param name="parentContain">可移动控件的容器</param>
117         public HigoMovingControl(Control c, ScrollableControl parentContain)
118         {
119             _ctrl = c;
120             this._container = parentContain;
121             _ctrl.MouseDown += new MouseEventHandler(MouseDown);
122             _ctrl.MouseMove += new MouseEventHandler(MouseMove);
123             _ctrl.MouseUp += new MouseEventHandler(MouseUp);
124         }
125         #endregion
126     }
127 }

应用例子代码

View Code  1  Higo.Controls.Config.FormConfig _formConfig = Higo.Controls.Config.ConfigHelper.Instance["Higo.EntryValid.App.PrintForm"];
 2 
 3 
 4  this.certificateWordLabel1.Text = _currentPersonInfo.CertificateWord;
 5             HigoMovingControl certificateWordLabel1Control = new HigoMovingControl(this.certificateWordLabel1,this);
 6             if(!_formConfig["certificateWordLabel1"].Config.ContainsKey("Left"))
 7             {
 8                 _formConfig["certificateWordLabel1"]["Left"= this.certificateWordLabel1.Left.ToString();
 9                 _formConfig["certificateWordLabel1"]["Top"= this.certificateWordLabel1.Top.ToString();
10             }
11             else
12             {
13                 this.certificateWordLabel1.Left =int.Parse(_formConfig["certificateWordLabel1"]["Left"].ToString());
14                 this.certificateWordLabel1.Top =int.Parse(_formConfig["certificateWordLabel1"]["Top"].ToString());
15             }

     当然,后续也可以进行批量移动等控制,有兴趣的同学可以一起来完善一下,功能很简单,希望对你有所帮助,谢谢!

原创粉丝点击