110,534
社区成员
发帖
与我相关
我的任务
分享
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);
}
}));
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
System.Timers.Timer timer;
int cout = 0;
Point pt = new Point();
ToolTip tooltip = new ToolTip();
private void Form1_Load(object sender, EventArgs e)
{
timer= new System.Timers.Timer();
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Interval = 500;
timer.Enabled = false;
}
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (cout >= 1000)
{
timer.Enabled = false;
func();
}
else
cout += 500;
}
private void richTextBox1_MouseMove(object sender, MouseEventArgs e)
{
if (!timer.Enabled)
timer.Enabled = true;
cout = 0;
pt.X = e.Location.X;
pt.Y = e.Location.Y;
}
private void richTextBox1_MouseHover(object sender, EventArgs e)
{
timer.Enabled = true;
}
private void richTextBox1_MouseLeave(object sender, EventArgs e)
{
timer.Enabled = false;
}
void func()
{
this.Invoke(new MethodInvoker(delegate
{
int index = richTextBox1.GetCharIndexFromPosition(pt);
string ret = "";
for (int i = index; i < richTextBox1.Text.Length; i++)
{
char c=richTextBox1.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=richTextBox1.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;