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 ryControls
{
    /// <summary>
    /// 窗体皮肤
    /// </summary>
    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public partial class Formbase : Form
    {
        string PicFolder = "ryControls.Controls.FormImages.";
        /// <summary>
        /// 窗体皮肤
        /// </summary>
        public Formbase()
        {
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
            SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
            title = base.Text;
            InitializeComponent();
            SetWinTopIcons();
            try
            {
                if (this.Owner != null)
                {
                    if (this.Owner.TopMost)
                    {
                        this.TopMost = true;
                    }
                    if (UseParentIcon)
                    {
                        this.Icon = this.Owner.Icon;
                    }
                }
            }
            catch { }
        }
        string title = "";
        /// <summary>
        /// 标题属性
        /// </summary>
        [Browsable(true)]
        new public string Text
        {
            get
            {
                return title;
            }
            set
            {
                base.Text = value;
                title = value;
                s_lblText.Text = value;
            }
        }
        bool _UseParentIcon = false;
        /// <summary>
        /// 是否使用父窗口的图标
        /// </summary>
        [Description("是否使用父窗口的图标")]
        public bool UseParentIcon
        {
            get
            {
                return _UseParentIcon;
            }
            set
            {
                _UseParentIcon = value;
            }
        }
        /// <summary>
        /// 点击最小化按钮是最小化还是隐藏
        /// </summary>
        [Description("点击最小化按钮是最小化还是隐藏")]
        public bool MinButtonHideAction
        {
            get; set;
        }
        /// <summary>
        /// 最大化按钮
        /// </summary>
        new public bool MaximizeBox
        {
            get
            {
                return base.MaximizeBox;
            }
            set
            {
                base.MaximizeBox = value;
                SetWinTopIcons();
            }
        }
        /// <summary>
        /// 最小化按钮
        /// </summary>
        new public bool MinimizeBox
        {
            get
            {
                return base.MinimizeBox;
            }
            set
            {
                base.MinimizeBox = value;
                SetWinTopIcons();
            }
        }
        bool closeBox = true;
        /// <summary>
        /// 标题栏关闭按钮是否显示
        /// </summary>
        [Description("标题栏关闭按钮是否显示")]
        public bool CloseBox
        {
            get
            {
                return closeBox;
            }
            set
            {
                closeBox = value;
                SetWinTopIcons();
            }
        }
        bool showBackTip = true;
        /// <summary>
        /// 当窗口位于后台时，是否标注显示。
        /// </summary>
        [Description("当窗口位于后台时，是否标注显示。")]
        public bool ShowBackTip
        {
            get
            {
                return showBackTip;
            }
            set
            {
                showBackTip = value;
                if (!value)
                {
                    s_lblText.Text = title;
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        new public FormWindowState WindowState
        {
            get
            {
                return base.WindowState;
            }
            set
            {
                if (value == FormWindowState.Maximized)
                {
                    base.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
                }
                base.WindowState = value;
            }
        }
        private Color m_FormTitleColor = Color.White;
        /// <summary>
        /// 标题栏字体颜色
        /// </summary>
        [Description("标题栏字体颜色。")]
        public Color FormTitleColor
        {
            get
            {
                return m_FormTitleColor;
            }
            set
            {
                m_FormTitleColor = value;
                s_lblText.BackColor = value;
            }
        }
        private Color m_FormTitleLeaveColor = Color.Gray;
        /// <summary>
        /// 标题栏不处于活动状态时的字体颜色
        /// </summary>
        [Description("标题栏不处于活动状态时的字体颜色。")]
        public Color FormTitleLeaveColor
        {
            get
            {
                return m_FormTitleLeaveColor;
            }
            set
            {
                m_FormTitleLeaveColor = value;
            }
        }
        private Color m_FormTitleBackColor = Color.FromArgb(81, 169, 251);
        /// <summary>
        /// 标题栏背景颜色
        /// </summary>
        [Description("标题栏背景颜色。")]
        public Color FormTitleBackColor
        {
            get
            {
                return m_FormTitleBackColor;
            }
            set
            {
                m_FormTitleBackColor = value;
                s_pnlWinTop.BackColor = value;
            }
        }
        private Color m_FormTitleLeaveBackColor = Color.FromArgb(235, 238, 242);
        /// <summary>
        /// 标题栏不处于活动状态时的背景颜色
        /// </summary>
        [Description("标题栏不处于活动状态时的背景颜色。")]
        public Color FormTitleLeaveBackColor
        {
            get
            {
                return m_FormTitleLeaveBackColor;
            }
            set
            {
                m_FormTitleLeaveBackColor = value;
            }
        }
        /// <summary>
        /// 是否允许拖拉窗体边缘来改变窗体大小
        /// </summary>
        [Description("是否允许拖拉窗体边缘来改变窗体大小。")]
        public bool CanChangeSize
        {
            get
            {
                return canChangeSize;
            }
            set
            {
                canChangeSize = value;
            }
        }
        private bool isMouseDown = false;
        private Point FormLocation;     //form的location
        private Point mouseOffset;      //鼠标的按下位置
        private bool canChangeSize = false;
        private void PnlWinTop_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                int _x = 0;
                int _y = 0;
                if (isMouseDown)
                {
                    Point pt = Control.MousePosition;
                    _x = mouseOffset.X - pt.X;
                    _y = mouseOffset.Y - pt.Y;

                    this.Location = new Point(FormLocation.X - _x, FormLocation.Y - _y);
                }
            }
            catch (Exception) { }
        }

        private void PnlWinTop_MouseUp(object sender, MouseEventArgs e)
        {
            isMouseDown = false;
        }

        private void PnlWinTop_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == MouseButtons.Left)
                {
                    isMouseDown = true;
                    FormLocation = this.Location;
                    mouseOffset = Control.MousePosition;
                }
            }
            catch (Exception) { }
        }


        private void SetWinTopIcons()
        {
            s_pnMax.Visible = MaximizeBox;
            s_pnMini.Visible = MinimizeBox;
            s_pnClose.Visible = CloseBox;
            //if (MaximizeBox)
            //{
            //    pnMax.Left = pnClose.Left - pnMax.Width;
            //    pnMax.Visible = true;
            //    pnMini.Left = pnMax.Left - pnMini.Width;
            //    if (MinimizeBox)
            //    {
            //        pnMini.Visible = true;
            //    }
            //    else
            //    {
            //        pnMini.Visible = false;
            //    }
            //}
            //else
            //{
            //    pnMax.Visible = false;
            //    pnMini.Left = pnClose.Left - pnMini.Width;
            //    if (MinimizeBox)
            //    {
            //        pnMini.Visible = true;
            //    }
            //    else
            //    {
            //        pnMini.Visible = false;
            //    }
            //}
        }
        private void Btnmin_MouseEnter(object sender, EventArgs e)
        {
            s_btnmin.BackgroundImage = ButtonImages.ImageObject.GetResBitmap(PicFolder + "btn_mini_Hover.png");
        }

        private void Btnmin_MouseLeave(object sender, EventArgs e)
        {
            s_btnmin.BackgroundImage = ButtonImages.ImageObject.GetResBitmap(PicFolder + "btn_mini.png");
        }

        private void BtnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #region 控制改变窗体大小
        const int WM_NCHITTEST = 0x0084;
        const int HTLEFT = 10;    //左边界
        const int HTRIGHT = 11;   //右边界
        const int HTTOP = 12; //上边界
        const int HTTOPLEFT = 13; //左上角
        const int HTTOPRIGHT = 14;    //右上角
        const int HTBOTTOM = 15;  //下边界
        const int HTBOTTOMLEFT = 0x10;    //左下角
        const int HTBOTTOMRIGHT = 17; //右下角
        /// <summary>
        /// 
        /// </summary>
        /// <param name="m"></param>
        protected override void WndProc(ref Message m)
        {
            try
            {
                base.WndProc(ref m);
            }
            catch { }
            switch (m.Msg)
            {
                case WM_NCHITTEST:
                    {
                        Point vPoint = new Point((int)m.LParam & 0xFFFF,
                            (int)m.LParam >> 16 & 0xFFFF);
                        vPoint = PointToClient(vPoint);
                        //判断：仅当当前窗体状态不是最大化时，相关鼠标事件生效
                        if (this.WindowState != FormWindowState.Maximized && (CanChangeSize))
                        {
                            #region 调整窗体
                            if (vPoint.X <= 5)
                            {
                                if (vPoint.Y <= 5)
                                {
                                    m.Result = (IntPtr)HTTOPLEFT;
                                }
                                else if (vPoint.Y >= ClientSize.Height - 5)
                                {
                                    m.Result = (IntPtr)HTBOTTOMLEFT;
                                }
                                else
                                {
                                    m.Result = (IntPtr)HTLEFT;
                                }
                            }
                            else if (vPoint.X >= ClientSize.Width - 5)
                            {
                                if (vPoint.Y <= 5)
                                {
                                    m.Result = (IntPtr)HTTOPRIGHT;
                                }
                                else if (vPoint.Y >= ClientSize.Height - 5)
                                {
                                    m.Result = (IntPtr)HTBOTTOMRIGHT;
                                }
                                else
                                {
                                    m.Result = (IntPtr)HTRIGHT;
                                }
                            }
                            else if (vPoint.Y <= 5)
                            {
                                m.Result = (IntPtr)HTTOP;
                            }
                            else if (vPoint.Y >= ClientSize.Height - 5)
                            {
                                m.Result = (IntPtr)HTBOTTOM;
                            }
                            #endregion
                        }
                        break;
                    }
            }
        }
        #endregion
        private void BtnMax_Click(object sender, EventArgs e)
        {
            if (!MaximizeBox) { return; }
            if (WindowState != FormWindowState.Maximized)
            {
                this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
                this.WindowState = FormWindowState.Maximized;
            }
            else
            {
                this.WindowState = FormWindowState.Normal;
            }
        }
        Point init_Position = new Point();
        private void Btnmin_Click(object sender, EventArgs e)
        {
            if (!MinimizeBox) { return; }
            init_Size = new Size(Width, Height);
            if (MinButtonHideAction) { this.Hide(); }
            else
            {
                this.WindowState = FormWindowState.Minimized;
            }

        }
        private Size init_Size = new Size();
        private void Formbase_Load(object sender, EventArgs e)
        {
            s_lblText.ForeColor = m_FormTitleColor;
            s_btnMax.BackgroundImage = ButtonImages.ImageObject.GetResBitmap(PicFolder + "btn_max.png");
            s_btnmin.BackgroundImage = ButtonImages.ImageObject.GetResBitmap(PicFolder + "btn_mini.png");
            s_btnClose.BackgroundImage = ButtonImages.ImageObject.GetResBitmap(PicFolder + "btn_close.png");
            //init_Size = new Size(Width, Height);
            //init_Position = new Point(Left,Top);
            if (this.Site != null && this.Site.DesignMode)
            {

            }
            else
            {
                MinimumSize = new Size(Width, Height);
            }
            if (this.Owner != null)
            {
                if (this.Owner.TopMost)
                {
                    this.TopMost = true;
                }
                if (UseParentIcon)
                {
                    this.Icon = this.Owner.Icon;
                }
            }
        }
        /// <summary>
        /// 画边框
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            base.OnPaintBackground(e);
            try
            {
                ControlPaint.DrawBorder(e.Graphics, new Rectangle(0, 0, this.Width, this.Height), SkinHelp.DefalutBorderColor, ButtonBorderStyle.Solid);
            }
            catch { }
        }

        private void PnlWinTop_DoubleClick(object sender, EventArgs e)
        {
            if (this.WindowState != FormWindowState.Maximized && MaximizeBox)
            {
                this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
                this.WindowState = FormWindowState.Maximized;
            }
            else
            {
                this.WindowState = FormWindowState.Normal;
            }
        }

        private void PnMini_Paint(object sender, PaintEventArgs e)
        {

        }

        private void BtnMax_MouseLeave(object sender, EventArgs e)
        {
            s_btnMax.BackgroundImage = ButtonImages.ImageObject.GetResBitmap(PicFolder + "btn_max.png");
        }

        private void BtnMax_MouseEnter(object sender, EventArgs e)
        {
            s_btnMax.BackgroundImage = ButtonImages.ImageObject.GetResBitmap(PicFolder + "btn_max_Hover.png");
        }

        private void BtnClose_MouseLeave(object sender, EventArgs e)
        {
            s_btnClose.BackgroundImage = ButtonImages.ImageObject.GetResBitmap(PicFolder + "btn_close.png");
        }

        private void BtnClose_MouseEnter(object sender, EventArgs e)
        {
            s_btnClose.BackgroundImage = ButtonImages.ImageObject.GetResBitmap(PicFolder + "btn_close_Hover.png");
        }

        private void Formbase_Leave(object sender, EventArgs e)
        {

        }

        private void Formbase_Deactivate(object sender, EventArgs e)
        {
            if (Width > 100 && Height > 100)
                init_Size = new Size(Width, Height);
            if (Left > 0 && Top > 0)
                init_Position = new Point(Left, Top);
            s_pnlWinTop.BackColor = m_FormTitleLeaveBackColor;
            s_lblText.ForeColor = m_FormTitleLeaveColor;
            if (ShowBackTip)
            {
                s_lblText.Text = title + " <处于后台>";
            }
        }

        private void Formbase_Activated(object sender, EventArgs e)
        {
            s_pnlWinTop.BackColor = m_FormTitleBackColor;
            s_lblText.ForeColor = m_FormTitleColor;
            s_lblText.Text = title;
            Form a = this;
            if (Width < 10) { Width = init_Size.Width; }
            if (Height < 10) { Height = init_Size.Height; }
            if (Left < 0) { Left = init_Position.X; }
            if (Top < 0) { Top = init_Position.Y; }
        }

        private void Formbase_Shown(object sender, EventArgs e)
        {
            try
            {
                if (this.Owner != null)
                {
                    if (this.Owner.TopMost)
                    {
                        this.TopMost = true;
                    }
                }
            }
            catch { }
        }

        private void Formbase_ResizeEnd(object sender, EventArgs e)
        {

        }

        private void Formbase_Resize(object sender, EventArgs e)
        {
            try
            {
                if (this.WindowState == FormWindowState.Minimized)
                {
                    for (int i = 0; i < this.OwnedForms.Length; i++)
                    {
                        Form form = this.OwnedForms[i];
                        form.WindowState = FormWindowState.Normal;
                        form.Visible = true;
                    }
                }
            }
            catch { }
        }

        private void Formbase_VisibleChanged(object sender, EventArgs e)
        {

        }

        private void Formbase_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            { this.Close(); }
        }
    }
}
