11

与えられたデータに基づいていくつかの値を表示する複数行のテキスト ボックスがあります (通常、1 行に 1 つの値)。

(いくつかの「代替」データを含むツール ヒント ポップアップを表示する目的で) マウスがホバリングしている単語 (または少なくとも行) を取得して、表示する代替を見つけることができるようにしたいと考えています。

テキストボックスとフォントサイズに基づいた計算でこれを行う方法についていくつかのアイデアがありますが、サイズとフォントが頻繁に変更される可能性があるため、この道をたどる方法はありません.

だから...マウスの位置を使用して特定のテキストボックスのテキストを取得する方法はありますか?

4

3 に答える 3

9

これが代替ソリューションです。この MouseMove イベントを TextBox に追加します。

private void txtHoverWord_MouseMove(object sender, MouseEventArgs e)
{
    if (!(sender is TextBox)) return;
    var targetTextBox = sender as TextBox;
    if(targetTextBox.TextLength < 1) return;

    var currentTextIndex = targetTextBox.GetCharIndexFromPosition(e.Location);

    var wordRegex = new Regex(@"(\w+)");
    var words = wordRegex.Matches(targetTextBox.Text);
    if(words.Count < 1) return;

    var currentWord = string.Empty;
    for (var i = words.Count - 1; i >= 0; i--)
    {
        if (words[i].Index <= currentTextIndex)
        {
            currentWord = words[i].Value;
            break;
        }
    }
    if(currentWord == string.Empty) return;
    toolTip.SetToolTip(targetTextBox, currentWord);
}
于 2012-10-25T16:00:45.443 に答える
4

GetCharIndexFromPositionメソッドを使用して、マウスの位置を Text 全体のインデックスにマップします。その位置から、単語全体が得られるまで左右に進みます。

マウスの位置を取得するには、MouseHoverイベントを使用して、毎回ではなく、静止しているときにのみ取得できるようにします (これにより処理が遅くなります)。

于 2012-10-25T15:14:46.897 に答える
2

私のソリューションでは、トリックを使用して目的を達成します。

テキスト領域内をダブルクリックすると、下にある単語が選択されます。

したがって、フォームでRichTextBox(TextBoxマウスイベントで点滅します) を使用すると、マウスの中ボタンがクリックされたときにダブルクリックをシミュレートできます (Babylon 辞書のようなもの)。MouseHover必要に応じて、の代わりに使用することもできますMouseDown。できます。

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();
            }
        }
    }
于 2012-10-25T15:20:38.463 に答える