解决cefsharp在winform中不显示tooltipText问题(网页元素的title提示)

 1.监听网页属性改变事件

webView.PropertyChanged += webView_PropertyChanged;

 

2.拖一个ToolTip控件到窗体

 

3.在webView_PropertyChanged这个事件处理函数中,获取TooltipText并显示出来

  //隐藏toolTip

                if (this.IsHandleCreated)

                {

                    this.BeginInvoke(new MethodInvoker(() =>

                    {

                        if (this.IsHandleCreated && !this.IsDisposed)

                        {

                            if (this.toolTip1.Active)

                            {

                                this.toolTip1.Hide(this);

                            }

                        }

                    }));

                }





                if (e.PropertyName == "TooltipText") //tooltipText改变事件

                {

                    string tooltipText = this.webView.TooltipText;

                    Point elementPos = new Point(Cursor.Position.X - this.Location.X, Cursor.Position.Y - this.Location.Y);

                    if (!String.IsNullOrEmpty(tooltipText))

                    {

                        this.Invoke(new MethodInvoker(() =>

                        {

                            System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();

                            timer.Interval = 600;

                            timer.Tick += (timer_sender, timer_e) =>

                            {

                                Point nowPos = new Point(Cursor.Position.X - this.Location.X, Cursor.Position.Y - this.Location.Y);

                                //if ((nowPos.X <= elementPos.X + 20 && nowPos.X >= elementPos.X - 20) && (nowPos.Y <= elementPos.Y + 20 && nowPos.Y >= elementPos.Y - 20)) //鼠标停留了500ms(范围限定在一定范围)

                                if (this.webView.TooltipText == tooltipText) //600毫秒后,tooltipText没变

                                {

                                    this.toolTip1.Show(tooltipText, this, nowPos.X + 15, nowPos.Y + 15);

                                }

                                (timer_sender as System.Windows.Forms.Timer).Stop();

                                (timer_sender as System.Windows.Forms.Timer).Dispose();

                            };

                            timer.Start();



                        }));

                    }

                }

 

你可能感兴趣的:(WinForm)