隐藏RichTextBox滚动条而不会丢失鼠标滚轮滚动

时间:2014-07-19 19:29:32

标签: c# winforms

我想要隐藏RichTextBox的垂直和水平滚动条,而不会失去使用鼠标滚轮滚动它的能力......我已经在这里和那里搜索过而没有找到有效的解决方案问题?

到目前为止我尝试了什么

protected override CreateParams CreateParams
{
    get
    {
        CreateParams cp = base.CreateParams;
        cp.Style = cp.Style & ~0x200000; //Hides the scrollbars but doesn't allow mouse wheel scrolling...
        return cp;
    }
}

1 个答案:

答案 0 :(得分:1)

这里的VB.Net解决方案RichTextBox VScrollbar

您可以创建如下所示的自定义控件

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class RichTextBoxX : RichTextBox
{

    //// see http://social.msdn.microsoft.com/Forums/vstudio/en-US/ba339154-95b7-4e13-a2c0-32593cadb984/richtextbox-vscrollbar?forum=vbgeneral
    [DllImport("user32.dll")]
    private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
    private uint WM_VSCROLL = 0x115;
    private const uint SB_LINEUP = 0;
    private const uint SB_LINEDOWN = 1;
    private const uint SB_PAGEUP = 2;
    private const uint SB_PAGEDOWN = 3;
    private const uint SB_TOP = 6;
    private const uint SB_BOTTOM = 7;

    private const uint SB_ENDSCROLL = 8;
    public RichTextBoxX()
    {
        this.ScrollBars = RichTextBoxScrollBars.None;
        this.MouseWheel += Me_MouseWheel;
    }


    private void Me_MouseWheel(object sender, MouseEventArgs e)
    {
        try
        {
            if (e.Delta < 0)
            {
                // Scrolls down
                SendMessage(this.Handle, WM_VSCROLL, (IntPtr)SB_LINEDOWN, IntPtr.Zero);
            }
            else
            {
                // Scrolls up
                SendMessage(this.Handle, WM_VSCROLL, (IntPtr)SB_LINEUP, IntPtr.Zero);
            }
        }
        catch
        {
        }
    }
}

或不创建自定义控件,

    [DllImport("user32.dll")]
    private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
    uint WM_VSCROLL = 0x115;
    private const uint SB_LINEUP = 0;
    private const uint SB_LINEDOWN = 1;
    private const uint SB_PAGEUP = 2;
    private const uint SB_PAGEDOWN = 3;
    private const uint SB_TOP = 6;
    private const uint SB_BOTTOM = 7;
    private const uint SB_ENDSCROLL = 8;


    void richTextBox1_MouseWheel(object sender, MouseEventArgs e)
    {
        if (e.Delta < 0)
        {
            SendMessage(richTextBox1.Handle, WM_VSCROLL, (IntPtr)SB_LINEDOWN, IntPtr.Zero);// Scrolls down
        }
        else
        {
            SendMessage(richTextBox1.Handle, WM_VSCROLL, (IntPtr)SB_LINEUP, IntPtr.Zero);// Scrolls up
        }
    }