#region Using directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using SmartInstitute.Automation.Commands.Framework;
using SmartInstitute.Client.Automation.Commands.Security;
using SmartInstitute.Client.Automation;
using System.Reflection;
#endregion
namespace SmartInstitute.Client.Forms
{
partial class LoginScreen : Form
{
private Color DarkBlue = Color.FromArgb(255, 0, 48, 156);
private Color LightBlue = Color.FromArgb(255, 90, 126, 220);
public LoginScreen()
{
InitializeComponent();
this.ResizePanels();
this.lblServer.Text = _Application.Settings.WebServiceUrl;
Version version = Assembly.GetExecutingAssembly().GetName().Version;
this.lblServer.Text += " " + version.ToString();
}
private void pnlCenter_Paint(object sender, PaintEventArgs e)
{
Color gradientEnd = Color.FromArgb(95, Color.White);
Color gradientStart = Color.FromArgb(10, Color.White);
// Draw the vertical separator at the middle
// First half of the separator
Rectangle rect = new Rectangle((this.pnlCenter.ClientRectangle.Width / 2) - 2, 10, 1, (this.pnlCenter.ClientRectangle.Height / 2) - 10);
using (LinearGradientBrush brush = new LinearGradientBrush(rect, gradientStart, gradientEnd, 90))
{
e.Graphics.FillRectangle(brush, rect);
}
// Second half of the separator
rect.Offset(0, (this.pnlCenter.ClientRectangle.Height / 2) - 10);
using (LinearGradientBrush brush = new LinearGradientBrush(rect, gradientEnd, gradientStart, 90))
{
e.Graphics.FillRectangle(brush, rect);
}
// Draw the top and bottom gradient bars
// left half
Rectangle rectHorizontal = new Rectangle(0, 0, this.pnlCenter.ClientRectangle.Width / 2, 2);
// right half
Rectangle rectHorizontal2 = rectHorizontal;
rectHorizontal2.Offset(this.pnlCenter.ClientRectangle.Width / 2, 0);
// draw left half on top and bottom
using (LinearGradientBrush brush = new LinearGradientBrush(rectHorizontal, gradientStart, gradientEnd, 0f))
{
e.Graphics.FillRectangle(brush, rectHorizontal);
rectHorizontal.Offset(0, this.pnlCenter.ClientRectangle.Height - 2);
e.Graphics.FillRectangle(brush, rectHorizontal);
}
// draw right half on top and bottom
using (LinearGradientBrush brush = new LinearGradientBrush(rectHorizontal, gradientEnd, gradientStart, 0f))
{
e.Graphics.FillRectangle(brush, rectHorizontal2);
rectHorizontal2.Offset(0, this.pnlCenter.ClientRectangle.Height - 2);
e.Graphics.FillRectangle(brush, rectHorizontal2);
}
}
private void linkClose_Click(object sender, EventArgs e)
{
this.Close();
}
private void pnlCenter_Resize(object sender, EventArgs e)
{
this.ResizePanels();
}
private void ResizePanels()
{
this.Invalidate();
int centerX = (this.pnlCenter.ClientRectangle.Width / 2);
// the logo is left to the center
this.pnlLogo.Left = centerX - this.pnlLogo.Width - 10;
// the login box is righ to the center
this.pnlLogin.SetBounds(centerX + 10, this.pnlLogin.Top, this.pnlCenter.ClientRectangle.Width - centerX - 10, this.pnlLogin.Height);
}
/// <summary>
/// Start login process
/// </summary>
private void Login()
{
string[] credential = new string[] { txtUserName.Text, txtPassword.Text };
lblProgress.Visible = true;
lblProgress.Refresh();
loginProgress.Visible = true;
loginProgress.Value = loginProgress.Minimum;
worker.RunWorkerAsync(credential);
}
private void btnOK_Click(object sender, EventArgs e)
{
this.Login();
}
/// <summary>
/// Call web service and validate credentials
/// </summary>
/// <param name="userName"></param>
/// <param name="password"></param>
/// <returns></returns>
private void Authenticate(string userName, string password)
{
AuthenticateCommand command = new AuthenticateCommand(userName, password);
((ICommand)command).Execute();
worker.ReportProgress(1, "Loading...");
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.DrawImage(SmartInstitute.App.Properties.Resources.RightArrow, 0, 0);
if (base.Focused)
{
e.Graphics.DrawRectangle(Pens.Yellow, e.ClipRectangle);
}
}
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
// This method will run on a thread other than the UI thread.
// Be sure not to manipulate any Windows Forms controls created
// on the UI thread from this method.
string[] credential = (string[])e.Argument;
string userName = credential[0];
string password = credential[1];
this.Authenticate(userName, password);
}
private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
lblProgress.Text = (string)e.UserState;
lblProgress.Refresh();
}
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (null != e.Error)
{
MessageBox.Show(this, e.Error.Message, "Login Problem",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
this.DialogResult = DialogResult.OK;
this.Close();
}
lblProgress.Visible = loginProgress.Visible = false;
}
private void LoginScreen_Load(object sender, EventArgs e)
{
txtUserName.Focus();
}
}
}