Exferia/Exferia_Formularios/Exferia_Formularios/PE_ValidacionLicencia.cs

333 lines
13 KiB
C#

using Exferia_Aplicacion.General;
using Exferia_Aplicacion.Modelos_ServiciosWeb;
using Exferia_Aplicacion.Visualizacion;
using Exferia_General;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
namespace Exferia_Formularios
{
public partial class PE_ValidacionLicencia : Form
{
#region Librerias Externas
[DllImport("dwmapi.dll")]
public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);
[DllImport("dwmapi.dll")]
public static extern int DwmIsCompositionEnabled(ref int pfEnabled);
[DllImport("dwmapi.dll")]
public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMarInset);
#endregion
#region Variables privadas
private BackgroundWorker g_bgw_ValidacionLicencia = null;
private bool g_bol_ValidandoLicencia = false;
private PE_Esperando g_frm_PE_Espera = null;
#endregion
#region Propiedad Publicas
private bool g_bol_ValidacionCorrecta = false;
public bool PE_ValidacionLicencia_ValidacionCorrecta
{
set
{
g_bol_ValidacionCorrecta = value;
}
get
{
return g_bol_ValidacionCorrecta;
}
}
//Propiedad que define el color del titulo y los bordes de la pantalla
public Color PE_ValidacionLicencia_ColorTituloyBordes
{
set
{
lbl_Titulo.BackColor = value;
pnl_Titulo.BackColor = value;
pnl_BordeDerecho.BackColor = value;
pnl_BordeInferior.BackColor = value;
pnl_BordeIzquierdo.BackColor = value;
}
get
{
return lbl_Titulo.BackColor;
}
}
#endregion
public PE_ValidacionLicencia()
{
InitializeComponent();
Repintar.Empezar(this);
//Tarea Asincrona para Comprobar la Validacion de la Activacion
g_bgw_ValidacionLicencia = new BackgroundWorker();
g_bgw_ValidacionLicencia.DoWork += new DoWorkEventHandler(TareaAsincrona_ValidacionLicencia_DoWork);
g_bgw_ValidacionLicencia.RunWorkerCompleted += new RunWorkerCompletedEventHandler(TareaAsincrona_ValidacionLicencia_Completed);
g_bgw_ValidacionLicencia.WorkerReportsProgress = true;
g_bgw_ValidacionLicencia.WorkerSupportsCancellation = true;
ex_txt_NumeroLicencia.Exferia_TextBox_Foco();
}
private void ex_btn_Salir_Click(object sender, EventArgs e)
{
this.Close();
}
private void ex_btn_Aceptar_Click(object sender, EventArgs e)
{
Validar();
}
#region Tarea Asincrona Validacion de la Licencia
private void Validar()
{
try
{
if (g_bol_ValidandoLicencia == false)
{
g_bol_ValidandoLicencia = true;
g_frm_PE_Espera = new PE_Esperando();
g_frm_PE_Espera.Show();
//## Arrancar Tarea Asincrona ######################
g_bgw_ValidacionLicencia.RunWorkerAsync();
//##################################################
}
}
catch (Exception ex)
{
Control_Errores.Errores_Log("",ex, nameof(PE_ValidacionLicencia) + "/" + nameof(Validar));
g_bol_ValidandoLicencia = false;
}
}
private void TareaAsincrona_ValidacionLicencia_DoWork(object sender, DoWorkEventArgs e)
{
INTERNO_ServicioWeb_Licencia_Modelo mdl_INTERNO_ServicioWeb_Licencia_Modelo = new INTERNO_ServicioWeb_Licencia_Modelo();
mdl_INTERNO_ServicioWeb_Licencia_Modelo.respuesta = Variables.G_INT_VALIDARLICENCIA_RESPUESTA_ERROR;
try
{
//Llama al Servicio Web para Actualizar
//Cargamos el ensamblado #########################################################################################################################################################################
Assembly m_assembly_1 = Assembly.LoadFrom(Variables.G_STR_LIBRERIA_RUTAINICIAL + @"\" + Variables.G_STR_LIBRERIA_SERVICIOWEB + ".dll");
//Obtenemos el tipo de la clase
Type m_type_1 = m_assembly_1.GetType("" + Variables.G_STR_LIBRERIA_SERVICIOWEB + "." + Variables.G_STR_LIBRERIA_DATOSSERVICIOWEB);
//creamos la instancia
var obj_Clase_1 = Activator.CreateInstance(m_type_1);
//Cargamos el metodo solicitado
MethodInfo obj_MethodInfo_1 = obj_Clase_1.GetType().GetMethod(Variables.G_STR_LIBRERIA_DATOSSERVICIOWEB_PROCEDIMIENTO_VALIDAR_LICENCIA, BindingFlags.Instance | BindingFlags.NonPublic);
//Obtener valores
object[] arr_Parametros_1 = { long.Parse(ex_txt_CodigoCliente.Text),
ex_txt_NumeroLicencia.Text
};
//Pasamos los parametros al metodo y lo ejecutamos
mdl_INTERNO_ServicioWeb_Licencia_Modelo = (INTERNO_ServicioWeb_Licencia_Modelo)obj_MethodInfo_1.Invoke(obj_Clase_1, arr_Parametros_1);
//##################################################################################################################################################################################################
//Esta validado correctamente
if (mdl_INTERNO_ServicioWeb_Licencia_Modelo.respuesta.Equals(Variables.G_INT_VALIDARLICENCIA_RESPUESTA_NUMEROLICENCIA_CORRECTO))
{
//Actulizar variables validacion de licencia
Variables.G_MDL_LICENCIA_VALIDADA = mdl_INTERNO_ServicioWeb_Licencia_Modelo;
Funciones_FicherosXML.CrearDocumentoXML_Licencia(Variables.G_MDL_LICENCIA_VALIDADA);
Datos_Generales.PRV_OpcionesDisponiblesParaCliente_Actualizar_Segun_ProductosLicenciasFechas();
Datos_Generales.PRV_Productos_Cargar();
}
}
catch (ThreadAbortException ex)
{
Control_Errores.Errores_Log("", ex, nameof(PE_ValidacionLicencia) + "" + nameof(TareaAsincrona_ValidacionLicencia_DoWork));
mdl_INTERNO_ServicioWeb_Licencia_Modelo.respuesta = Variables.G_INT_VALIDARLICENCIA_RESPUESTA_ERROR_TAREAASINCRONA;
}
catch (Control_Errores)
{
mdl_INTERNO_ServicioWeb_Licencia_Modelo.respuesta = Variables.G_INT_VALIDARLICENCIA_RESPUESTA_ERROR_TAREAASINCRONA;
}
catch (Exception ex)
{
Control_Errores.Errores_Log("", ex, nameof(PE_ValidacionLicencia) + "" + nameof(TareaAsincrona_ValidacionLicencia_DoWork));
mdl_INTERNO_ServicioWeb_Licencia_Modelo.respuesta = Variables.G_INT_VALIDARLICENCIA_RESPUESTA_ERROR_TAREAASINCRONA;
}
finally
{
e.Result = mdl_INTERNO_ServicioWeb_Licencia_Modelo;
}
}
private void TareaAsincrona_ValidacionLicencia_Completed(object sender, RunWorkerCompletedEventArgs e)
{
try
{
g_frm_PE_Espera.Dispose();
INTERNO_ServicioWeb_Licencia_Modelo mdl_INTERNO_ServicioWeb_Licencia_Modelo = (INTERNO_ServicioWeb_Licencia_Modelo)e.Result;
// Hubo un Error en tarea Asincrna
if (mdl_INTERNO_ServicioWeb_Licencia_Modelo.respuesta.Equals(Variables.G_INT_VALIDARLICENCIA_RESPUESTA_ERROR_TAREAASINCRONA))
{
Mensajes.MostrarMensaje(Mensajes.G_STR_LICENCIA_ERROR_TAREAASINCRONA);
}
// Hubo un Error
else if (mdl_INTERNO_ServicioWeb_Licencia_Modelo.respuesta.Equals(Variables.G_INT_VALIDARLICENCIA_RESPUESTA_ERROR))
{
Mensajes.MostrarMensaje(Mensajes.G_STR_LICENCIA_ERROR_SERVICIO);
}
else if (mdl_INTERNO_ServicioWeb_Licencia_Modelo.respuesta.Equals(Variables.G_INT_VALIDARLICENCIA_RESPUESTA_NUMEROLICENCIA_INCORRECTO))
{
Mensajes.MostrarMensaje(Mensajes.G_STR_LICENCIA_VALIDACIONES_LICENCIANOEXISTE);
}
else if (mdl_INTERNO_ServicioWeb_Licencia_Modelo.respuesta.Equals(Variables.G_INT_VALIDARLICENCIA_RESPUESTA_NUMEROLICENCIA_CORRECTO))
{
g_bol_ValidacionCorrecta = true;
this.Close();
}
}
catch (Exception ex)
{
Control_Errores.Errores_Log("",ex, nameof(PE_ValidacionLicencia) + "/" + nameof(TareaAsincrona_ValidacionLicencia_Completed));
}
finally
{
g_bol_ValidandoLicencia = false;
}
}
#endregion
#region Control Grafico de la Pantalla
protected override void WndProc(ref Message m)
{
//Sombra de la pantalla ..................................................
if (m.Msg == WM_NCPAINT)// box shadow
{
if (g_bol_HabilitarSombra)
{
var v = 2;
DwmSetWindowAttribute(Handle, 2, ref v, 4);
MARGINS margins = new MARGINS()
{
bottomHeight = 1,
leftWidth = 1,
rightWidth = 1,
topHeight = 1
};
DwmExtendFrameIntoClientArea(Handle, ref margins);
}
}
base.WndProc(ref m);
if (m.Msg == WM_NCHITTEST && (int)m.Result == HTCLIENT) // drag the form
m.Result = (IntPtr)HTCAPTION;
}
private void PE_Mensajes_Resize(object sender, EventArgs e)
{
pnl_BordeDerecho.Location = new Point(Width - 3, pnl_BordeDerecho.Location.Y);
pnl_BordeDerecho.Size = new Size(2, Height - 3);
pnl_BordeInferior.Location = new Point(pnl_BordeInferior.Location.X, Height - 3);
pnl_BordeInferior.Size = new Size(Width - 4, 2);
pnl_Titulo.Size = new Size(Width - 4, pnl_Titulo.Height);
}
#endregion
#region Crear Sombra de la pantalla
[DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
private static extern IntPtr CreateRoundRectRgn
(
int nLeftRect, // x-coordinate of upper-left corner
int nTopRect, // y-coordinate of upper-left corner
int nRightRect, // x-coordinate of lower-right corner
int nBottomRect, // y-coordinate of lower-right corner
int nWidthEllipse, // height of ellipse
int nHeightEllipse // width of ellipse
);
private bool g_bol_HabilitarSombra; // variables for box shadow
private const int CS_DROPSHADOW = 0x00020000;
private const int WM_NCPAINT = 0x0085;
private const int WM_ACTIVATEAPP = 0x001C;
public struct MARGINS // struct for box shadow
{
public int leftWidth;
public int rightWidth;
public int topHeight;
public int bottomHeight;
}
private const int WM_NCHITTEST = 0x84; // variables for dragging the form
private const int HTCLIENT = 0x1;
private const int HTCAPTION = 0x2;
protected override CreateParams CreateParams
{
get
{
g_bol_HabilitarSombra = CheckAeroEnabled();
CreateParams cp = base.CreateParams;
if (!g_bol_HabilitarSombra)
cp.ClassStyle |= CS_DROPSHADOW;
return cp;
}
}
private bool CheckAeroEnabled()
{
if (Environment.OSVersion.Version.Major >= 6)
{
int enabled = 0;
DwmIsCompositionEnabled(ref enabled);
return (enabled == 1) ? true : false;
}
return false;
}
#endregion
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Enter)) // Aceptar
{
Validar();
return true;
}
else if (keyData == Keys.Escape) // Salir
{
Close();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
}
}