using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using US.Browser.IE.BLL;


namespace US.Browser.IE
{
    /// <summary>
    /// 文件下载区域
    /// </summary>
    [System.Runtime.InteropServices.ComVisible(true)]
    public partial class UpdateCue : Form
    {
        public static StaticClass _USE;
        public static UpdateCue _UDE;
        System.Windows.Forms.Timer _UT;

        /// <summary>
        /// 初始化更新
        /// </summary>
        /// <param name="USE"></param>
        /// <returns></returns>
        public static UpdateCue NewUpCF(StaticClass USE)
        {
            if (_UDE == null) { _UDE = new UpdateCue(); _USE = USE; }
            return _UDE;
        }

        /// <summary>
        /// 更新页面初始
        /// </summary>
        public UpdateCue()
        {
            InitializeComponent(); UpC.ObjectForScripting = this; UpC.ScriptErrorsSuppressed = true;
            Point _UTL = new Point((Int32)(Screen.PrimaryScreen.WorkingArea.Width - this.Width) / 2, (Int32)(Screen.PrimaryScreen.WorkingArea.Height - this.Height) / 2);
            this.PointToScreen(_UTL); this.Location = _UTL; UpC.IsWebBrowserContextMenuEnabled = false;
            UpC.Url = new System.Uri(Application.StartupPath + "\\html\\UpdateCue.html"); IsGX();
        }

        /// <summary>
        /// 判断是否需要更新浏览器
        /// </summary>
        private void IsGX()
        {
            _UT = new System.Windows.Forms.Timer();
            _UT.Tick += new EventHandler(_UT_Elapsed);
            _UT.Interval = 3600000; _UT.Start();//3600000
        }

        /// <summary>
        /// 获取版本
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void _UT_Elapsed(object sender, EventArgs e)
        {
            if (BLL.USIE.isnet()) { showp(true); }
        }

        /// <summary>
        /// 检测更新
        /// </summary>
        public bool showp(bool UTF)
        {
            string UST = USClientCommunicate.GetPage("http://seo.1473.cn/Client/ver.txt");
            if (UST.Length > 1 && UST != StaticClass.ver) { _UT.Stop(); _UT.Dispose(); this.Visible = true; if (UTF) { this.SendToBack(); } else { this.BringToFront(); } return true; } return false;
        }

        /// <summary>
        /// 网络上下载安装包
        /// </summary>
        public void UpCNow()
        {
            UpC.Navigate("http://seo.1473.cn/Client/云端.msi");
            UpC.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(UpC_DocumentCompleted);
        }

        /// <summary>
        /// 下载安装包完毕
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void UpC_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            this.Hide(); UpC.DocumentCompleted -= UpC_DocumentCompleted;
        }

        /// <summary>
        /// 点击稍后更新
        /// </summary>
        public void UpCNext()
        {
            this.Hide(); IsGX();
        }


        #region 窗体拖动

        //窗体拖动
        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();//API函数
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwdn, int wMsg, int mParam, int lParam);//接受发送来的消息 并处理
        public const int WM_SYSCOMMAND = 0x0112;//该变量表示将向Windows发送消息的类型
        public const int SC_MOVE = 0xF010;//该变量表示发送消息的附加消息
        public const int HTCAPTION = 0x0002;

        /// <summary>
        /// 拖动
        /// </summary>
        public void _Main_Drag()
        {
            ReleaseCapture();//该函数从当前线程中释放鼠标捕获事件，并恢复鼠标输入处理
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }

        #endregion

    }
}
