C \ # RichTextBox 를 봉 하여 마우스 가 멈 추고 현재 단어 텍스트 의 tooltip 알림 을 표시 합 니 다.

    class MyRichTextBox : RichTextBox
    {
        System.Timers.Timer timer;
        int cout = 0;
        Point pt = new Point();
        ToolTip tooltip = new ToolTip();
        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            timer = new System.Timers.Timer();
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
            timer.Interval = 500;
            timer.Enabled = false;
            
        }
        protected override void OnMouseHover(EventArgs e)
        {
            base.OnMouseHover(e);
            timer.Enabled = true;
        }
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            timer.Enabled = false;
        }
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (!timer.Enabled)
                timer.Enabled = true;
            cout = 0;
            pt.X = e.Location.X;
            pt.Y = e.Location.Y;
        }
        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (cout >= 1000)
            {
                timer.Enabled = false;
                func();
            }
            else
                cout += 500;
        }
        void func()
        {
            this.Invoke(new MethodInvoker(delegate
                {
                    int index = this.GetCharIndexFromPosition(pt);
                    string ret = "";
                    for (int i = index; i < this.Text.Length; i++)
                    {
                        char c = this.Text[i];
                        if ((c > 64 && c < 91) || (c > 96 && c < 123) || (c > 47 && c < 58))
                        {
                            ret += c.ToString();
                        }
                        else
                            break;
                    }
                    for (int i = index; i - 1 > 0; i--)
                    {
                        char c = this.Text[i - 1];
                        if ((c > 64 && c < 91) || (c > 96 && c < 123) || (c > 47 && c < 58))
                        {
                            ret = c.ToString() + ret;
                        }
                        else
                            break;
                    }
                    if (ret.Length > 0)
                    {
                        //textBox1.Text = ret;
                        tooltip.Show("          :" + ret, this, pt, 5000);
                    }
                }));
        }
    }

좋은 웹페이지 즐겨찾기