using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Permissions;

namespace US.Browser.IE
{

    /// <summary>
    /// 无边框窗体使用区域
    /// </summary>
    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public partial class skinform : Form
    {
        skinm _usk;
        const int WM_ERASEBKGND = 0x14;
        const int WS_MINIMIZEBOX = 0x00020000;
        const int CS_DROPSHADOW = 0x20000;

        /// <summary>
        /// 初始化
        /// </summary>
        public skinform()
        {
            InitializeComponent(); SetStyles();
        }

        /// <summary>
        /// 画上阴影边框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void skinform_Paint(object sender, PaintEventArgs e)
        {
            Rectangle myRectangle = new Rectangle(0, 0, this.Width, this.Height);
            ControlPaint.DrawBorder(e.Graphics, myRectangle, Color.FromArgb(80, 80, 80), 5, ButtonBorderStyle.Solid, Color.FromArgb(80, 80, 80), 5, ButtonBorderStyle.Solid, Color.FromArgb(80, 80, 80), 5, ButtonBorderStyle.Solid, Color.FromArgb(80, 80, 80), 5, ButtonBorderStyle.Solid);
        }

        #region

        /// <summary>
        /// 窗体大小变化
        /// </summary>
        /// <param name="e"></param>
        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            BLL.WindowHWND.SetFormRoundRectRgn(this, this.WindowState == FormWindowState.Maximized ? 0 : 15);
        }

        #endregion

        #region 窗体美化

        #region 设置样式

        /// <summary>
        /// 添加控件信息
        /// </summary>
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.Style |= WM_ERASEBKGND | WS_MINIMIZEBOX;
                cp.ClassStyle |= CS_DROPSHADOW;
                return cp;
            }
        }

        /// <summary>
        /// 设置样式
        /// </summary>
        private void SetStyles()
        {
            SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.DoubleBuffer, true); //自行绘制 禁止擦除背景 缓存
            UpdateStyles(); base.AutoScaleMode = AutoScaleMode.None;
        }

        #endregion

        /// <summary>
        /// 显示影藏变化
        /// </summary>
        /// <param name="e"></param>
        protected override void OnVisibleChanged(EventArgs e)
        {
            if (Visible)
            {
                if (!DesignMode)
                {
                    BLL.WindowHWND.AnimateWindow(this.Handle, 100, BLL.WindowHWND.AW_BLEND | BLL.WindowHWND.AW_ACTIVATE); //淡入淡出
                    if (_usk == null) //添加绘图窗体层
                    {
                        // _usk = new skinm(this, new Point(this.Location.X - 5, this.Location.Y - 5));
                        //  _usk.Show(this);
                    }
                }
            }
            else { BLL.WindowHWND.AnimateWindow(this.Handle, 100, BLL.WindowHWND.AW_BLEND | BLL.WindowHWND.AW_HIDE); }//淡入淡出  
            base.OnVisibleChanged(e); SetStyles(); this.Refresh();
        }

        /// <summary>
        /// 窗体关闭
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e); if (_usk != null) { _usk.Close(); }
            BLL.WindowHWND.AnimateWindow(this.Handle, 150, BLL.WindowHWND.AW_BLEND | BLL.WindowHWND.AW_HIDE);
        }


        #region 拉伸

        const int WM_NCHITTEST = 0x0084;
        const int HT_LEFT = 10;
        const int HT_RIGHT = 11;
        const int HT_TOP = 12;
        const int HT_TOPLEFT = 13;
        const int HT_TOPRIGHT = 14;
        const int HT_BOTTOM = 15;
        const int HT_BOTTOMLEFT = 16;
        const int HT_BOTTOMRIGHT = 17;
        const int HT_CAPTION = 2;
        const Int32 WM_PARENTNOTIFY = 0x210;
        const Int32 WM_DESTROY = 0x2;

        #endregion

        #region 钩子


        /// <summary>
        /// 窗口轮子
        /// </summary>
        /// <param name="Msg"></param>
        protected override void WndProc(ref Message Msg)
        {
            switch (Msg.Msg)
            {
                case WM_ERASEBKGND: Msg.Result = IntPtr.Zero; break;
            }
            if (Msg.Msg == WM_NCHITTEST)
            {
                //获取鼠标位置
                int nPosX = (Msg.LParam.ToInt32() & 65535);
                int nPosY = (Msg.LParam.ToInt32() >> 16);
                //右下角
                if (nPosX >= this.Right - 6 && nPosY >= this.Bottom - 6)
                {
                    Msg.Result = new IntPtr(HT_BOTTOMRIGHT);
                    return;
                }
                //左上角
                else if (nPosX <= this.Left + 6 && nPosY <= this.Top + 6)
                {
                    Msg.Result = new IntPtr(HT_TOPLEFT);
                    return;
                }
                //左下角
                else if (nPosX <= this.Left + 6 && nPosY >= this.Bottom - 6)
                {
                    Msg.Result = new IntPtr(HT_BOTTOMLEFT);
                    return;
                }
                //右上角
                else if (nPosX >= this.Right - 6 && nPosY <= this.Top + 6)
                {
                    Msg.Result = new IntPtr(HT_TOPRIGHT);
                    return;
                }
                else if (nPosX >= this.Right - 2)
                {
                    Msg.Result = new IntPtr(HT_RIGHT);
                    return;
                }
                else if (nPosY >= this.Bottom - 2)
                {
                    Msg.Result = new IntPtr(HT_BOTTOM);
                    return;
                }
                else if (nPosX <= this.Left + 2)
                {
                    Msg.Result = new IntPtr(HT_LEFT);
                    return;
                }
                else if (nPosY <= this.Top + 2)
                {
                    Msg.Result = new IntPtr(HT_TOP);
                    return;
                }
                else
                {
                    Msg.Result = new IntPtr(HT_CAPTION);
                    return;
                }
            }
            base.WndProc(ref Msg);
        }

        #endregion

        #endregion
    }
}
