2015-03-02 46 views

回答

2

解决方案是使用从TextBox继承并重写WndProc方法的自定义控件。以下溶液从一个答案适应于similar question

class MouseTransparentTextBox : TextBox 
{ 
    protected override void WndProc(ref Message m) 
    { 
     switch (m.Msg) 
     { 
      case 0x020A: // WM_MOUSEWHEEL 
      case 0x020E: // WM_MOUSEHWHEEL 
       if (this.ScrollBars == ScrollBars.None && this.Parent != null) 
        m.HWnd = this.Parent.Handle; // forward this to your parent 
       base.WndProc(ref m); 
       break; 

      default: 
       base.WndProc(ref m); 
       break; 
     } 
    } 
} 
+0

在我的情况下,这没有奏效。覆盖WndProc是正确的,但我不得不重新发布消息到父控件。 'PostMessage(Parent.Handle,m.Msg,m.WParam,m.LParam);' – RancidOne 2017-03-01 18:53:01

0

一个更直接的方法是捕集鼠标滚轮事件。在发射时,向上或向下移动插入符号很容易,然后使用ScrollToCaret()使htat线可见。

private void ScrollTextBox(object sender, MouseEventArgs e) 
     // Mouse wheel has been turned while text box has focus 
    { 

     // Check scroll amount (+ve is upwards) 
     int deltaWheel = e.Delta; 
     if (deltaWheel != 0) 
     { 
      // Find total number of lines 
      int nLines = edtAddress.Lines.Length; 
      if (nLines > 0) 
      { 
       // Find line containing caret 
       int iLine = edtAddress.GetLineFromCharIndex(edtAddress.SelectionStart); 
       if (iLine >= 0) 
       { 
        // Scroll down 
        if (deltaWheel > 0) 
        { 
         // Move caret to start of previous line 
         if (iLine > 0) 
         { 
          int position = edtAddress.GetFirstCharIndexFromLine(iLine - 1); 
          edtAddress.Select(position, 0); 

         } 
        } 
        else // Scroll up 
        { 
         // Move caret to start of next line 
         if (iLine < (nLines - 1)) 
         { 
          int position = edtAddress.GetFirstCharIndexFromLine(iLine + 1); 
          edtAddress.Select(position, 0); 
         } 
        } 

        // Scroll to new caret position 
        edtAddress.ScrollToCaret(); 
       } 
      }   

     } 

    } 
相关问题