Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to Hide ScrollBar of RichtextBox

How to Hide ScrollBar of RichtextBox

Scheduled Pinned Locked Moved C#
jsontutorialquestion
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jay Shankar
    wrote on last edited by
    #1

    Dear CPians, I want to hide RichtextBox's ScrollBar or set its width = 0, since I am scrolling the RichtextBox programetically in runtime using API methods (SetScrollPos , GetScrollPos, PostMessageA). How to achieve this? Thanks in advance, Regards, Jay.

    S 1 Reply Last reply
    0
    • J Jay Shankar

      Dear CPians, I want to hide RichtextBox's ScrollBar or set its width = 0, since I am scrolling the RichtextBox programetically in runtime using API methods (SetScrollPos , GetScrollPos, PostMessageA). How to achieve this? Thanks in advance, Regards, Jay.

      S Offline
      S Offline
      Stefan Troschuetz
      wrote on last edited by
      #2

      What about the RichTextBox.ScrollBars property!? MSDN[^] says "You can also use this property to remove scroll bars from the control to restrict scrolling the contents of the control."


      J 1 Reply Last reply
      0
      • S Stefan Troschuetz

        What about the RichTextBox.ScrollBars property!? MSDN[^] says "You can also use this property to remove scroll bars from the control to restrict scrolling the contents of the control."


        J Offline
        J Offline
        Jay Shankar
        wrote on last edited by
        #3

        This would not solve my purpose. Actually I want to syncronise the scrolling of two RichTextBoxes, say RichTextBox1 and RichTextBox2 I am overriding WndProc method of RichTextBox1 and sending System.Windows.Forms.Message m to RichTextBox2 by raising an event and passing m.WParam, m.LParam. If I set RichTextBox2.ScrollBars = None, the above would not work.

        H 1 Reply Last reply
        0
        • J Jay Shankar

          This would not solve my purpose. Actually I want to syncronise the scrolling of two RichTextBoxes, say RichTextBox1 and RichTextBox2 I am overriding WndProc method of RichTextBox1 and sending System.Windows.Forms.Message m to RichTextBox2 by raising an event and passing m.WParam, m.LParam. If I set RichTextBox2.ScrollBars = None, the above would not work.

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          What does synchronization have to do with "How to Hide ScrollBar of RichtextBox", which is what your subject says? The first reply answered your subject. You don't pass a Message to WndProc. The RichTextBox encapsulates the Rich Edit common control, which manages the scroll bars itself. You only need to P/Invoke SendMessage (or SetScrollPos, as you asked about before) and set the scroll position. This results in the scroll messages being sent and the scroll bars adjusted automatically, all something done by the Rich edit control. The only reason to override WndProc is to handle the WM_HSCROLL (0x0114) and WM_VSCROLL (0x0115) messages. As documented in the Platform SDK, the high-order word is the actual position, where the low-order word is the scrolling request. You should read the documentation for these two messages (though they are alike) in the Platform SDK on MSDN[^]. What you then do is call the P/Invoke'd SendMessage (see pinvoke.net[^] if you need help with the signature) with the same message (WM_HSCROLL or WM_VSCROLL) and the same parameters. Do not call WndProc directly. SendMessage (or use PostMessage for asynchronous operations, which should work in most cases) does this.

          Microsoft MVP, Visual C# My Articles

          J 1 Reply Last reply
          0
          • H Heath Stewart

            What does synchronization have to do with "How to Hide ScrollBar of RichtextBox", which is what your subject says? The first reply answered your subject. You don't pass a Message to WndProc. The RichTextBox encapsulates the Rich Edit common control, which manages the scroll bars itself. You only need to P/Invoke SendMessage (or SetScrollPos, as you asked about before) and set the scroll position. This results in the scroll messages being sent and the scroll bars adjusted automatically, all something done by the Rich edit control. The only reason to override WndProc is to handle the WM_HSCROLL (0x0114) and WM_VSCROLL (0x0115) messages. As documented in the Platform SDK, the high-order word is the actual position, where the low-order word is the scrolling request. You should read the documentation for these two messages (though they are alike) in the Platform SDK on MSDN[^]. What you then do is call the P/Invoke'd SendMessage (see pinvoke.net[^] if you need help with the signature) with the same message (WM_HSCROLL or WM_VSCROLL) and the same parameters. Do not call WndProc directly. SendMessage (or use PostMessage for asynchronous operations, which should work in most cases) does this.

            Microsoft MVP, Visual C# My Articles

            J Offline
            J Offline
            Jay Shankar
            wrote on last edited by
            #5

            Dear Heath, I am doing the same. Please refer to the code below: protected override void WndProc(ref System.Windows.Forms.Message m) { if(m.Msg == WM_MOUSEWHEEL) //WM_MOUSEWHEEL 0x020a { if(Control.ModifierKeys != Keys.Control) { MouseWheelRollingEventArgs e = new MouseWheelRollingEventArgs(m.WParam, m.LParam); MouseWheelRolling(this, e); base.WndProc(ref m); } else { return; } } else if( m.Msg == WM_VSCROLL ) { //process similarly for scroll, below //..... //..... base.WndProc (ref m); } else { base.WndProc (ref m); } }

            private void rtb_MouseWheelRolling(object sender, TextEditor.MouseWheelRollingEventArgs e)
            {
            
            	//note : lineSrNo is the other richTextBox I am syncronising
            	const int WM_MOUSEWHELL = 0x020a;
            	PostMessageA((int)lineSrNo.Handle,WM_MOUSEWHELL, (int) e.wp, (int) e.lp);
            }
            

            rtb and lineSrNo are the two richTextBoxes, I want to syncronise. lineSrNo richTextBox contains the lineNumbers of rtb richTextBox. When ever user scrolls the rtb richTextBox(using MouseWheel rolling or ScrollBar) correspondingly lineSrNo richTextBox should also get scrolled. Line numbers are added in the lineSrNo richTextBox when ever user enters any line in rtb richTextBox. lineSrNo richTextBox Locked property is set to True and I am scrolling it programetically. I am able to acheive the target, but I do not want scrollBar in lineSrNo richTextBox That is the reason, I am asking the way out to hide the scrollBar of the richTextBox or set its width to 0. Regards, Jay

            H 1 Reply Last reply
            0
            • J Jay Shankar

              Dear Heath, I am doing the same. Please refer to the code below: protected override void WndProc(ref System.Windows.Forms.Message m) { if(m.Msg == WM_MOUSEWHEEL) //WM_MOUSEWHEEL 0x020a { if(Control.ModifierKeys != Keys.Control) { MouseWheelRollingEventArgs e = new MouseWheelRollingEventArgs(m.WParam, m.LParam); MouseWheelRolling(this, e); base.WndProc(ref m); } else { return; } } else if( m.Msg == WM_VSCROLL ) { //process similarly for scroll, below //..... //..... base.WndProc (ref m); } else { base.WndProc (ref m); } }

              private void rtb_MouseWheelRolling(object sender, TextEditor.MouseWheelRollingEventArgs e)
              {
              
              	//note : lineSrNo is the other richTextBox I am syncronising
              	const int WM_MOUSEWHELL = 0x020a;
              	PostMessageA((int)lineSrNo.Handle,WM_MOUSEWHELL, (int) e.wp, (int) e.lp);
              }
              

              rtb and lineSrNo are the two richTextBoxes, I want to syncronise. lineSrNo richTextBox contains the lineNumbers of rtb richTextBox. When ever user scrolls the rtb richTextBox(using MouseWheel rolling or ScrollBar) correspondingly lineSrNo richTextBox should also get scrolled. Line numbers are added in the lineSrNo richTextBox when ever user enters any line in rtb richTextBox. lineSrNo richTextBox Locked property is set to True and I am scrolling it programetically. I am able to acheive the target, but I do not want scrollBar in lineSrNo richTextBox That is the reason, I am asking the way out to hide the scrollBar of the richTextBox or set its width to 0. Regards, Jay

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #6

              Have you tried getting the handle to the scroll bars (use FindWindowEx, or some controls actually define a message to return the scrollbar handle) and simply P/Invoking SetWindowsLongPtr passing ~WS_VISIBLE OR'ed with the previous style? That would literally hide the scrollbars but should still respond to scroll messages.

              Microsoft MVP, Visual C# My Articles

              J 1 Reply Last reply
              0
              • H Heath Stewart

                Have you tried getting the handle to the scroll bars (use FindWindowEx, or some controls actually define a message to return the scrollbar handle) and simply P/Invoking SetWindowsLongPtr passing ~WS_VISIBLE OR'ed with the previous style? That would literally hide the scrollbars but should still respond to scroll messages.

                Microsoft MVP, Visual C# My Articles

                J Offline
                J Offline
                Jay Shankar
                wrote on last edited by
                #7

                I tried getting the Handle of richTextBox's scrollBar using the following code, but could not succeed. Can you please point out where am I going wrong?

                public class ScrollBarHiddenRTB : System.Windows.Forms.RichTextBox
                {
                
                	[System.Runtime.InteropServices.DllImport("user32.dll")]
                	public static extern IntPtr FindWindowEx(IntPtr parent, IntPtr next, string sClassName, IntPtr sWindowTitle);
                
                	[System.Runtime.InteropServices.DllImport("user32.dll")]
                	public static extern int SetWindowLongPtr( IntPtr hWnd, int nIndex, int dwNewLong);
                
                	private  const int WS_VISIBLE = 0x10000000;
                
                	public ScrollBarHiddenRTB() : base()
                	{
                		this.HandleCreated += new EventHandler(CreateRTBHandle);
                
                	}
                
                
                	private void CreateRTBHandle(object sender, EventArgs e) 
                	{
                		ScrollBarHiddenRTB self = sender as ScrollBarHiddenRTB;
                		System.Diagnostics.Debug.Assert(self != null);
                		IntPtr scrollHandle = FindWindowEx(self.Handle, IntPtr.Zero, "ScrollBar", IntPtr.Zero);
                		if(scrollHandle != IntPtr.Zero)
                		{
                			MessageBox.Show(scrollHandle.ToString());
                		}
                
                	}
                
                }
                

                Regards, Jay.

                H 1 Reply Last reply
                0
                • J Jay Shankar

                  I tried getting the Handle of richTextBox's scrollBar using the following code, but could not succeed. Can you please point out where am I going wrong?

                  public class ScrollBarHiddenRTB : System.Windows.Forms.RichTextBox
                  {
                  
                  	[System.Runtime.InteropServices.DllImport("user32.dll")]
                  	public static extern IntPtr FindWindowEx(IntPtr parent, IntPtr next, string sClassName, IntPtr sWindowTitle);
                  
                  	[System.Runtime.InteropServices.DllImport("user32.dll")]
                  	public static extern int SetWindowLongPtr( IntPtr hWnd, int nIndex, int dwNewLong);
                  
                  	private  const int WS_VISIBLE = 0x10000000;
                  
                  	public ScrollBarHiddenRTB() : base()
                  	{
                  		this.HandleCreated += new EventHandler(CreateRTBHandle);
                  
                  	}
                  
                  
                  	private void CreateRTBHandle(object sender, EventArgs e) 
                  	{
                  		ScrollBarHiddenRTB self = sender as ScrollBarHiddenRTB;
                  		System.Diagnostics.Debug.Assert(self != null);
                  		IntPtr scrollHandle = FindWindowEx(self.Handle, IntPtr.Zero, "ScrollBar", IntPtr.Zero);
                  		if(scrollHandle != IntPtr.Zero)
                  		{
                  			MessageBox.Show(scrollHandle.ToString());
                  		}
                  
                  	}
                  
                  }
                  

                  Regards, Jay.

                  H Offline
                  H Offline
                  Heath Stewart
                  wrote on last edited by
                  #8

                  A few tips, first: don't use <code> within a <pre> tag. The former is for in-line code, and the latter is for block code. Also, don't handle events for the control class you're extending. It is much faster and you have more control by overriding OnHandleCreated, or any of the On_EventName_ handlers defined by the base class. This lets you call base.OnHandleCreated if you want (in this case you should, but there are times you might not want to when overriding WndProc, for example). It's also much faster code. To invoke a delegate, many, many IL instructions are required. To call a virtual function it's one: callvirt. The Rich Edit control doesn't expose its scroll bars in the same way as other controls. If you look at the message documentation for the Rich Edit control like I mentioned before you might notice an EM_SHOWSCROLLBAR message you can send by P/Invoking SendMessage like so:

                  [DllImport("user32.dll")]
                  private static extern int SendMessage(IntPtr hWnd, int msg, ScrollBar sb, bool show);
                  private enum ScrollBar
                  {
                  Horizontal,
                  Vertical
                  }

                  Please note that this signature is not 64-bit compatible, however. In such a case, both return value and the last two params should be IntPtrs, which is a system-dependent bit width. Then, just send EM_SHOWSCROLLBAR (0x0460) with the params you require.

                  Microsoft MVP, Visual C# My Articles

                  J 1 Reply Last reply
                  0
                  • H Heath Stewart

                    A few tips, first: don't use <code> within a <pre> tag. The former is for in-line code, and the latter is for block code. Also, don't handle events for the control class you're extending. It is much faster and you have more control by overriding OnHandleCreated, or any of the On_EventName_ handlers defined by the base class. This lets you call base.OnHandleCreated if you want (in this case you should, but there are times you might not want to when overriding WndProc, for example). It's also much faster code. To invoke a delegate, many, many IL instructions are required. To call a virtual function it's one: callvirt. The Rich Edit control doesn't expose its scroll bars in the same way as other controls. If you look at the message documentation for the Rich Edit control like I mentioned before you might notice an EM_SHOWSCROLLBAR message you can send by P/Invoking SendMessage like so:

                    [DllImport("user32.dll")]
                    private static extern int SendMessage(IntPtr hWnd, int msg, ScrollBar sb, bool show);
                    private enum ScrollBar
                    {
                    Horizontal,
                    Vertical
                    }

                    Please note that this signature is not 64-bit compatible, however. In such a case, both return value and the last two params should be IntPtrs, which is a system-dependent bit width. Then, just send EM_SHOWSCROLLBAR (0x0460) with the params you require.

                    Microsoft MVP, Visual C# My Articles

                    J Offline
                    J Offline
                    Jay Shankar
                    wrote on last edited by
                    #9

                    Dear Heath, Thanks for the tips. I will keep in mind for future postings. I tried sending EM_SHOWSCROLLBAR message by P/Invoking SendMessage, But It did not work in my case. However, I could solve the problem by P/Invoking GetSystemMetrics, CreateRectRgn, SetWindowRgn. I am pasting the code for future reference.

                    private const int SM_CXVSCROLL = 2;

                    //
                    [System.Runtime.InteropServices.DllImport("user32")]
                    public static extern int GetSystemMetrics(int nIndex);
                    //

                    [System.Runtime.InteropServices.DllImport("gdi32")]
                    public static extern int CreateRectRgn(int X1, int Y1, int X2, int Y2) ;
                    //

                    [System.Runtime.InteropServices.DllImport("user32")]
                    public static extern int SetWindowRgn(int hwnd, int hRgn, bool bRedraw);

                    public void HideVerticalScrollBar(RichTextBox rtb)
                    {
                    //get scroll bar width
                    int scrollbarWidth = WinApi.GetSystemMetrics(SM_CXVSCROLL);
                    // handle of the region
                    int handleRgn = WinApi.CreateRectRgn(0, 0, rtb.Width , rtb.Height);
                    //change the width of the rtb
                    rtb.Width += scrollbarWidth;
                    //set window region
                    WinApi.SetWindowRgn(rtb.Handle.ToInt32(), handleRgn, true);
                    }

                    Regards, Jay.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups