Login.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Data;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.IO;
  6. using System.Windows.Forms;
  7. using UAS_LabelMachine.Entity;
  8. namespace UAS_LabelMachine
  9. {
  10. public partial class Login : Form
  11. {
  12. DataHelper dh;
  13. DataTable dt;
  14. public Login()
  15. {
  16. InitializeComponent();
  17. StartPosition = FormStartPosition.CenterScreen;
  18. }
  19. protected override void WndProc(ref Message m)
  20. {
  21. //拦截双击标题栏、移动窗体的系统消息
  22. if (m.Msg != 0xA3)
  23. base.WndProc(ref m);
  24. }
  25. private void Login_Load(object sender, EventArgs e)
  26. {
  27. dh = new DataHelper();
  28. //获取账套信息
  29. dt = (DataTable)dh.ExecuteSql("select ma_function,ms_pwd,ma_user from datacenter.master where ma_enable<>0", "select");
  30. MasterCombox.DisplayMember = "ma_function";
  31. MasterCombox.ValueMember = "ma_user";
  32. MasterCombox.DataSource = dt;
  33. //读取上次登陆时的用户名和选择的账套
  34. UserName.Text = Properties.Settings.Default.LastLoginUser;
  35. MasterCombox.Text = Properties.Settings.Default.LastLoginMaster;
  36. IP.Text = Properties.Settings.Default.IPAddress;
  37. //如果上次的用户名不为空则直接跳到密码输入框
  38. if (UserName.Text != "")
  39. PassWord.Select();
  40. if (!Directory.Exists("Log"))//若文件夹不存在则新建文件夹
  41. Directory.CreateDirectory("Log"); //新建文件夹
  42. string sysdisc = Environment.GetEnvironmentVariable("windir").Substring(0, 1);
  43. if (!Directory.Exists(sysdisc + @":\打印标签"))
  44. Directory.CreateDirectory(sysdisc + @":\打印标签");
  45. FileStream fs = new FileStream("Log/" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
  46. }
  47. private void LoginIcon_Click(object sender, EventArgs e)
  48. {
  49. //根据匹配的账套在DataTable查找到对应的密码
  50. string Ms_Pwd = "";
  51. for (int i = 0; i < dt.Rows.Count; i++)
  52. {
  53. if (MasterCombox.SelectedValue == dt.Rows[i]["ma_user"])
  54. Ms_Pwd = dt.Rows[i]["ms_pwd"].ToString();
  55. }
  56. //切换至用户选择的数据源
  57. string ConnectionString = "Data Source=" + IP.Text + "/orcl;User ID=" + MasterCombox.SelectedValue + ";PassWord=" + Ms_Pwd;
  58. DataHelper.DBConnectionString = ConnectionString;
  59. //设置了Connection,重新执行构造函数,重置数据库连接
  60. dh = new DataHelper();
  61. if (dh.CheckExist("employee", "em_code='" + UserName.Text + "' and em_password='" + PassWord.Text + "'"))
  62. {
  63. dt = (DataTable)dh.ExecuteSql("select em_name,em_type from employee where em_code='" + UserName.Text + "'", "select");
  64. User.UserName = dt.Rows[0]["em_name"].ToString();
  65. User.UserCode = UserName.Text;
  66. User.UserAccountType = dt.Rows[0]["em_type"].ToString();
  67. //保存此次登陆的用户名和密码
  68. Properties.Settings.Default.LastLoginUser = UserName.Text;
  69. Properties.Settings.Default.LastLoginMaster = MasterCombox.Text;
  70. Properties.Settings.Default.IPAddress = IP.Text;
  71. Properties.Settings.Default.Save();
  72. //弹出标签打印的主界面
  73. UAS_出货标签打印 main = new UAS_出货标签打印();
  74. main.StartPosition = FormStartPosition.CenterScreen;
  75. Hide();
  76. main.ShowDialog();
  77. Close();
  78. }
  79. else
  80. MessageBox.Show("用户名或者密码错误!");
  81. }
  82. /// <summary>
  83. /// 绘制蓝色的背景图
  84. /// </summary>
  85. /// <param name="sender"></param>
  86. /// <param name="e"></param>
  87. private void Login_Paint(object sender, PaintEventArgs e)
  88. {
  89. //简单的画一个浅蓝色的背景
  90. Graphics g = e.Graphics;
  91. GraphicsPath Rect = new GraphicsPath();
  92. Pen p = new Pen(Color.CadetBlue, 2);
  93. Rectangle rect = new Rectangle(141, 80, 600, 300);
  94. LinearGradientBrush b3 = new LinearGradientBrush(rect, Color.AliceBlue, Color.LightBlue, LinearGradientMode.Vertical);
  95. g.FillRectangle(b3, rect);
  96. e.Graphics.DrawRectangle(p, rect);
  97. }
  98. private void PassWord_KeyDown(object sender, KeyEventArgs e)
  99. {
  100. if (e.KeyCode == Keys.Enter)
  101. {
  102. LoginIcon_Click(sender, e);
  103. }
  104. }
  105. private void Login_FormClosing(object sender, FormClosingEventArgs e)
  106. {
  107. Application.Exit();
  108. }
  109. private void IP_KeyDown(object sender, KeyEventArgs e)
  110. {
  111. if (e.KeyCode == Keys.Enter)
  112. DataHelper.DBConnectionString = "Data Source=" + IP.Text + "/orcl;User ID=DATACENTER;PassWord=select!#%*(";
  113. }
  114. }
  115. }