花了三小时,用C#写了个屏幕取词的算法!(有兴趣一起来讨论一下)
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace tiptest
{
/// <summary>
/// You may have some primary information about this class.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ToolTip toolTip1;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.ComponentModel.IContainer components;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.SuspendLayout();
//
// richTextBox1
//
this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.richTextBox1.Font = new System.Drawing.Font("宋体", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((System.Byte)(134)));
this.richTextBox1.Location = new System.Drawing.Point(0, 0);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(344, 273);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
this.richTextBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.richTextBox1_MouseMove);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(344, 273);
this.Controls.Add(this.richTextBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void richTextBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
int total_len = (this.richTextBox1.Left + e.X) / 10;//get total char count
if(total_len < this.richTextBox1.Text.Length)
{
try
{
int front_len = 1;//front length
int after_len = 1;//after length
//get front char count
while(this.richTextBox1.Text[total_len - front_len] != ' ')
front_len ++;
//get after char count
while(this.richTextBox1.Text[total_len + after_len] != ' ')
after_len ++;
//calculate start char index
int sta_index = total_len - (front_len-1);
//calculate that dispaly char length
int dispaly_len = (front_len-1) + after_len;
//get string
string getstr = this.richTextBox1.Text.Substring(sta_index,dispaly_len);
//dispaly string
this.toolTip1.SetToolTip(this.richTextBox1,getstr);
}
catch
{
}
}
}
}
}