Giải pháp của tôi sử dụng mẹo để đạt được những gì bạn muốn.
Khi bạn nhấp đúp vào bên trong vùng văn bản, nó chọn từ cơ bản.
Vì vậy, sử dụng RichTextBox
(TextBox
không nhấp nháy trên sự kiện chuột) trên biểu mẫu của bạn, bạn có thể mô phỏng nhấp đúp khi nút chuột giữa được nhấp (giống như từ điển Babylon). Nếu bạn muốn, bạn cũng có thể sử dụng MouseHover
thay vì MouseDown
. Nó hoạt động.
public partial class Form3 : Form
{
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
public Form3()
{
InitializeComponent();
timer.Interval = 50;
timer.Tick += timer_Tick;
}
void timer_Tick(object sender, EventArgs e)
{
timer.Stop();
MessageBox.Show(richTextBox1.SelectedText);
// do more stuff here, e.g display your tooltip for the selected word or anything else
richTextBox1.SelectionLength = 0; // remove the highlighted color of selection
}
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
private const uint MOUSEEVENTF_LEFTDOWN = 0x02;
private const uint MOUSEEVENTF_LEFTUP = 0x04;
private const uint MOUSEEVENTF_RIGHTDOWN = 0x08;
private const uint MOUSEEVENTF_RIGHTUP = 0x10;
public void DoMouseDoubleClick()
{
//Call the imported function with the cursor's current position
uint X = (uint)Cursor.Position.X;
uint Y = (uint)Cursor.Position.Y;
mouse_event(MOUSEEVENTF_LEFTDOWN, X, Y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTDOWN, X, Y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
timer.Start(); // some delay is required so that mouse event reach to RichTextBox and the word get selected
}
private void richTextBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Middle)
{
DoMouseDoubleClick();
}
}
}
Nguồn
2012-10-25 15:20:38
cảm ơn, điều này hoạt động rực rỡ! Tôi thích regex được sử dụng để đá ra không gian màu trắng quá! :) – Jammerz858
Vui vì bạn thích nó. Lưu ý rằng nó không chỉ đơn giản là loại bỏ không gian màu trắng. Nó chỉ giữ những gì Regex xem xét một từ, vì vậy dấu câu và bất cứ điều gì khác không được coi là một từ được loại bỏ. –