Form1.cs 6.68 KB
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using MoyaAdminLib;
using MoyaSignup.Properties;
using ThermoPrinterLibrary;

namespace MoyaSignup
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll")]
        public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);

        [DllImport("user32.dll")]
        public static extern bool UnregisterHotKey(IntPtr hWnd, int id);

        private const int ShutdownHotkey = 1;
        private ThermoPrinter _thermoPrinter;

        public bool AllowShutdown { get; set; }

        //public delegate void ThermoPrinterEvent(ThermoPrinter.Models model);
        //public event ThermoPrinterEvent SetPrinter;

        public Form1()
        {
            RestClient.ApiApplicationKey = Settings.Default.ApiApplicationKey;
            RestClient.ApiPass = Settings.Default.ApiPass;
            RestClient.ApiUser = Settings.Default.ApiUser;
            RestClient.ApiURL = Settings.Default.ApiURL;
            InitializeComponent();

            RegisterHotKey(Handle, ShutdownHotkey, 3, (int) Keys.S);
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x0312 && m.WParam.ToInt32() == ShutdownHotkey)
            {
                AllowShutdown = true;
                Application.Exit();
            }

            base.WndProc(ref m);
        }

        enum Language
        {
            Finnish = 0,
            English = 1
        }
//        Language currentLanguage = Language.English;

        /*
        void changed()
        {
            if (!pictureTaken )
            {

                    this.lblMessage.Text = "Ota kuva ensin painamalla 'Käytä tätä kuvaa' nappia";
                
                this.lblMessage.ForeColor = Color.Red;
                this.btnSave.Enabled = false;
                return;
            }
           
            this.lblMessage.ForeColor = Color.Green;

                this.lblMessage.Text = "OK, varmista tietojen oikeellisuus ja tallenna";
            
            this.btnSave.Enabled = true;
        }*/


        public void SetPrinter(ThermoPrinter.Models model)
        {
            _thermoPrinter = new ThermoPrinter(model);

            try
            {
                _thermoPrinter.Start(Settings.Default.printerPort);
                _thermoPrinter.SetBarcodeWidth(ThermoPrinter.BarcodeWidths.Small);
                _thermoPrinter.SetCharacterAlignment(ThermoPrinter.CharacterAlignments.Center);
                _thermoPrinter.SetPositionHRI(ThermoPrinter.HRIPrintingPositions.Below);
            }
            catch
            {
                _thermoPrinter = null;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            /*if (AutoUpdateLib.AutoUpdateCore.CheckForUpdates(null))
            {
                Application.ExitThread();		//program is required to close by autoupdate (probably updated)
                return;
            }*/

            if (Debugger.IsAttached)
                WindowState = FormWindowState.Normal;

            var form = new ApiSettings();
            form.ShowDialog();

            if (Settings.Default.printerPort != null && Settings.Default.ThermoPrinter > 0)
            {
                var model = (ThermoPrinter.Models) Settings.Default.ThermoPrinter;
                SetPrinter(model);
            }

            userLoginControl1.SetPrinter += SetPrinter;
        }

        /*
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!checkSaveSafety())
            {
                this.btnSave.Enabled = false;
                return;
            }

            this.Cursor = Cursors.WaitCursor;
            this.btnSave.Enabled = false;
            this.Enabled = false;
            this.btnSave.Refresh();

            
            //savePicture(pid);


            MessageBox.Show("Tallennus onnistui.\r\nMene infotiskille, ja kerro heille tämä numero, niin he opastavat sinut eteenpäin.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            
            
            this.Enabled = true;
            this.Cursor = Cursors.Default;
        }*/

        /*
        private void txtName_TextChanged(object sender, EventArgs e)
        {
            changed();
        }*/

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!AllowShutdown)
            {
                e.Cancel = true;
            }
            else
            {
                _thermoPrinter?.Stop();
            }
        }

        private void Form1_Deactivate(object sender, EventArgs e)
        {
            if (!AllowShutdown)
            {
                Activate();
            }
            else
            {
                UnregisterHotKey(Handle, ShutdownHotkey);
            }
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            /* if (e.Alt && e.Control && e.KeyCode == Keys.S)
            {
                AllowShutdown = true;
                Application.Exit();
                return;
            }
            if (e.Alt && e.KeyCode == Keys.F5)
            {
                //userDetailsEditor1.SetPrinter(ThermoPrinterLibrary.ThermoPrinter.Models.IDP3210);
                //MessageBox.Show("Printteri: IDP3210");
            }*/
        }


        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Alt && e.Control && e.KeyCode == Keys.S)
            {
                AllowShutdown = true;
                Close();
            }
        }


        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
/*
            if (e.Button == MouseButtons.Right)
            {
                allowShutdown = true;
                Application.Exit();
                return;
            }*/
        }


        private void button1_Click(object sender, EventArgs e)
        {
            AllowShutdown = true;
            Application.Exit();
        }

        private void userLoginControl1_LoginOk(object sender, EventArgs e)
        {
            userLoginControl1.Visible = false;
            userDetailsEditor1.LoadUser(userLoginControl1.CurrentUser);
            userDetailsEditor1.ThermoPrinter = _thermoPrinter;
            userLoginControl1.Clear();
            userDetailsEditor1.Visible = true;
            //userLoginControl1.CurrentUser;
        }

        private void userDetailsEditor1_Load(object sender, EventArgs e)
        {
        }

        private void userDetailsEditor1_CloseView(object sender, EventArgs e)
        {
            userDetailsEditor1.Visible = false;
            userLoginControl1.Visible = true;
        }
    }
}