TextBox with vertical scrollbar on the left side

后端 未结 2 1980
别跟我提以往
别跟我提以往 2021-01-07 04:56

I\'m developing a Windows Forms application in C#, which has a multiline TextBox control on a form.

Due to specific (irrelevant) reasons, this TextBox needs a vertic

2条回答
  •  时光说笑
    2021-01-07 05:28

    I took some of the example code from Rachel Gallen's link and made this version of the TextBox:

    public class TextBoxWithScrollLeft : TextBox {
      private const int GWL_EXSTYLE = -20;
      private const int WS_EX_LEFTSCROLLBAR = 16384;
    
      [DllImport("user32", CharSet = CharSet.Auto)]
      public extern static int GetWindowLong(IntPtr hWnd, int nIndex);
    
      [DllImport("user32")]
      public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
    
      protected override void OnHandleCreated(EventArgs e) {
        base.OnHandleCreated(e);
        int style = GetWindowLong(Handle, GWL_EXSTYLE);
        style = style | WS_EX_LEFTSCROLLBAR;
        SetWindowLong(Handle, GWL_EXSTYLE, style);
      }
    }
    

    I've never done it before, but the results seem to have worked:

    enter image description here

提交回复
热议问题