本人做Winform开发多年,孜孜不倦,略有小成,其中收集或者自己开发一些常用的东西,基本上在各个项目都能用到的一些开发经验及知识积累,现逐步介绍一些,以飨读者,共同进步。

1、窗口【×】关闭按钮变为最小化,并在托盘提示信息

一般有些管理系统,为了防止客户随意关闭程序或者基于其他原因,一般会把 窗口【×】关闭按钮变为最小化,如大家熟悉的飞信、MSN等等,但是有些不是很熟悉的客户,最小化到托盘的时候,却不知道程序到了那里去了,因此,最小化的时候,伴随一个气泡提示信息,显得有一定的必要,如下截图所示。

首先在主窗体的设计界面中添加一个NotifyIcon控件,然后实现相关的代码即可。 

下面列出一些关键的代码出来,大家看了应该就知道如何实现了

  1. private void notifyMenu_Show_Click(object sender, EventArgs e)
  2.  
  3. {
  4.  
  5. if (this.WindowState == FormWindowState.Minimized)
  6.  
  7. {
  8.  
  9. this.WindowState = FormWindowState.Maximized;
  10.  
  11. this.Show();
  12.  
  13. this.BringToFront();
  14.  
  15. this.Activate();
  16.  
  17. this.Focus();
  18.  
  19. }
  20.  
  21. else
  22.  
  23. {
  24.  
  25. this.WindowState = FormWindowState.Minimized;
  26.  
  27. this.Hide();
  28.  
  29. }
  30.  
  31. }
  32.  
  33. private void notifyMenu_Exit_Click(object sender, EventArgs e)
  34.  
  35. {
  36.  
  37. try
  38.  
  39. {
  40.  
  41. this.ShowInTaskbar = false;
  42.  
  43. Portal.gc.Quit();
  44.  
  45. }
  46.  
  47. catch
  48.  
  49. {
  50.  
  51. // Nothing to do.
  52.  
  53. }
  54.  
  55. }
  56.  
  57. private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
  58.  
  59. {
  60.  
  61. notifyMenu_Show_Click(sender, e);
  62.  
  63. }
  64.  
  65. private void MainForm_MaximizedBoundsChanged(object sender, EventArgs e)
  66.  
  67. {
  68.  
  69. this.Hide();
  70.  
  71. }
  72.  
  73. /// <summary>
  74.  
  75. /// 缩小到托盘中,不退出
  76.  
  77. /// </summary>
  78.  
  79. private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
  80.  
  81. {
  82.  
  83. //如果我们操作【×】按钮,那么不关闭程序而是缩小化到托盘,并提示用户.
  84.  
  85. if (this.WindowState != FormWindowState.Minimized)
  86.  
  87. {
  88.  
  89. e.Cancel = true;//不关闭程序
  90.  
  91. //最小化到托盘的时候显示图标提示信息,提示用户并未关闭程序
  92.  
  93. this.WindowState = FormWindowState.Minimized;
  94.  
  95. notifyIcon1.ShowBalloonTip(3000, "程序最小化提示",
  96.  
  97. "图标已经缩小到托盘,打开窗口请双击图标即可。",
  98.  
  99. ToolTipIcon.Info);
  100.  
  101. }
  102.  
  103. }
  104.  
  105. private void MainForm_Move(object sender, EventArgs e)
  106.  
  107. {
  108.  
  109. if (this == null)
  110.  
  111. {
  112.  
  113. return;
  114.  
  115. }
  116.  
  117. //最小化到托盘的时候显示图标提示信息
  118.  
  119. if (this.WindowState == FormWindowState.Minimized)
  120.  
  121. {
  122.  
  123. this.Hide();
  124.  
  125. notifyIcon1.ShowBalloonTip(3000, "程序最小化提示",
  126.  
  127. "图标已经缩小到托盘,打开窗口请双击图标即可。",
  128.  
  129. ToolTipIcon.Info);
  130.  
  131. }
  132.  
  133. }

2、只允许允许一个程序实例,即使是通过虚拟桌面方式连接过来的,也是只允许一个人运行。

这个已经封装好代码了,只需要在Main函数里面调用一下函数即可,允许多个实例会出现下面的对话框提示信息,提示不允许多实例运行,如下所示:

代码如下所示。

  1. /// <summary>
  2. /// 应用程序的主入口点。
  3. /// </summary>
  4. [STAThread]
  5. private static void Main()
  6. {
  7. GlobalMutex();
  8.  
  9. Application.EnableVisualStyles();
  10. Application.SetCompatibleTextRenderingDefault(false);
  11.  
  12. //******启动代码**********
  13. }
  14.  
  15. private static Mutex mutex = null;
  16. private static void GlobalMutex()
  17. {
  18. // 是否第一次创建mutex
  19. bool newMutexCreated = false;
  20. string mutexName = "Global\\" + "WareHouseMis";//系统名称,Global为全局,表示即使通过通过虚拟桌面连接过来,也只是允许运行一次
  21. try
  22. {
  23. mutex = new Mutex(false, mutexName, out newMutexCreated);
  24. }
  25. catch (Exception ex)
  26. {
  27. Console.Write(ex.Message);
  28. System.Threading.Thread.Sleep(1000);
  29. Environment.Exit(1);
  30. }
  31.  
  32. // 第一次创建mutex
  33. if (newMutexCreated)
  34. {
  35. Console.WriteLine("程序已启动");
  36. //todo:此处为要执行的任务
  37. }
  38. else
  39. {
  40. MessageUtil.ShowTips("另一个窗口已在运行,不能重复运行。");
  41. System.Threading.Thread.Sleep(1000);
  42. Environment.Exit(1);//退出程序
  43. }
  44. }

3、使用NotifyWindow给用户提示信息

可以通过NotifyWindow类(最后附件中有),做一些信息的提示,方便用户了解一些重要信息的提示,界面较为友好,如下所示:

提示信息的代码使用如下:

  1. /// <summary>
  2. /// 弹出提示消息窗口
  3. /// </summary>
  4. public void Notify(string caption, string content)
  5. {
  6. Notify(caption, content, 400, 200, 5000);
  7. }
  8.  
  9. /// <summary>
  10. /// 弹出提示消息窗口
  11. /// </summary>
  12. public void Notify(string caption, string content, int width, int height, int waitTime)
  13. {
  14. NotifyWindow notifyWindow = new NotifyWindow(caption, content);
  15. notifyWindow.TitleClicked += new System.EventHandler(notifyWindowClick);
  16. notifyWindow.TextClicked += new EventHandler(notifyWindowClick);
  17. notifyWindow.SetDimensions(width, height);
  18. notifyWindow.WaitTime = waitTime;
  19. notifyWindow.Notify();
  20. }
  21.  
  22. private void notifyWindowClick(object sender, EventArgs e)
  23. {
  24. //SystemMessageInfo info = BLLFactory<SystemMessage>.Instance.FindLast();
  25. //if (info != null)
  26. //{
  27. // //FrmEditMessage dlg = new FrmEditMessage();
  28. // //dlg.ID = info.ID;
  29. // //dlg.ShowDialog();
  30. //}
  31. }

4、使用SearchCondion控件,简化查询条件的转化

不管在Winform或者在WebForm中,查询构造条件总是非常繁琐的事情,使用该控件能有效简化代码,提高操作的准确及方便行,这个控件我完成了几年了,一直伴随我处理各种查询操作。

  1. private string GetConditionSql()
  2. {
  3. SearchCondition condition = new SearchCondition();
  4. condition.AddCondition("ItemName", this.txtName.Text, SqlOperator.Like)
  5. .AddCondition("ItemBigType", this.txtBigType.Text, SqlOperator.Like)
  6. .AddCondition("ItemType", this.txtItemType.Text, SqlOperator.Like)
  7. .AddCondition("Specification", this.cmbSpecNumber.Text, SqlOperator.Like)
  8. .AddCondition("MapNo", this.txtMapNo.Text, SqlOperator.Like)
  9. .AddCondition("Material", this.txtMaterial.Text, SqlOperator.Like)
  10. .AddCondition("Source", this.txtSource.Text, SqlOperator.Like)
  11. .AddCondition("Note", this.txtNote.Text, SqlOperator.Like)
  12. .AddCondition("Manufacture", this.txtManufacture.Text, SqlOperator.Like)
  13. .AddCondition("ItemNo", this.txtItemNo.Text, SqlOperator.LikeStartAt);
  14. string where = condition.BuildConditionSql().Replace("Where", "");
  15.  
  16. return where;
  17. }

可以构造条件后,传入查询函数,实现数据的查询。

  1. string where = GetConditionSql();
  2. List<ItemDetailInfo> list = BLLFactory<ItemDetail>.Instance.Find(where, this.winGridViewPager1.PagerInfo);
  3. this.winGridViewPager1.DataSource = new WHC.Pager.WinControl.SortableBindingList<ItemDetailInfo>(list);
  4. this.winGridViewPager1.PrintTitle = Portal.gc.gAppUnit + " -- " + "备件信息报表";

最后呈上代码用到的一些类库及控件:http://files.cnblogs.com/wuhuacong/WinformTips.rar

