禁用RichTextBox的fontsize更改

时间:2011-05-29 17:53:12

标签: c# vb.net winforms richtextbox font-size

在我的应用程序中,我使用RichTextBox es,readonly属性设置为True 但是仍然可以使用鼠标滚轮更改字体大小并更改字体大小的默认窗口键盘快捷键( Ctrl + shift + &gt; / < KBD>&LT; )

如何禁用RichTextBox字体大小更改?

2 个答案:

答案 0 :(得分:3)

要禁用Control+Shift+<Control+Shift+>的组合键,您需要为 RichTextBox 控件实现以下 KeyDown 事件处理程序:< / p>

 Private Sub RichTextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyDown

    ' disable the key combination of 
    '     "control + shift + <" 
    '     or
    '     "control + shift + >"
    e.SuppressKeyPress = e.Control AndAlso e.Shift And (e.KeyValue = Keys.Oemcomma OrElse e.KeyValue = Keys.OemPeriod)

End Sub

此代码可防止用户使用键盘命令调整给定RichTextBox中字体的大小。

要使用 Ctrl 加上鼠标滚轮禁用更改字体大小,我知道如何执行此操作的唯一方法是让用户control继承自RichTextBox

完成后,您唯一需要做的就是覆盖WndProc过程,以便在滚轮移动并按下 Ctrl 按钮时有效地禁用任何消息。请参阅以下代码,以实现从UserControl

派生的RichTextBox
Public Class DerivedRichTextBox
    Inherits RichTextBox

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

        ' windows message constant for scrollwheel moving
        Const WM_SCROLLWHEEL As Integer = &H20A

        Dim scrollingAndPressingControl As Boolean = m.Msg = WM_SCROLLWHEEL AndAlso Control.ModifierKeys = Keys.Control

        'if scolling and pressing control then do nothing (don't let the base class know), 
        'otherwise send the info down to the base class as normal
        If (Not scrollingAndPressingControl) Then

            MyBase.WndProc(m)

        End If


    End Sub

End Class

答案 1 :(得分:0)

这是一个提供禁用滚轮和快捷方式的类,可以在设计器视图中编辑组件时获得的属性中作为实际选项进行缩放:

public class RichTextBoxZoomControl : RichTextBox
{
    private Boolean m_AllowScrollWheelZoom = true;
    private Boolean m_AllowKeyZoom = true;

    [Description("Allow adjusting zoom with [Ctrl]+[Scrollwheel]"), Category("Behavior")]
    [DefaultValue(true)]
    public Boolean AllowScrollWheelZoom
    {
        get { return m_AllowScrollWheelZoom; }
        set { m_AllowScrollWheelZoom = value; }
    }

    [Description("Allow adjusting zoom with [Ctrl]+[Shift]+[,] and [Ctrl]+[Shift]+[.]"), Category("Behavior")]
    [DefaultValue(true)]
    public Boolean AllowKeyZoom
    {
        get { return m_AllowKeyZoom; }
        set { m_AllowKeyZoom = value; }
    }

    protected override void WndProc(ref Message m)
    {
        if (!m_AllowScrollWheelZoom && (m.Msg == 0x115 || m.Msg == 0x20a) && (Control.ModifierKeys & Keys.Control) != 0)
            return;
        base.WndProc(ref m);
    }

    protected override void OnKeyDown(KeyEventArgs e)
    {
        if (!this.m_AllowKeyZoom && e.Control && e.Shift && (e.KeyValue == (Int32)Keys.Oemcomma || e.KeyValue == (Int32)Keys.OemPeriod))
            return;
        base.OnKeyDown(e);
    }
}