2015-03-02 51 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(); 
       } 
      }   

     } 

    } 
相關問題