Login.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. FileInfo f = new FileInfo(@"C:\打印标签\94 大疆 中盒.lab");
  28. Console.WriteLine(f.LastWriteTime);
  29. dh = new DataHelper();
  30. //获取账套信息
  31. dt= (DataTable)dh.ExecuteSql("select ma_function,ms_pwd,ma_user from master where ma_enable<>0", "select");
  32. MasterCombox.DisplayMember = "ma_function";
  33. MasterCombox.ValueMember = "ma_user";
  34. MasterCombox.DataSource = dt;
  35. //读取上次登陆时的用户名和选择的账套
  36. UserName.Text = Properties.Settings.Default.LastLoginUser;
  37. MasterCombox.Text = Properties.Settings.Default.LastLoginMaster;
  38. IP.Text = Properties.Settings.Default.IPAddress;
  39. //如果上次的用户名不为空则直接跳到密码输入框
  40. if (UserName.Text != "")
  41. {
  42. PassWord.Select();
  43. }
  44. if (!Directory.Exists("Log"))//若文件夹不存在则新建文件夹
  45. {
  46. Directory.CreateDirectory("Log"); //新建文件夹
  47. }
  48. string sysdisc = Environment.GetEnvironmentVariable("windir").Substring(0, 1);
  49. if (!Directory.Exists(sysdisc + @":\打印标签"))
  50. Directory.CreateDirectory(sysdisc + @":\打印标签");
  51. FileStream fs = new FileStream("Log/" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
  52. }
  53. private void LoginIcon_Click(object sender, EventArgs e)
  54. {
  55. //根据匹配的账套在DataTable查找到对应的密码
  56. string Ms_Pwd = "";
  57. for (int i = 0; i < dt.Rows.Count; i++)
  58. {
  59. if (MasterCombox.SelectedValue == dt.Rows[i]["ma_user"])
  60. {
  61. Ms_Pwd = dt.Rows[i]["ms_pwd"].ToString();
  62. }
  63. }
  64. //切换至用户选择的数据源
  65. string ConnectionString = "Data Source=" + IP.Text + "/orcl;User ID=" + MasterCombox.SelectedValue + ";PassWord=" + Ms_Pwd;
  66. DataHelper.DBConnectionString = ConnectionString;
  67. //设置了Connection,重新执行构造函数,重置数据库连接
  68. dh = new DataHelper();
  69. if (dh.CheckExist("employee", "em_code='" + UserName.Text + "' and em_password='" + PassWord.Text + "'"))
  70. {
  71. dt = (DataTable)dh.ExecuteSql("select em_name,em_type from employee where em_code='" + UserName.Text + "'", "select");
  72. User.UserName = dt.Rows[0]["em_name"].ToString();
  73. User.UserCode = UserName.Text;
  74. User.UserAccountType = dt.Rows[0]["em_type"].ToString();
  75. //保存此次登陆的用户名和密码
  76. Properties.Settings.Default.LastLoginUser = UserName.Text;
  77. Properties.Settings.Default.LastLoginMaster = MasterCombox.Text;
  78. Properties.Settings.Default.IPAddress = IP.Text;
  79. Properties.Settings.Default.Save();
  80. //弹出标签打印的主界面
  81. UAS_出货标签打印 main = new UAS_出货标签打印();
  82. main.StartPosition = FormStartPosition.CenterScreen;
  83. Hide();
  84. main.ShowDialog();
  85. Close();
  86. }
  87. else
  88. {
  89. MessageBox.Show("用户名或者密码错误!");
  90. }
  91. }
  92. /// <summary>
  93. /// 绘制蓝色的背景图
  94. /// </summary>
  95. /// <param name="sender"></param>
  96. /// <param name="e"></param>
  97. private void Login_Paint(object sender, PaintEventArgs e)
  98. {
  99. //简单的画一个浅蓝色的背景
  100. Graphics g = e.Graphics;
  101. GraphicsPath Rect = new GraphicsPath();
  102. Pen p = new Pen(Color.CadetBlue, 2);
  103. Rectangle rect = new Rectangle(141, 80, 600, 300);
  104. LinearGradientBrush b3 = new LinearGradientBrush(rect, Color.AliceBlue, Color.LightBlue, LinearGradientMode.Vertical);
  105. g.FillRectangle(b3, rect);
  106. e.Graphics.DrawRectangle(p, rect);
  107. }
  108. private void PassWord_KeyDown(object sender, KeyEventArgs e)
  109. {
  110. if (e.KeyCode == Keys.Enter)
  111. {
  112. LoginIcon_Click(sender, e);
  113. }
  114. }
  115. private void Login_FormClosing(object sender, FormClosingEventArgs e)
  116. {
  117. Application.Exit();
  118. }
  119. private void IP_KeyDown(object sender, KeyEventArgs e)
  120. {
  121. if (e.KeyCode == Keys.Enter)
  122. DataHelper.DBConnectionString = "Data Source=" + IP.Text + "/orcl;User ID=DATACENTER;PassWord=select!#%*(";
  123. }
  124. }
  125. }