【问题标题】:Hide RichTextBox Scrollbars without losing Mouse Wheel scrolling隐藏 RichTextBox 滚动条而不丢失鼠标滚轮滚动
【发布时间】:2014-07-20 17:21:14
【问题描述】:

我想隐藏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;
    }
}

【问题讨论】:

标签: c# winforms


【解决方案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
        }
    }

【讨论】:

    猜你喜欢
    • 2018-04-20
    • 1970-01-01
    • 2010-12-09
    • 1970-01-01
    • 1970-01-01
    • 2012-02-27
    • 2011-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多