using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Security.Permissions;

namespace US.Browser.IE
{
    /// <summary>
    /// 窗体
    /// </summary>
    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    [ComVisibleAttribute(true)]
    public partial class USMain : skinform
    {
        private bool _TF = false;
        public static List<Form> _UFL = new List<Form>();//所有的//头部
        public BrowserDAO _USY;
        private WebBrowser USBrowserNav;
        public static string id = Guid.Empty.ToString();
        private System.Windows.Forms.Timer _UT = null;

        #region 初始化浏览器

        /// <summary>
        /// 无参构造
        /// </summary>
        public USMain()
        {
            InitializeComponent(); AddBS(); GetTZ(); ChangeUserAgent(" usestudio/" + StaticClass.ver + ";");//初始化导航页面
            _USY = new BrowserDAO(USBrowserNav, this); _UFL.Add(this); //确定使用的域和网站
        }

        #region webborser导航头部

        /// <summary>
        /// 初始化窗体处理
        /// </summary>
        private void AddBS()
        {
            int W = Screen.PrimaryScreen.WorkingArea.Width, H = Screen.PrimaryScreen.WorkingArea.Height; this.Width = W / 4 * 3; this.Height = H / 4 * 3;
            Point _UP = new Point((W - this.Width) / 2, (H - this.Height) / 2); this.PointToScreen(_UP); this.Location = _UP; this.Visible = false;
            USBrowserNav.ScriptErrorsSuppressed = true;
            USBrowserNav.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(USBrowserNav_DocumentCompleted);//加载完毕
            USBrowserNav.PreviewKeyDown += new PreviewKeyDownEventHandler(USBrowserNav_PreviewKeyDown);//键盘事件
            FVCBrower.ChangeBrower(USBrowserNav, new Point(3, 3), new Size(this.Size.Width - 6, 61), true);//初始化收藏夹
            USBrowserNav.Url = new System.Uri(Application.StartupPath + "\\html\\head.html");//导航头部使用的链接 
        }

        /// <summary>
        /// 导航窗口加载完毕
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void USBrowserNav_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { }

        /// <summary>
        /// 导航键盘事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void USBrowserNav_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            int _UKY = e.KeyValue;
            if (_TF == true)
            {
                switch (_UKY)
                {
                    case 116: e.IsInputKey = true; USBrowserNav.Document.InvokeScript("Refresh"); break;//刷新当前页面
                    case 122: _USY.TYUWBF11(((WebBrowser)sender)); break;//窗口变化
                    default: break;
                }
            }
            _TF = _TF == false ? true : false;
        }

        #endregion

        #endregion

        #region 功能区域

        /// <summary>
        /// 获取通知公告信息
        /// </summary>
        public void GetTZ()
        {
            System.Windows.Forms.Timer _tt = new System.Windows.Forms.Timer();
            _tt.Tick += new EventHandler(delegate(object sender, EventArgs g)
            {
                if (BLL.USIE.isnet())
                {
                    BLL.USClientCommunicate.PostPage("http://main.1473.cn/Mian.ashx?functionname=pb.Publicpage", "POST", delegate(string ue) { if (ue.Length > 6) { this.Invoke(new MethodInvoker(delegate() { StaticClass._UNE.TFP(ue, null, this._USY._UVB); })); } }, "pb.Publicpage=", "099a06fe-073b-4426-aa20-7703aa94322e", "0", "10", "1", id);
                }
            });
            _tt.Interval = 60000; _tt.Start();
        }

        /// <summary>
        /// 修改UserAgent
        /// </summary>
        public static void ChangeUserAgent(string UAE)
        {
            string _USE = GetDefaultUserAgent() + UAE;
            BLL.WindowHWND.UrlMkSetSessionOption(BLL.WindowHWND.URLMON_OPTION_USERAGENT, _USE, _USE.Length, 0);
        }

        /// <summary>
        /// 获取IE默认UserAgent的方法
        /// </summary>
        private static string GetDefaultUserAgent()
        {
            using (WebBrowser UWB = new WebBrowser())
            {
                Type _UTP, _UTE; object _UW;
                UWB.Navigate("about:blank"); while (UWB.IsBusy) { Application.DoEvents(); } _UW = UWB.Document.Window.DomWindow;
                _UTP = _UW.GetType(); object _UNE = _UTP.InvokeMember("navigator", BindingFlags.GetProperty, null, _UW, new object[] { });
                _UTE = _UNE.GetType(); object _UAE = _UTE.InvokeMember("userAgent", BindingFlags.GetProperty, null, _UNE, new object[] { });
                return _UAE.ToString();
            }
        }

