mfc类库详解,厚积薄发,丰富的公用类库积累,助你高效进行系统开发(2)

自从上篇随笔《厚积薄发,丰富的公用类库积累,助你高效进行系统开发(1)》一文以来,得到同行很多人的鼎力支持和关注,并且在大家的推动下,这篇文章也是上榜博客头条、编辑推荐、10天内推荐排行等荣誉,很多人对这些类库很是感兴趣,也希望进一步详细介绍相关类库的使用。本随笔系列将逐步介绍相关的类库的详细使用,并逐步整理成CHM的帮助文档,作为类库使用的指引手册,同时我会对类库进行进一步的丰富、提炼和优化,随笔将逐步发送,感谢大家的支持和鼓励。
1、程序配置管理辅助类 AppConfig

实现效果

  1. 本辅助类主要是用来方便获取或设置系统配置项的辅助类,可以用于读取*.exe.Config文件或者Web.config的文件内容,或者可以读取指定文件的配置项。 辅助类默认从当前目录中按顺序检索Web.Config和*.exe.Config文件。如果找到一个,则使用它作为默认的配置文件,不需要指定文件路径。
  2. 读取的文件格式是一般的XML配置文件格式,如下所示。

实现代码

  1. 读取配置项目。 AppConfig config = new AppConfig(); string Manufacturer = config.AppConfigGet("Manufacturer"); string ApplicationName = config.AppConfigGet("ApplicationName"); string AppWholeName = string.Format("{0}-{1}", Manufacturer, ApplicationName);
  2. 读取复杂的连接字符串配置,如上面的EnterpriseLibrary的连接字符串 DataAccess 配置项目的ConnectString。
    /// /// 测试数据库是否正常连接 /// /// public static bool TestConnection(string connectionString) { bool result = false; using (DbConnection connection = new SqlConnection(connectionString)) { connection.Open(); if (connection.State == System.Data.ConnectionState.Open) { result = true; } } return result; } public static bool TestConnection() { AppConfig config = new AppConfig(); return TestConnection(config.GetConnectionString("DataAccess")); }
  3. 写入配置项内容。
    AppConfig config = new AppConfig(); //保存地址(标准调用) config.AppConfigSet("PictureRootDir", this.txtPath.Text); //保存EnterpriseLibray数据库配置项(自定义设置) string connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}Database1.mdb;User ID=Admin;Jet OLEDB:Database Password=;", System.AppDomain.CurrentDomain.BaseDirectory); config.SetConnectionString("DataAccess", connectionString); //更新配置文件,以便起效 ConfigurationManager.RefreshSection("dataConfiguration"); ConfigurationManager.RefreshSection("connectionStrings"); ConfigurationManager.RefreshSection("appSettings");

2、消息提示对话框辅助类 MessageUtil

实现效果
1、本辅助类主要是用来方便实现标准统一的消息提示对话框。
2、封装的消息提示对话框包括个各种常用的对话框,如下所示: 厚积薄发,丰富的公用类库积累,助你高效进行系统开发(2)mfc类库详解
实现代码
MessageUtil.ShowTips("提示信息对话框"); MessageUtil.ShowWarning("警告消息提示框"); MessageUtil.ShowError("错误消息提示框"); MessageUtil.ShowYesNoAndTips("提示对话框,有Yes/No按钮"); MessageUtil.ShowYesNoAndWarning("警告对话框,有Yes/No按钮"); MessageUtil.ShowYesNoAndError("错误对话框,有Yes/No按钮"); MessageUtil.ShowYesNoCancelAndTips("提示对话框,有Yes/No/Cancel按钮"); MessageUtil.ConfirmYesNo("确认对话框,有Yes/No对话框"); MessageUtil.ConfirmYesNoCancel("确认对话框,有Yes/No/Cancel对话框");
3、日历类辅助类 CCalendar
实现效果
  1. 本辅助类主要是用来方便显示日期时间、农历、生肖的日历类。
  2. 其效果如下所示 厚积薄发,丰富的公用类库积累,助你高效进行系统开发(2)mfc类库详解

