Login.cs 5.2 KB

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