⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 public.cs

📁 食堂就餐提醒系统(含源码) 为一个单位开发的
💻 CS
字号:
using System;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

//数据库操作
using System.Data;
using System.Data.SqlClient;

//字符中加密用
using System.Security;
using System.Security.Cryptography;

using Microsoft.Win32;
using System.Diagnostics;

namespace XFMAS
{
	/// <summary>
	/// Public 的摘要说明。
	/// </summary>
	public class Public
	{
		public Public()
		{
			//
			// TODO: 在此处添加构造函数逻辑
			//
		}

		public static System.Windows.Forms.NotifyIcon notifyIcon1 = null;

		/// <summary>
		/// 系统名称
		/// </summary>
		public static string systemName = null;

		/// <summary>
		/// 配置文件流
		/// </summary>
		public static DataTable configXml = null;

		/// <summary>
		/// 配置文件名
		/// </summary>
		public static string configFile = "System.Config";

		/// <summary>
		/// 用户表数据库信息,共含有六个参数
		/// 1、数据库连接串
		/// 2、数据库名称
		/// 3、用户编号
		/// 4、登录密码
		/// 5、SQL
		/// 6、用户姓名
		/// </summary>
		public static string usersDbInfo = "";

		//
		public static string currentUserID = "";
		public static string currentUserName = "";

		/// <summary>
		/// 最近的一次提醒时间
		/// </summary>
		public static string AlertTime = "1900-0-0 0:00:00";

		/// <summary>
		/// 提醒起始时间
		/// </summary>
		public static DateTime startTime = Convert.ToDateTime("1900-1-1 0:00:00");

		/// <summary>
		/// 提醒结束时间
		/// </summary>
		public static DateTime endTime = Convert.ToDateTime("1900-1-1 0:00:00");

		/// <summary>
		/// 登录窗口
		/// </summary>
		public static LoginApp loginWin = null;

		/// <summary>
		/// 提醒时间段内是否允许修改
		/// </summary>
		public static bool alertingIsMod = true;

		/// <summary>
		/// 今日是否提示
		/// </summary>
		public static bool todayIsAlert = false;

		//
		public static string[] ruleCodes     = new string[]{"R_001","R_002","R_003","R_004","R_005","R_006"};
		public static string[] ruleCodeNames = new string[]{"查看就餐数据","设置管理员及权限","查看用户","提醒时间设置","注册用户/IP到服务器","修改用户就餐数据"};

		/// <summary>
		/// 当前用户权限
		/// </summary>
		public static string currentRCS = null;

		/// <summary>
		/// 数据库链接
		/// </summary>
		public static SqlConnection conn = null;

		public static string connStr = "";

		public static void Conn(string sqlServer,string sqlUser,string sqlPwd){

			Public.connStr = "Data Source="+ sqlServer.Trim() +";Initial Catalog=MessAlert;User Id="+ sqlUser.Trim() +";Password="+ sqlPwd.Trim() +";";

			if(!sqlServer.Trim().Equals("") && !sqlUser.Trim().Equals("") && !sqlPwd.Trim().Equals("")){
				try{
					Public.conn = new SqlConnection(Public.connStr);
					Public.conn.Open();
					Public.conn.Close();
				}catch(Exception ex){
					throw new Exception(ex.Message.ToString());
				}
			}else{
				throw new Exception("服务器连接参数设置不正确");
			}
		}

		/// <summary>
		/// 加密字符串
		/// </summary>
		/// <param name="info"></param>
		/// <returns></returns>
		public static string AddSec(string M_Info) {
			int num1 = M_Info.Length;
			int num2 = 0;
			string text1 = "";
			string text2 = "";
			if (num1 != 0) {
				int num3;
				for (int num5 = 0; num5 < num1; num5++) {
					num2 += Convert.ToChar(M_Info.Substring((num1 - num5) - 1, 1).ToString());
				}
				num2 *= 2;
				for (int num6 = 0; num6 < num1; num6++) {
					num3 = num2 % 0x1a;
					num2 -= Convert.ToChar(M_Info.Substring((num1 - num6) - 1, 1).ToString());
					text1 = text1 + Convert.ToChar((int) (0x41 + num3));
				}
				int num4 = 12 - num1;
				num2 *= 3;
				for (int num7 = 0; num7 < num4; num7++) {
					num3 = num2 % 0x1a;
					num2 -= (num3 * 3);
					text2 = text2 + Convert.ToChar((int) (0x41 + num3));
				}
				return (text2 + text1);
			}
			return "";
		}

		/// <summary>
		/// 将给定的字符串按给定的key进行解码
		/// </summary>
		/// <param name="keyChr"></param>
		/// <param name="strText"></param>
		/// <returns></returns>
		public static string DeCode(string keyChr,string strText) {

			string strDecrKey     = keyChr;
			Byte[] byKey          = {};
			Byte[] IV             = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
			Byte[] inputByteArray = new byte[strText.Length];

			try {
				byKey                        = System.Text.Encoding.UTF8.GetBytes(strDecrKey.Substring(0, 8));
				DESCryptoServiceProvider des = new DESCryptoServiceProvider();
				inputByteArray               = Convert.FromBase64String(strText);
				MemoryStream  ms             = new MemoryStream();
				CryptoStream cs              = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);

				cs.Write(inputByteArray, 0, inputByteArray.Length);
				cs.FlushFinalBlock();
				System.Text.Encoding encoding = System.Text.Encoding.UTF8;
				return encoding.GetString(ms.ToArray());
			}catch(Exception ex) {
				return ex.Message;
			}
		}