实现代码
CCalendar cal = new CCalendar(); this.lblCalendar.Text = cal.GetDateInfo(System.DateTime.Now).Fullinfo;
可以修改XML文件,实现对节日、时间提示等信息调整。
新年好!祝您在新的一年里身体健康,事业进步! ./img/theme/0101.gif 祝天下有情人终成眷属! ./img/theme/0214.gif .............
4、托盘图标辅助类 NotifyIconHelper
实现效果
  1. 本辅助类主要是用来方便动态设置托盘图标。
  2. 动态设置托盘图标,其效果如下所示 厚积薄发,丰富的公用类库积累,助你高效进行系统开发(2)mfc类库详解 厚积薄发,丰富的公用类库积累,助你高效进行系统开发(2)mfc类库详解

实现步骤
  1. 在主窗体添加一个NotifyIcon组件,并设置好相关的属性,如下所示。

厚积薄发,丰富的公用类库积累,助你高效进行系统开发(2)mfc类库详解 2、在代码引用相关的代码实现动态调用。 实现代码
public partial class FrmMain : Form { private NotifyIconHelper notifyHelper; private const string ClientName = "数据采集终端";//PC式采集器终端 public frmMain() { InitializeComponent(); this.Text = ClientName; //初始化托盘图标 notifyHelper = new NotifyIconHelper(this.notifyIcon1); notifyHelper.Icon_Conntected = Resources.Connected; notifyHelper.Icon_Shrink1 = Resources.shrink1; notifyHelper.Icon_Shrink2 = Resources.shrink2; notifyHelper.Icon_UnConntect = Resources.unConnected; notifyHelper.Text_Conntected = string.Format("{0}:终端已经连接", ClientName); notifyHelper.Text_UnConntect = string.Format("{0}:终端未连接", ClientName); notifyHelper.NotifyStatus = NotifyIconHelper.Status.TwinkleNotice; } /// /// 设置托盘图标的状态 /// ///
public void SetNotifyStatus(NotifyIconHelper.Status status) { notifyHelper.NotifyStatus = status; if (status == NotifyIconHelper.Status.Offline) { this.Invoke(new MethodInvoker(delegate() { this.Icon = notifyHelper.Icon_UnConntect; })); } else if (status == NotifyIconHelper.Status.Online) { this.Invoke(new MethodInvoker(delegate() { this.Icon = notifyHelper.Icon_Conntected; })); } }
5、DataTable操作辅助类 DataTableHelper

实现效果

  1. 本辅助类主要是用来方便对DataTable进行相关操作的辅助类,该类是非常常用的工具类,常用与数据显示、转换和报表生成等操作中。
  2. 提供的操作,包括有创建表、DataTable和实体类集合IList相互转化、表排序、表过滤等操作,以求达到快速进行DataTable操作的目的。

实现代码

  1. 根据字段创建表对象,多个列用逗号(,)分开,默认为表对象的列为string。 string columns = @"流水号,备注,供货商,操作员,库房名称,备件编号(pm码),备件名称,图号,规格型号,材质,备件属类,备件类别, 单位,最新单价(元),入库数量,总价,入库日期,来源,库位,部门,使用位置"; DataTable dt = DataTableHelper.CreateTable(columns);
  2. 根据字段创建表对象,多个列用逗号(,)分开。如需要制定列的类型,在字段后加上“|int”格式的字符。 string tableColumns = "ID|int,ItemNo,ItemName,StockQuantity|int,Manufacture,MapNo,Specification,Material,ItemBigType,ItemType, Unit,Price|decimal,Source,StoragePos,UsagePos,Note,WareHouse,Dept"; DataTable dt = DataTableHelper.CreateTable(tableColumns);
  3. 实体类转DataTable对象操作ListToDataTable,其对应的反操作函数DataTableToList使用方法类似。 string where = GetSearchSql(); IList list = BLLFactory.Instance.Find(where, this.winGridViewPager1.PagerInfo); DataTable dt = DataTableHelper.ToDataTable(list); this.winGridViewPager1.DataSource = dt.DefaultView; this.winGridViewPager1.dataGridView1.Refresh();
  4. DataTable对象排序操作SortedTable,可对表多列进行排序操作。 string where = GetSearchSql(); IList list = BLLFactory.Instance.Find(where, this.winGridViewPager1.PagerInfo); DataTable dt = DataTableHelper.ToDataTable(list); DataTable dtSort = DataTableHelper.SortedTable(dt, new string[]{"Number asc", "Type desc"});


由于篇幅原因,下篇继续整理发布出来,谢谢支持。
Tags:  vb类库 php类库 mfc类库 java类库 mfc类库详解

延伸阅读

最新评论

发表评论