Winform开发几个常用的开发经验及知识积累(一)的更多相关文章

  1. WinForm开发,窗体显示和窗体传值相关知识总结

    主窗体中代码: public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void b ...

  2. 【Cocos2d-x游戏开发】细数Cocos2d-x开发中那些常用的C++11知识

    自从Cocos2d-x3.0开始,Cocos2dx就正式的使用了C++11标准.C++11简洁方便的特性使程序的可拓展性和可维护性大大提高,也提高了代码的书写速度. 下面我们就来一起学习一下Cocos ...

  3. Winform开发常用控件之DataGridView的简单数据绑定——自动绑定

    DataGridView控件可谓是Winform开发的重点控件,对于数据的呈现和操作非常方便,DataGridView可谓是既简单又复杂.简单在于其已经集成了很多方法,复杂在于可以使用其实现复杂的数据 ...

  4. Winform开发的快速、健壮、解耦的几点建议

    在Winform开发领域开发过十多年的项目中,见证着形形色色的架构和官方技术的应用,从最早类似Winform模式的WebForm技术,到接着的JQuery+界面组件,再到Asp.net Core的技术 ...

  5. 在Winform开发中使用日程控件XtraScheduler(2)--深入理解数据的存储

    在上篇随笔<在Winform开发中使用日程控件XtraScheduler>中介绍了DevExpress的XtraScheduler日程控件的各种使用知识点,对于我们来说,日程控件不陌生,如 ...

  6. Navi.Soft30.框架.WinForm.开发手册

    阅读导航 Navi.Soft30.Core类库.开发手册 http://www.cnblogs.com/xiyang1011/p/5709489.html Navi.Soft30.框架.WinForm ...

  7. Winform开发的界面处理优化

    在Winform开发中,客户体验是个很好的参考性指标,如果一个功能使用的时候感觉很流畅,说明我们的程序执行效率还不错,但是随着数据的真多,原先可能流程的地方可能会变得比较卡,这时候就需要追本索源,找到 ...

  8. C# WinForm开发系列 - 文章索引

    该系列主要整理收集在使用C#开发WinForm应用文章及相关代码, 平时看到大家主要使用C#来开发Asp.Net应用,这方面的文章也特别多,而关于WinForm的文章相对少很多,而自己对WinForm ...

  9. Winform开发全套31个UI组件开源共享

    一.前言 这套UI库是上一个公司(好几年前了)完成的.当时主要为开发公司内部ERP系统,重新设计实现了所有用到的Winform组建,包括Form窗体组建6个(支持换肤),基础控件25个.其中有很多参考 ...

随机推荐

  1. gitlab和Django实现push自动更新

    1.设置webhook gitlab->setting->webhook:http://121.143.191.166:7000?token=23028-b396-12e5-9912-ba ...

  2. Android数据的四种存储方式

    作为一个完成的应用程序,数据存储操作是必不可少的.因此,Android系统一共提供了四种数据存储方式.分别是:SharePreference.SQLite.Content Provider和File. ...

  3. 初探接口测试框架--python系列1

    点击标题下「蓝色微信名」可快速关注 坚持的是分享,搬运的是知识,图的是大家的进步,没有收费的培训,没有虚度的吹水,喜欢就关注.转发(免费帮助更多伙伴)等来交流,想了解的知识请留言,给你带来更多价值,是 ...

  4. Flex开发自定义控件

    前期准备: 点击File菜单 -> New -> MXML Component,然后弹出一个对话框. 在对话框中输入组件名,选择此组件继承的类型,如:Canvas,DataGrid,Com ...

  5. 如何在后台动态生成ASPxCheckBoxList标签并循环(数据调用存储过程)

    DataTable dt_attrname = new DataTable(); DataTable dt_valuename = new DataTable(); dt_valuename = go ...

  6. 在PHP5.3以上版本运行ecshop和ecmall出现的问题及解决方案

    ecshop 问题一:商城首页报错 Strict Standards: Only variables should be passed by reference in D:\wamp\ecshop\i ...

  7. Could not resolve this reference. Could not locate the assembly

    Rebuild Project 的时候提示找不到NewtonJson 组件,重新添加了Dll(Newtonsoft.Json.dll),依然抛错. 解决办法,将Dll(Newtonsoft.Json. ...

  8. 在ASP.NET开始执行HTTP请求的处理程序之前

    using Dscf.Client.Web.Class; using Dscf.Client.Web.DscfService; using Dscf.Client.Web.Handler; using ...

  9. linux 下 jdk tar.gz 包安装方法

    JDK安装 tar.gz为解压后就可使用的版本,这里我们将jdk-7u3-linux-i586.tar.gz解压到/usr/local/下. 1.解压 解压到当前目录:$ tar -zxvf /opt ...

  10. storm学习-storm入门

    超好资料: 英文:https://github.com/xetorthio/getting-started-with-storm/blob/master/ch03Topologies.asc 中文:h ...