        #endregion

        #region 事件区域

        /// <summary>
        /// 窗体关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void USMain_FormClosed(object sender, FormClosedEventArgs e) { StaticClass.CloseSoft(this); }

        /// <summary>
        /// 关闭窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void USMain_Disposed(object sender, EventArgs e)
        {
            StaticClass._USE.exit_Click(sender, e); base.Dispose();
        }

        /// <summary>
        /// 窗口大小变化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void USMain_Resize(object sender, EventArgs e)
        {
            if (this.WindowState != FormWindowState.Minimized)//不在最小化窗体使用
            {
                try
                {
                    SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true); this.Refresh();//页面重绘设置
                    this._USY._UVB.DXBH(this._USY._UF.WindowState == FormWindowState.Maximized);//页面大小设置
                }
                catch { }
            }
        }

        void USMain_ResizeEnd(object sender, EventArgs e) { }

        void USMain_Shown(object sender, EventArgs e) { }

        /// <summary>
        /// 聚焦显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void USMain_SizeChanged(object sender, EventArgs e)
        {
            if (StaticClass._UST.IndexOf("-background") == -1 && _UT == null)
            {
                Form _UF = (Form)sender;
                if (_UF.WindowState != FormWindowState.Minimized)
                {
                    _UT = new System.Windows.Forms.Timer();
                    _UT.Tick += new EventHandler(_UT_Tick);
                    _UT.Interval = 1; _UT.Start();
                }
            }
        }

        /// <summary>
        /// 计时器
        /// </summary>
        /// <param name="sender"></param>me
        /// <param name="e"></param>
        void _UT_Tick(object sender, EventArgs e)
        {
            if (_UT != null)
            {
                _UT.Stop(); _UT.Dispose(); _UT = null;
                StaticClass._USE.Show_Click();
            }
        }

        /// <summary>
        /// 聚焦显示窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void USMain_GotFocus(object sender, EventArgs e)
        {
            if (!this.Visible && BLL.USIE.isnet())
            {
                if (StaticClass._UST.IndexOf("-background") == -1) { USMain_SizeChanged(sender, e); }
            }
        }

        #endregion

        #region 没用区域

        //if (((WebBrowser)sender).DocumentText == "") USBrowserNav.Navigate(System.IO.Path.GetFullPath("html/head.html"));
        //WebBrowser _UWB = BrowserDAO._UBL[0];
        //if (UURL == "Set") { new Respond_setting(_UWB); }
        //else { BrowserDAO.ModifyOneBrowser(BrowserDAO._UBL[0].Name, UURL); }
        //UURL = "";

        //if (_UKY == 116)//刷新当前页面
        //{
        //}
        //else if (_UKY == 122)//窗口变化
        //{
        //    _USY.TYUWBF11(((WebBrowser)sender));
        //    //Point? _UP = null;
        //    //if (this.WindowState != FormWindowState.Maximized)
        //    //{

        //    //    //if (this.WindowState == FormWindowState.Maximized) this.WindowState = FormWindowState.Normal;
        //    //    //_USY._UWB.Visible = false;//隐藏((WebBrowser)sender).Height
        //    //    _UP = new Point(2, 2);
        //    //    _USY._UVB.BSSZ(_UP, true);

        //    //}
        //    //else
        //    //{
        //    //    _UP = new Point(2, ((WebBrowser)sender).Height - 2);
        //    //    _USY._UVB.BSSZ(_UP, false);
        //    //}
        //}

        //try
        //{
        //USBrowserNav.Navigate(Application.StartupPath + "\\html\\head.html");
        //MessageBox.Show("yes");
        //}
        //catch (Exception e) { MessageBox.Show(e.Message); }


        //[System.Runtime.InteropServices.DllImport("user32.dll")]
        //public static extern int PostMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        //public const int WM_CLOSE = 0x10;
        //public const int SC_MOVE = 0xF010;//该变量表示发送消息的附加消息
        //public const int HTCAPTION = 0x0002;
        //public void LS()
        //{
        //    //Message M = Message.Create(this.Handle, 0, new IntPtr(0), new IntPtr(0));
        //    //M.Result = new IntPtr(HT_BOTTOMRIGHT);
        //    Point screenPoint = Control.MousePosition;
        //    PostMessage(this.Handle, WM_NCHITTEST, 0, 21299350);
        //    //Message.Create(new IntPtr(HT_BOTTOMRIGHT),WM_NCHITTEST,
        //    //Message.Result = ;
        //}
        //private void MainForm_Resize(object sender, EventArgs e)
        //{

        //}
        //[DllImport("user32.dll")]
        //public static extern bool ReleaseCapture();//系统接口
        //public const int HWND_BROADCAST = 0xFFFF;
        //public const int WM_FONTCHANGE = 0x1D;
        //[DllImport("user32.dll")]
        //public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        //public static int WM_SYSCOMMAND = 0x0112;
        //public static int SC_MOVE = 0xF010;
        //public static int HTCAPTION = 0x0002;
        //protected override CreateParams CreateParams
        //{
        //    get
        //    {
        //        CreateParams cp = base.CreateParams;
        //        cp.Style |= 0xC0000 + 0x20000;
        //        return cp;
        //    }
        //}
        //private void Server_MouseMove(object sender, MouseEventArgs e)
        //{
        //    ReleaseCapture();
        //    SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);

        //}


        //private void USBrowserNav_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        //{
        //    if (this.keypressed)
        //    {
        //        this.keypressed = false;
        //        return;
        //    }
        //    switch (e.KeyCode)
        //    {
        //        case Keys.F5:
        //            e.IsInputKey = true;
        //            this.USBrowserNav.Document.InvokeScript("Refresh");
        //            break;
        //        case Keys.F11:
        //            if (this.WindowState == FormWindowState.Normal)
        //            {
        //                this.WindowState = FormWindowState.Maximized;
        //            }
        //            else if (this.WindowState == FormWindowState.Maximized)
        //            {
        //                this.WindowState = FormWindowState.Normal;
        //            }
        //            WebBrowser wb = (WebBrowser)_broDao.BrowserList[0];
        //            if (this.USBrowserNav.Dock == DockStyle.Top)
        //            {
        //                this.USBrowserNav.Visible = false;

        //                a.Dock = DockStyle.Fill;
        //                a.Document.Focus();
        //            }
        //            else
        //            {
        //                this.USBrowserNav.Visible = true;
        //                WebBrowser a = (WebBrowser)_broDao.BrowserList[0];
        //                a.Dock = DockStyle.None;
        //                a.Width = this.ClientRectangle.Width - 1;
        //                a.Height = (this.ClientRectangle.Height - 1) - this.USBrowserNav.Height;
        //            }
        //            break;
        //    }
        //    this.keypressed = true;

        //}

        //private void USMain_MouseDown(object sender, MouseEventArgs e)
        //{
        //    isMouseDown = true;//表示鼠标按下了
        //}

        //private void USMain_MouseUp(object sender, MouseEventArgs e)
        //{
        //    isMouseDown = false;//松开鼠标的时候 鼠标状态变回false
        //    direction = MouseDirection.None;//鼠标松开的时候不做任何的拖动
        //}
        //public void ResizeWindow()//窗体的拉伸
        //{
        //    if (!isMouseDown) return;//表示只有鼠标按下的时候才能拉伸窗体
        //    if (direction == MouseDirection.Declining)
        //    {
        //        this.Cursor = Cursors.SizeNWSE;
        //        this.Width = MousePosition.X - this.Left;
        //        this.Height = MousePosition.Y - this.Top;
        //    }
        //    if (direction == MouseDirection.Herizontal)
        //    {
        //        this.Cursor = Cursors.SizeWE;
        //        this.Width = MousePosition.X - this.Left;
        //    }
        //    else if (direction == MouseDirection.vertical)
        //    {
        //        this.Cursor = Cursors.SizeNS;
        //        this.Height = MousePosition.Y - this.Top;

        //    }
        //    else
        //    {
        //        this.Cursor = Cursors.Arrow;
        //    }
        //}
        //private void USMain_MouseMove(object sender, MouseEventArgs e)//鼠标的样式,表示鼠标移动到form窗体边缘的时候 改变鼠标的样式
        //{
        //    ResizeWindow();
        //    if (e.Location.X >= this.Width - 5 && e.Location.Y > this.Height - 5)//右边以及下面的form边缘
        //    {
        //        this.Cursor = Cursors.SizeNWSE;
        //        direction = MouseDirection.Declining;
        //    }
        //    else if (e.Location.X >= this.Width - 5)//窗体右边的时候进行拉伸
        //    {
        //        this.Cursor = Cursors.SizeWE;
        //        direction = MouseDirection.Herizontal;
        //    }
        //    else if (e.Location.Y >= this.Height - 5)//窗体下面的时候
        //    {
        //        this.Cursor = Cursors.SizeNS;
        //        direction = MouseDirection.vertical;
        //    }
        //    else
        //    {
        //        this.Cursor = Cursors.Arrow;
        //    }
        //}
        #endregion

    }
}
