using ScaBox30.Model; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Text.Json; using System.Threading.Tasks; using System.Windows.Forms; namespace ScaBox30 { public partial class Usuario : Form { public Usuario() { InitializeComponent(); InitializeItems(); InitializeButtons(); LoadUsers(); } private void LoadUsers() { CreateUserJson(Globals.auth_json); LoadUserJson(Globals.auth_json); if (Globals.users.Count > 0) { foreach (var user in Globals.users) { comboUser.Items.Add(user.user); } comboUser.SelectedIndex = 0; } } private void CreateUserJson(string jsonfile) { if (!File.Exists(jsonfile)) { List users = new List { }; users.Add(new User() { user = "admin", password = "admin123" }); var json = JsonSerializer.Serialize>(users); File.WriteAllText(jsonfile, json); users.Clear(); } } private void LoadUserJson(string jsonfile) { if (File.Exists(jsonfile)) { Globals.users.Clear(); string jsoncontent = File.ReadAllText(jsonfile); Globals.users = JsonSerializer.Deserialize>(jsoncontent); jsoncontent = ""; } } private void buttonOk_Click(object sender, EventArgs e) { if (Globals.users.Count() > 0) { var res = Globals.users.Find(u => u.password == textPassword.Text); if (res != null) { Globals.valid_user = true; Globals.users.Clear(); this.Close(); } else { textPassword.Text = ""; Globals.valid_user = false; Globals.users.Clear(); this.Close(); } } } private void buttonCancel_Click(object sender, EventArgs e) { this.Close(); } private void button1_Click(object sender, EventArgs e) { if (Globals.users.Count() > 0) { var res = Globals.users.Find(u => u.password == textPassword.Text); if (res != null) { Globals.valid_user = true; Globals.users.Clear(); this.Close(); } else { textPassword.Text = ""; Globals.valid_user = false; Globals.users.Clear(); this.Close(); } } } private void button2_Click(object sender, EventArgs e) { this.Close(); } private void InitializeItems() { this.FormBorderStyle = FormBorderStyle.None; this.Padding = new Padding(Globals.borderSize);//Set border size this.btnClose.DialogResult = DialogResult.Cancel; this.button1.DialogResult = DialogResult.OK; this.labelCaption.Text = this.Text; this.panelTitleBar.BackColor = Globals.primaryColor; this.panelTitleBar.Padding = new Padding(0, 0, 0, Globals.borderSize); this.BackColor = Globals.primaryColor; } private void InitializeButtons() { int xCenter = (this.panelButtons.Width - button1.Width) / 2; int yCenter = (this.panelButtons.Height - button1.Height) / 2; //OK Button button1.Visible = true; button1.Location = new Point(xCenter - (button1.Width / 2) - 5, yCenter); button1.Text = "Ok"; button1.DialogResult = DialogResult.OK;//Set DialogResult //Cancel Button button2.Visible = true; button2.Location = new Point(xCenter + (button2.Width / 2) + 5, yCenter); button2.Text = "Cancel"; button2.DialogResult = DialogResult.Cancel;//Set DialogResult button2.BackColor = Globals.colors.retry_cancell; } #region -> Drag Form [DllImport("user32.DLL", EntryPoint = "SendMessage")] private extern static void SendMessage(System.IntPtr hWnd, int wMsg, int wParam, int lParam); [DllImport("user32.DLL", EntryPoint = "ReleaseCapture")] private extern static void ReleaseCapture(); private void panelTitleBar_MouseDown(object sender, MouseEventArgs e) { ReleaseCapture(); SendMessage(this.Handle, 0x112, 0xf012, 0); } #endregion } }