【问题标题】:Highlighting text in a Winforms textbox突出显示 Winforms 文本框中的文本
【发布时间】:2015-06-18 22:54:22
【问题描述】:

对于 Winforms 文本框,当您单击并拖动文本时,它会突出显示它。有没有办法确定用户拖动的方向?

【问题讨论】:

    标签: winforms textbox highlighting


    【解决方案1】:

    无法使用 Windows 文本框选择 API 获取此信息。例如,EM_GETSEL 消息定义了选择的开始和结束字符位置,但按预定义(排序)顺序。

    但是,您可以通过处理控件的MouseMove 事件来获取此信息。例如:

    textBox1.MouseMove += new MouseEventHandler(textBox1_MouseMove);
    
    void textBox1_MouseMove(object sender, MouseEventArgs e)
    {
        Control tbCtrl = sender as Control;
        // the mouse coordinate values here are relative to the coordinates of the control that raised the event
        int mouseX = e.X;
        ...
    }
    

    通过对mouseX 应用一些逻辑,您可能会发现光标移动的平均方向。如果用户进行线性运动,效果最好。如果您只希望在用户拖动鼠标时引发该事件,您也可以处理文本框的拖放事件以获取类似的鼠标信息。

    【讨论】:

      猜你喜欢
      • 2014-03-30
      • 2011-01-25
      • 1970-01-01
      • 1970-01-01
      • 2014-02-09
      • 2011-02-21
      • 2013-01-23
      • 1970-01-01
      • 2010-10-18
      相关资源
      最近更新 更多