		/// <summary>
		/// 按给定的key将给定的字符串进行编码
		/// </summary>
		/// <param name="keyChr"></param>
		/// <param name="strText"></param>
		/// <returns></returns>
		public static string Encode(string keyChr,string strText) {

			string strEncrKey = keyChr;
			Byte[] byKey      = {};
			Byte[] IV         = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};

			try {
				byKey                        = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8));
				DESCryptoServiceProvider des = new DESCryptoServiceProvider();
				Byte[] inputByteArray        = System.Text.Encoding.UTF8.GetBytes(strText);
				MemoryStream ms              = new MemoryStream();
				CryptoStream cs              = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);

				cs.Write(inputByteArray, 0, inputByteArray.Length);
				cs.FlushFinalBlock();
				return Convert.ToBase64String(ms.ToArray());
			}catch(Exception ex) {
				return ex.Message.ToString();
			}
		}

		/// <summary>
		/// 根据月份返回这个月的最后一天
		/// </summary>
		/// <param name="month"></param>
		/// <returns></returns>
		public static string GetMonthEndDay(string month){
			if(month != null && month != ""){
				switch(month){
					case "1":
						return "31";
					case "2":
						int year = Int32.Parse(DateTime.Now.Year.ToString());
						if(year % 4 == 0){
							return "29";
						}else{
							return "28";
						}
					case "3":
						return "31";
					case "4":
						return "30";
					case "5":
						return "31";
					case "6":
						return "30";
					case "7":
						return "31";
					case "8":
						return "31";
					case "9":
						return "30";
					case "10":
						return "31";
					case "11":
						return "30";
					case "12":
						return "31";
					default:
						return "30";
				}
			}else{
				return "30";
			}
		}

		/// <summary>
		/// 检查是否有某权限
		/// </summary>
		/// <returns></returns>
		public static bool CheckIsRule(string rc){
			if(Public.currentRCS.IndexOf(rc) > 0 || Public.currentRCS == "CLIENT"){
				return true;
			}else{
				return false;
			}
		}

		#region 使grid根据内容适应列宽

		public static void SizeColumnsToContent (System.Windows.Forms.DataGrid dataGrid, int nRowsToScan,System.Data.DataTable _dataTable,string[] hideColumn) {

			Graphics Graphics = dataGrid.CreateGraphics();

			System.Data .DataTable dataTable =_dataTable;

			DataGridTableStyle tableStyle = null;

			if(dataGrid.TableStyles.Count == 0){
				tableStyle = new DataGridTableStyle();

				dataGrid.TableStyles.Clear();

				tableStyle.MappingName = dataTable.TableName;

				//设置grid中蓝白交错效果
				tableStyle.AlternatingBackColor =Color.FromArgb(224,226,228);
				tableStyle.BackColor =Color.White ;

			}else tableStyle = dataGrid.TableStyles[0];

			tableStyle.PreferredRowHeight = 22;

			try {
				

				if (-1 == nRowsToScan) {
					nRowsToScan = dataTable.Rows.Count;
				}else {
					nRowsToScan = System.Math.Min(nRowsToScan, dataTable.Rows.Count);
				}

				DataGridTextBoxColumn columnStyle;
				int iWidth;

				for (int iCurrCol = 0; iCurrCol < dataTable.Columns.Count; iCurrCol++) {
					DataColumn dataColumn = dataTable.Columns[iCurrCol];

					columnStyle = new DataGridTextBoxColumn();

					columnStyle.TextBox.Enabled = true;
					columnStyle.HeaderText = dataColumn.ColumnName;
					columnStyle.MappingName = dataColumn.ColumnName;

					columnStyle.NullText = "";

					iWidth = (int)(Graphics.MeasureString(columnStyle.HeaderText, dataGrid.Font).Width);

					DataRow dataRow;
					for (int iRow = 0; iRow < nRowsToScan; iRow++) {
						dataRow = dataTable.Rows[iRow];

						if (null != dataRow[dataColumn.ColumnName]) {
							int iColWidth = (int)(Graphics.MeasureString(dataRow.ItemArray[iCurrCol].ToString(), dataGrid.Font).Width);
							iWidth = (int)System.Math.Max(iWidth, iColWidth);
						}
					}

					bool isHide = false;

					if(hideColumn != null){
						for(int x=0;x<hideColumn.Length;x++){
							if(hideColumn[x].ToString().Equals(dataColumn.ColumnName)){
								isHide = true;
							}
						}
					}

					if(isHide) {
						columnStyle.Width = 0;
					}
					else{
						columnStyle.Width = iWidth + 4;
					}

					//只读
					columnStyle.ReadOnly = true;
					columnStyle.TextBox.Cursor = Cursors.Hand;
					columnStyle.TextBox.BackColor = Color.White;
					//

					tableStyle.GridColumnStyles.Add(columnStyle);
				}

				dataGrid.TableStyles.Add(tableStyle);
			}catch(Exception e) {
				MessageBox.Show(e.Message);
			}finally {
				Graphics.Dispose();
			}
		}

		#endregion
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -