using MGdu.WinFormUI.MyGraphics;
using ryCommon;
using ryCommonDb;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using TimeClock.API;

namespace TimeClock.DbOp
{
    public partial class FrmSticky : Form
    {
        ControlBoxManager controlBoxManager;
        public FrmSticky()
        {
            InitializeComponent();
            Itrycn_Info.StickyList.Add(this);
            ryCommon.SuperForm superform = new ryCommon.SuperForm(this);
            superform.Resizable = true;
            superform.SetFormMove(this);
            superform.SetFormMove(PnlTop);
            superform.SetFormMove(PnlBottom);
            timer1.Start();
            SetFormRegion();
            this.SetStyle(
                ControlStyles.AllPaintingInWmPaint |
                ControlStyles.UserPaint |
                ControlStyles.OptimizedDoubleBuffer |
                ControlStyles.ResizeRedraw, true);
            this.DoubleBuffered = true;
            this.UpdateStyles();
            controlBoxManager = new ControlBoxManager(this);
            //RyForm.SetDesktopForm(Handle);
        }
        //定义图像反色函数
        private Bitmap PContray(Image a, Color _rev_color)
        {
            int Height = a.Height;
            int Width = a.Width;
            Bitmap bitmap = new Bitmap(Width, Height);
            Bitmap MyBitmap = (Bitmap)a;
            Color pixel;
            Color img_color = _rev_color;
            for (int x = 1; x < Width; x++)
            {
                for (int y = 1; y < Height; y++)
                {
                    pixel = MyBitmap.GetPixel(x, y);
                    if (pixel.A == 0 && pixel.R == 0 && pixel.G == 0 && pixel.B == 0) { continue; }
                    bitmap.SetPixel(x, y, img_color);
                }
            }
            return bitmap;
        }
        /// <summary>
        /// 窗体上移动鼠标时激发
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (ControlBox)
                controlBoxManager.MouseOperation(e.Location, MGdu.WinFormUI.MouseOperationType.Move);
        }
        /// <summary>
        /// 在窗体上按下鼠标时激发
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);

            if (e.Button != System.Windows.Forms.MouseButtons.Left)
            {
                return;
            }

            if (ControlBox)
                controlBoxManager.MouseOperation(e.Location, MGdu.WinFormUI.MouseOperationType.Down);
        }
        /// <summary>
        /// 在窗体上放开鼠标时激发
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            if (e.Clicks > 1)
                return;
            if (ControlBox)
                controlBoxManager.MouseOperation(e.Location, MGdu.WinFormUI.MouseOperationType.Up);
        }
        /// <summary>
        /// 在鼠标离开窗体时激发
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            if (ControlBox)
                controlBoxManager.MouseOperation(Point.Empty, MGdu.WinFormUI.MouseOperationType.Leave);
        }
        private bool WmNcCalcSize(ref Message m)
        {
            if (m.WParam == new IntPtr(1))
            {
                Win32.NCCALCSIZE_PARAMS info = (Win32.NCCALCSIZE_PARAMS)
                    Marshal.PtrToStructure(m.LParam, typeof(Win32.NCCALCSIZE_PARAMS));
                //if (IsAboutToMaximize(info.rectNewForm))
                //{
                //    Rectangle workingRect = Screen.GetWorkingArea(this);
                //    info.rectNewForm.Left = workingRect.Left - 1;
                //    info.rectNewForm.Top = workingRect.Top - 1;
                //    info.rectNewForm.Right = workingRect.Right + 1;
                //    info.rectNewForm.Bottom = workingRect.Bottom + 1;
                //    Marshal.StructureToPtr(info, m.LParam, false);
                //}
            }
            return true;
        }
        /// <summary>
        /// 消息
        /// </summary>
        /// <param name="m"></param>
        protected override void WndProc(ref Message m)
        {
            //if (this.DesignMode) { base.WndProc(ref m); return; }
            bool alreadyHandled = false;
            switch (m.Msg)
            {
                case (int)Win32.WindowMessages.WM_NCCALCSIZE:
                    alreadyHandled = WmNcCalcSize(ref m);
                    break;
                case (int)Win32.WindowMessages.WM_NCPAINT:
                    alreadyHandled = true;
                    break;

                default:
                    break;
            }

            if (!alreadyHandled)
                base.WndProc(ref m);
        }
        private Color _ButtonColor = Color.Gray;
        /// <summary>
        /// 按钮字体颜色
        /// </summary>
        [Category("GMForm")]
        [Description("按钮字体颜色")]
        public Color ButtonColor
        {
            get { return _ButtonColor; }
            set
            {
                _ButtonColor = value;
                Invalidate();
            }
        }
        private Size _CloseBoxSize=new Size(24, 24);
        /// <summary>
        /// 窗体关闭按钮的尺寸
        /// </summary>
        [Category("GMForm")]
        [Description("窗体关闭按钮的尺寸")]
        [DefaultValue(typeof(Size), "24, 24")]
        public Size CloseBoxSize
        {
            get { return _CloseBoxSize; }
            set
            {
                _CloseBoxSize = value;
                Invalidate();
            }
        }
        internal Rectangle CloseBoxRect
        {
            get
            {
                if (ControlBox)
                {
                    int x = ClientSize.Width -2 - CloseBoxSize.Width;
                    return new Rectangle(
                        new Point(x, 2),
                        CloseBoxSize);
                }
                else
                    return Rectangle.Empty;
            }
        }
        internal Rectangle AddBoxRect
        {
            get
            {
                if (ControlBox)
                {
                    int x = 4;
                    return new Rectangle(
                        new Point(x, 2),
                        CloseBoxSize);
                }
                else
                    return Rectangle.Empty;
            }
        }
        internal Rectangle TextRect
        {
            get
            {
                int x =controlBoxManager.AddBtnVisible? AddBoxRect.Right+5:8;
                int y = 1;
                int height = AddBoxRect.Height;
                int right = this.ClientSize.Width - x;
                if (ControlBox)
                {
                    right = CloseBoxRect.Left;
                }
                int width = right - x;
                return new Rectangle(x, y, width, height);
            }
        }
        /// <summary>
        /// 设置或获取当前便捷的ID号
        /// </summary>
        public int Id { get; set; } = 0;
        /// <summary>
        /// 是否是新增便笺
        /// </summary>
        public bool IsAdd { get; set; } = false;
        /// <summary>
        /// 设置或获取便笺背景色
        /// </summary>
        public Color StickyBackColor
        {
            get { return txtContent.BackColor; }
            set
            {
                BackColor = value;
                PnlTop.BackColor = value;
                PnlBottom.BackColor = value;
                txtContent.BackColor = value;
                TxtTitle.BackColor = value;
                var rev_color= GetRevColor();
                _ButtonColor = rev_color;
                TxtTitle.ForeColor = rev_color;
                txtContent.ForeColor = rev_color;
                LblBold.ForeColor = rev_color;
                LblItalic.ForeColor = rev_color;
                LblUnderline.ForeColor = rev_color;
                LblStrikeout.ForeColor = rev_color;
                LblBullet.ForeColor = rev_color;
                LblImage.ForeColor = rev_color;
                controlBoxManager.ButtonColor = ButtonColor;
                controlBoxManager.ResetBoxColor();
                Invalidate();
            }
        }
        private Color GetRevColor()
        {
            if (StickyBackColor == Color.Blue) { return Color.White; }
            if (StickyBackColor == Color.FromArgb(255,0,128,255)) { return Color.White; }
            if (StickyBackColor == Color.Gray) { return Color.White; }
            if (StickyBackColor == Color.Red) { return Color.White; }
            if (StickyBackColor == Color.FromArgb(255, 255, 0, 128)) { return Color.White; }
            if (StickyBackColor == Color.FromArgb(255, 255, 128, 0)) { return Color.White; }
            if (StickyBackColor == Color.FromArgb(255, 0, 0, 160)) { return Color.White; }
            if (StickyBackColor == Color.FromArgb(255, 128, 0, 255)) { return Color.White; }
            if (StickyBackColor == Color.Navy) { return Color.White; }
            var hsl = ryControls.ColorHelper.RgbToHsl(new ryControls.ColorRGB(StickyBackColor));
            hsl.L = 255 - hsl.L;
            // Color img_color = Color.FromArgb(255 - StickyBackColor.R, 255 - StickyBackColor.G, 255 - StickyBackColor.B);
            return ryControls.ColorHelper.HslToRgb(hsl).GetColor();
        }
        public void CloseByUser()
        {
            DeskShow = 0;
            if (txtContent.Text == "")
            {
                IDbInterface db = TimeClock.Itrycn_Db.CreateDataProvider(TimeClock.Itrycn_Db.dataType);
                if (db.ConnDb(TimeClock.Itrycn_Db.SQLConnStr) == 1)
                {
                    db.DelById("Sticky", Id.ToString());
                }
                db.Free();
            }
            else
            {
                if(!IsMini)
                SaveSticky();
            }
            this.Close();
        }
        #region 界面按钮设置
        private void LblClose_Click(object sender, EventArgs e)
        {

        }
        public void AddClick()
        {
            FrmSticky frm = new FrmSticky();
            frm.ProcUse = true;
            frm.Id = -1;
            frm.IsAdd = true;
            frm.StickyBackColor = StickyBackColor;
            frm.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - frm.Width, 20);
            frm.Show();
            frm.ProcUse = false;
        }
        public bool ProcUse = false;
        private void SetFontStyle(FontStyle style, bool IsRemove)
        {
            var font = txtContent.SelectionFont ?? this.Font;
            if (IsRemove)
            { txtContent.SelectionFont = new Font(font ?? this.Font, font.Style ^ style); }
            else { txtContent.SelectionFont = new Font(font, font.Style | style); }
        }
        private void LblBold_Click(object sender, EventArgs e)
        {
            SetFontStyle(FontStyle.Bold, txtContent.SelectionFont == null ? false : txtContent.SelectionFont.Bold);          
        }

        private void LblItalic_Click(object sender, EventArgs e)
        {
            SetFontStyle(FontStyle.Italic, txtContent.SelectionFont == null?false: txtContent.SelectionFont.Italic) ;
        }

        private void LblUnderline_Click(object sender, EventArgs e)
        {
            SetFontStyle(FontStyle.Underline, txtContent.SelectionFont == null ? false : txtContent.SelectionFont.Underline);
            
        }

        private void LblStrikeout_Click(object sender, EventArgs e)
        {
            SetFontStyle(FontStyle.Strikeout, txtContent.SelectionFont == null ? false : txtContent.SelectionFont.Strikeout);     
        }
        #endregion
        #region 菜单设置
        private void 撤销ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtContent.Undo();
        }

        private void 重做ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtContent.Redo();
        }

        private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtContent.Cut();
        }

        private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtContent.Copy();
        }

        private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtContent.Paste();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtContent.SelectedText = "";
        }

        private void 全选ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtContent.SelectAll();
        }

        private void MenuNote_Opening(object sender, CancelEventArgs e)
        {
            撤销ToolStripMenuItem.Enabled = txtContent.CanUndo;
            重做ToolStripMenuItem.Enabled = txtContent.CanRedo;
            剪切ToolStripMenuItem.Enabled = txtContent.SelectedText != "";
            复制ToolStripMenuItem.Enabled = txtContent.SelectedText != "";
            粘贴ToolStripMenuItem.Enabled = Clipboard.GetText() != "";
            删除ToolStripMenuItem.Enabled = txtContent.SelectedText != "";
            字体ToolStripMenuItem.Enabled = txtContent.SelectedText != "";
            字体颜色ToolStripMenuItem.Enabled = txtContent.SelectedText != "";
            不透明ToolStripMenuItem.Checked = this.Opacity == 1;
            for (int i = 2; i < 透明度ToolStripMenuItem.DropDownItems.Count; i++)
            {
                var item = (ToolStripMenuItem)透明度ToolStripMenuItem.DropDownItems[i];
                item.Checked = this.Opacity.ToString("0%") == item.Text;
            }
        }

        private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            fontDialog1.Font = txtContent.SelectionFont;
            if (fontDialog1.ShowDialog() == DialogResult.OK)
            {
                txtContent.SelectionFont = fontDialog1.Font;
                SaveSticky();
            }
        }

        private void 字体颜色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            colorDialog1.Color = txtContent.SelectionColor;
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                txtContent.SelectionColor = colorDialog1.Color;
                SaveSticky();
            }
        }
        private void 背景颜色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            colorDialog1.Color = StickyBackColor;
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                StickyBackColor = colorDialog1.Color;
                SaveSticky();
            }
        }
        private void LblTopMost_Click(object sender, EventArgs e)
        {
            this.TopMost = !this.TopMost;
            SaveSticky();
        }
        private void 插入时间ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtContent.SelectedText = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }

        private void 不透明ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Opacity = 1; SaveSticky();
        }

        private void toolStripMenuItem7_Click(object sender, EventArgs e)
        {
            this.Opacity = 0.6; SaveSticky();
        }

        private void toolStripMenuItem8_Click(object sender, EventArgs e)
        {
            this.Opacity = 0.7; SaveSticky();
        }

        private void toolStripMenuItem9_Click(object sender, EventArgs e)
        {
            this.Opacity = 0.75; SaveSticky();
        }

        private void toolStripMenuItem10_Click(object sender, EventArgs e)
        {
            this.Opacity = 0.8; SaveSticky();
        }

        private void toolStripMenuItem11_Click(object sender, EventArgs e)
        {
            this.Opacity = 0.85; SaveSticky();
        }

        private void toolStripMenuItem12_Click(object sender, EventArgs e)
        {
            this.Opacity = 0.9; SaveSticky();
        }

        private void toolStripMenuItem13_Click(object sender, EventArgs e)
        {
            this.Opacity = 0.95; SaveSticky();
        }
        #endregion
        private void FrmSticky_Load(object sender, EventArgs e)
        {
        }
        private int DeskShow = 1;
        /// <summary>
        /// 保存便笺
        /// </summary>
        private void SaveSticky()
        {
            if (ProcUse) { return; }
            RyQuickSQL mySQL = new RyQuickSQL("Sticky");
            mySQL.AddField("RichContent", txtContent.Rtf);
            mySQL.AddField("Title", this.Text);
            mySQL.AddField("Content", txtContent.Text);
            mySQL.AddField("DeskShow", DeskShow);
            ryCommon.Storage mStor = new ryCommon.Storage();
            mStor.SelectNodeBySet();
            mStor.SetAttrValue("StickyBackColor", ColorTranslator.ToHtml(StickyBackColor));
            mStor.SetAttrValue("Left", this.Left);
            mStor.SetAttrValue("Top", this.Top);
            if (!IsMini)
            {
                mStor.SetAttrValue("Width", this.Width);
                mStor.SetAttrValue("Height", this.Height);
            }
            else
            {
                mStor.SetAttrValue("Width",NormalWidth);
                mStor.SetAttrValue("Height",NormalHeight);
            }
            mStor.SetAttrValue("TopMost", this.TopMost);
            mStor.SetAttrValue("Opacity", this.Opacity);
            mStor.SetAttrValue("PullOverHide", this.靠边隐藏ToolStripMenuItem.Checked);
            mySQL.AddField("Setting", mStor.XMLText);
            mySQL.AddField("editTime", DateTime.Now);
            DataProvider mydb = new DataProvider();
            IDbInterface db = Itrycn_Db.CreateDataProvider(Itrycn_Db.dataType);
            if (db.ConnDb(Itrycn_Db.SQLConnStr) == 1)
            {
                if (IsAdd && Id == -1)
                {
                    mySQL.AddField("addTime", DateTime.Now);
                    var ds = db.ReadData(mySQL.GetInsertSQL() + ";select last_insert_rowid();", mySQL);
                    if (mydb.HaveData(ds))
                    {
                        var row = mydb.GetData(ds);
                        Id = row[0].ToInt();
                    }
                }
                else
                { db.ExecuteNonQuery(mySQL.GetUpdateSQL() + " where id=" + Id, mySQL); }
            }
            db.Free();
        }
        /// <summary>
        /// 加载便笺信息
        /// </summary>
        public void LoadSticky(bool _isAdd,int _Id)
        {
            IsAdd = _isAdd;
            Id = _Id;
            LoadSticky();
        }
        /// <summary>
            /// 加载便笺信息
            /// </summary>
        public void LoadSticky()
        {
            ProcUse = true;
            DataProvider mydb = new DataProvider();
            IDbInterface db = Itrycn_Db.CreateDataProvider(Itrycn_Db.dataType);
            if (db.ConnDb(Itrycn_Db.SQLConnStr) == 1)
            {
                var ds = db.ReadData("select * from Sticky where id="+Id);
                if(mydb.HaveData(ds))
                {
                    var row = mydb.GetData(ds);
                    #region 读取便笺信息
                    this.Text= row["Title"].ToString();
                    if (this.Text.Length == 0) { this.Text = "默认标题"; }
                    try
                    {
                        txtContent.Rtf = row["RichContent"].ToString();
                    }
                    catch { }
                    ryCommon.Storage mStor = new ryCommon.Storage(row["Setting"].ToString());
                    mStor.SelectNodeBySet();
                    this.Location = new Point(mStor.GetAttrValue("Left", 0), mStor.GetAttrValue("Top", 0));
                    this.Size = new Size(mStor.GetAttrValue("Width", 0), mStor.GetAttrValue("Height", 0));
                    if (this.Height < 50) { this.Height = 300; }
                    if (this.Width < 50) { this.Width = 200; }
                    if (this.Top < 0) { this.Top = 0; }
                    if (this.Left < 0) { this.Left = 0; }
                    var screen = Screen.GetWorkingArea(this);
                    if (this.Top > screen.Bottom) { this.Top = screen.Bottom-this.Height; }
                    if (this.Left > screen.Right) { this.Left = screen.Right - this.Width; }
                    if (this.Left < 0) { this.Left = 0; }
                    this.TopMost = mStor.GetAttrValue("TopMost", false);
                    this.置顶显示ToolStripMenuItem.Checked = this.TopMost;
                    this.Opacity = mStor.GetAttrValue("Opacity", 0d);
                    try
                    {
                        StickyBackColor = ColorTranslator.FromHtml(mStor.GetAttrValue("StickyBackColor"));
                    }
                    catch { }
                    this.靠边隐藏ToolStripMenuItem.Checked = mStor.GetAttrValue("PullOverHide", true);
                    #endregion
                }
            }
            db.Free();
            Invalidate();
            ProcUse = false;
        }

        private void txtContent_TextChanged(object sender, EventArgs e)
        {
            dt_change = DateTime.Now;
            isChange = true;
        }
        /// <summary>
        /// 便笺状态是否发生改变
        /// </summary>
        bool isChange = false;
        /// <summary>
        /// 经过多少毫秒延迟后来检测状态
        /// </summary>
        int delayTime = 2000;
        DateTime dt_change = DateTime.Now;
        /// <summary>
        /// 便笺状态发生改变时
        /// </summary>
        private void OnChange()
        {
            SaveSticky();
        }
        private void FrmSticky_ResizeEnd(object sender, EventArgs e)
        {
            if(!IsMini)
            SaveSticky();
        }

        private void FrmSticky_LocationChanged(object sender, EventArgs e)
        {
            dt_change = DateTime.Now;
            isChange = true;
        }
        private int NormalWidth = 0;
        private int NormalHeight = 0;
        private bool IsMini = false;
        private void SaveSize()
        {
            NormalWidth = this.Width;
            NormalHeight = this.Height;
        }
        [DllImport("User32.dll")]
        private static extern bool PtInRect(ref Rectangle Rects, Point lpPoint);
        private void Timer1_Tick(object sender, EventArgs e)
        {
            if(isChange && dt_change.AddMilliseconds(delayTime)<DateTime.Now)
            {
                isChange = false;               
                SaveSticky();
            }
            if (RyForm.GetActiveWindow() == Handle)
            {
                if (!PnlTop.Visible)
                {
                    TxtTitle.Text = Text;
                    PnlTop.Visible = true;
                }
                if (!PnlBottom.Visible)
                {
                    controlBoxManager.ButtonVisible(true);
                    Invalidate();
                    txtContent.Height = PnlBottom.Top - txtContent.Top - 2;
                    PnlBottom.Visible = true;
                }
            }
            else
            {
                if (PnlTop.Visible)
                {
                    Text = TxtTitle.Text;
                    PnlTop.Visible = false;
                    Invalidate();
                }
                if (PnlBottom.Visible)
                {
                    controlBoxManager.ButtonVisible(false);
                    Invalidate();
                    txtContent.Height = this.Height - txtContent.Top - 5;
                    PnlBottom.Visible = false;
                }
            }
            if (this.WindowState == FormWindowState.Normal && 靠边隐藏ToolStripMenuItem.Checked)
            {
                System.Drawing.Point cursorPoint = new Point(Cursor.Position.X, Cursor.Position.Y);//获取鼠标在屏幕的坐标点
                Rectangle Rects = new Rectangle(this.Left, this.Top, this.Left + this.Width, this.Top + this.Height);//存储当前窗体在屏幕的所在区域
                bool prInRect = PtInRect(ref Rects, cursorPoint);
                var cur_screen = Screen.FromControl(this);
                if (prInRect)
                {//当鼠标在当前窗体内
                    if (IsMini)
                    {
                        IsMini = false;
                        this.Size = new Size(NormalWidth, NormalHeight);
                        if (this.Top <= 0)//窗体的Top属性小于0
                        {
                            this.Top = 0;
                        }
                        else if (this.Left <= 0)//窗体的Left属性小于0
                        {
                            this.Left = 0;
                        }
                        else if (this.Right > cur_screen.WorkingArea.Width)//窗体的Right属性大于屏幕宽度
                        {
                            this.Left = cur_screen.WorkingArea.Width - this.Width;
                        }
                    }
                }
                else
                {
                    //int space = 8;
                    if (!IsMini)
                    {
                        if (this.Top < cur_screen.WorkingArea.Top+ 5)               //当窗体的上边框与屏幕的顶端的距离小于5时
                        {
                            SaveSize(); SaveSticky(); IsMini = true;
                            this.Height = 8;
                            this.Top = 0;
                            //this.Top = 7 - this.Height + space; //将窗体隐藏到屏幕的顶端
                        }
                        else if (this.Left < cur_screen.WorkingArea.Left+ 5)         //当窗体的左边框与屏幕的左端的距离小于5时
                        {
                            // this.Left = 5 - this.Width + space; //将窗体隐藏到屏幕的左端
                            SaveSize(); SaveSticky(); IsMini = true;
                            this.Width = 8;
                            this.Left = 0;
                        }
                        else if (this.Right > cur_screen.WorkingArea.Right - 5)//当窗体的右边框与屏幕的右端的距离小于5时
                        {
                            //this.Left = cur_screen.WorkingArea.Width - 5 - space;//将窗体隐藏到屏幕的右端
                            SaveSize(); SaveSticky(); IsMini = true;
                            this.Width = 8;
                            this.Left = cur_screen.WorkingArea.Width- this.Width;
                        }
                    }
                }
            }
        }
        private void SetFormRegion()
        {
            //if (this.DesignMode) { return; }
            if (base.Region != null)
                base.Region.Dispose();
            Rectangle rect = new Rectangle(Point.Empty, base.Size);
            GraphicsPath path = GraphicsPathHelper.CreateRoundedRect(rect, Radius,  RoundStyle.All, false);
            this.Region = new Region(path);
        }
        private void FrmSticky_FormClosed(object sender, FormClosedEventArgs e)
        {
            Itrycn_Info.StickyList.Remove(this);
        }

        private void FrmSticky_Resize(object sender, EventArgs e)
        {
            //SetFormRoundRectRgn(this, 15, true);
            //SetFormRoundRectRgn(this,15);
            if (this.WindowState == FormWindowState.Minimized)      //如果窗口大小发生改变后变成最小化，则将窗口重新设置为正常大小
            {
                this.WindowState = FormWindowState.Normal;
            }
        }

        private void LblBullet_Click(object sender, EventArgs e)
        {
            txtContent.SelectionBullet = !txtContent.SelectionBullet;
            //Image img = Image.FromFile(@"C:\Users\fengxin\Desktop\order_714.56449553001px_1229794_easyicon.net.png");
            //API.RTB_InsertImg.InsertImage(this.txtContent, img);
        }

        private void LblImage_Click(object sender, EventArgs e)
        {
            if(openFileDialog1.ShowDialog()==DialogResult.OK)
            {
                Image img =RyImage.LoadPic(openFileDialog1.FileName);
                if (img != null)
                { API.RTB_InsertImg.InsertImage(this.txtContent, img); }
                else
                {
                    MessageBox.Show("图片无效。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        /// <summary>
        /// 大小改变时激发
        /// </summary>
        /// <param name="e"></param>
        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            if (!IsMini)
            {
                SetFormRegion();
                if (ControlBox && controlBoxManager != null)
                    controlBoxManager.FormResize();
            }
        }
        private void FrmSticky_SizeChanged(object sender, EventArgs e)
        {
           // SetWindowRegion();
        }
        private void DrawFormIconAndText(Graphics g)
        {
            if (!string.IsNullOrEmpty(Text))
            {
                TextRenderer.DrawText(
                    g,
                    this.Text,
                    TxtTitle.Font,
                    TextRect,
                    ButtonColor,
                    TextFormatFlags.Left |
                    TextFormatFlags.VerticalCenter |
                    TextFormatFlags.EndEllipsis);
            }
        }
        /// <summary>
        /// 画UI
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            DrawFormBackground(e.Graphics);
            DrawFormBorder(e.Graphics);
            DrawFormIconAndText(e.Graphics);
            if (ControlBox)
                controlBoxManager.DrawBoxes(e.Graphics);
        }
        int Radius { get; set; } = 10;
        private void DrawFormBackground(Graphics g)
        {
            SmoothingMode oldMode = g.SmoothingMode;
            g.SmoothingMode = SmoothingMode.AntiAlias;
            using (SolidBrush sb = new SolidBrush(BackColor))
            {
                using (GraphicsPath path = GraphicsPathHelper.CreateRoundedRect(
                    ClientRectangle, Radius, RoundStyle.All, false))
                {
                    g.FillPath(sb, path);
                }
            }
            g.SmoothingMode = oldMode;
        }
        private void DrawFormBorder(Graphics g)
        {
            //if (this.DesignMode) { return; }
            int width = 1;
            Rectangle rect = ClientRectangle;

            SmoothingMode oldMode = g.SmoothingMode;
            g.SmoothingMode = SmoothingMode.AntiAlias;


            // inner border
            if (width > 0)
            {
                using (Pen p = new Pen(Color.FromArgb(230, 231, 232)))
                {
                    //rect.Inflate(-1, -1);
                    using (GraphicsPath path = GraphicsPathHelper.CreateRoundedRect(
                        rect, Radius, RoundStyle.All, true))
                    {
                        g.DrawPath(p, path);
                    }
                }
            }
            g.SmoothingMode = oldMode;
        }
        private void FrmSticky_ResizeBegin(object sender, EventArgs e)
        {
           // SetFormRoundRectRgn(this, 15, false);
        }

        private void 置顶显示ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.TopMost = 置顶显示ToolStripMenuItem.Checked;
        }

        private void TxtTitle_TextChanged(object sender, EventArgs e)
        {
            dt_change = DateTime.Now;
            isChange = true;
        }

        private void 插入星期ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtContent.SelectedText = DateTime.Now.ToString("dddd");
        }
    }
}
