using LB_SmartVisionCommon;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MessageBox = System.Windows.Forms.MessageBox;
namespace LB_SmartVisionLoginUI
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
///
/// 调用Windows的API库user32.dll中的消息发送机制,可实现跨程序数据发送
///
///
///
///
///
///
[DllImport("user32.dll", EntryPoint = "SendMessageA")]
public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
///
/// 当前登录的用户名,也需要共享给整个工程
///
public static string userName;//
///
/// 用户密码
///
private static string userPassword;
///
/// 是否退出
///
public bool isQuit = false;
///
/// 当前用户
///
public bool correctUser = false;
///
/// 是否登出
///
public bool login_on_off = false;
private MainWindow()
{
InitializeComponent();
correctUser = false;
}
private static MainWindow instanceLoginandConfirmation = null;
///
///
///
///
public static MainWindow InstanceLoginandConfirmation()
{
if (instanceLoginandConfirmation == null)
{
instanceLoginandConfirmation = new MainWindow();
}
return instanceLoginandConfirmation;
}
///
/// 关闭
///
///
///
private void test_Close(object sender, MouseButtonEventArgs e)
{
correctUser = false;
isQuit = true;
this.Close();
}
///
/// 最小化
///
///
///
private void test_Min(object sender, MouseButtonEventArgs e)
{
this.WindowState = WindowState.Minimized;
}
private void Btn_Cancel_Click(object sender, RoutedEventArgs e)
{
this.Close();
try
{
Process[] processes = System.Diagnostics.Process.GetProcesses(); //获得所有进程
foreach (Process p in processes)
{
if (p.ProcessName == "ShockPadMeasurementSystem" && p.StartTime < DateTime.Now.AddMilliseconds(-300))
{
p.Kill();
}
}
}
catch { }
}
private void Btn_KeyBoard_Click(object sender, RoutedEventArgs e)
{
System.Diagnostics.Process.Start(@"C:\WINDOWS\system32\osk.exe");
}
private void Btn_Login_Click(object sender, RoutedEventArgs e)
{
login_on_off = true;
loginon();
// 在此处添加事件处理程序实现。
if (correctUser)//如果用户名密码正确,合法登录,则获得当前用户名和级别。同时关闭登录窗体
{
userName = this.Txt_UserName.Text.Trim().ToLower();
userPassword = this.Txt_UserPassWord.Password;
//closeLoginFrm();
this.Hide();
}
}
private void loginon()
{
if (!UserManager.Instance.Login(this.Txt_UserName.Text.Trim(), this.Txt_UserPassWord.Password))
{
MessageBox.Show("请输入正确的用户名和密码.",
"登录失败!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
correctUser = false;
return;
}
else// Valid user, pass the authentication
{
userName = this.Txt_UserName.Text.Trim();
userPassword = this.Txt_UserPassWord.Password;
correctUser = true;
isQuit = false;
}
}
///
/// 关闭
///
public void closeLoginFrm()
{
this.ShowInTaskbar = false;//点击登录后去掉任务栏图标
instanceLoginandConfirmation = null;
this.Close();
}
private void Txt_UserPassWord_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key== Key.Enter)
{
login_on_off = true;
loginon();
// 在此处添加事件处理程序实现。
if (correctUser)
{
userName = this.Txt_UserName.Text.Trim().ToLower();
userPassword = this.Txt_UserPassWord.Password;
isQuit = false;
//closeLoginFrm();
}
}
else if (e.Key == Key.Escape)
{
correctUser = false;
isQuit = true;
this.Close();
}
}
private void Window_Closing(object sender, CancelEventArgs e)
{
this.Txt_UserName.Text = "";
this.Txt_UserPassWord.Password = "";
}
}
}