簡體   English   中英

禁用 RichTextBox 的字體大小更改

[英]Disable fontsize change for RichTextBox

在我的應用程序中,我使用的是RichTextBox es,其 readonly 屬性設置為 True。
但是字體大小仍然可以使用鼠標滾輪和默認的 windows 鍵盤快捷鍵更改字體大小( Ctrl + shift + > / < )。

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

要禁用Control+Shift+<Control+Shift+>的組合鍵,您需要為RichTextBox控件實現以下KeyDown事件處理程序:

 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加鼠標滾輪更改字體大小,我知道如何做到這一點的唯一方法是創建一個繼承自RichTextBox的用戶control

完成此操作后,您唯一需要做的就是覆蓋WndProc過程,以便在滾輪移動並按下Ctrl按鈕時有效地禁用任何消息。 請參閱下面的代碼以實現從RichTextBox派生的UserControl

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

這是一個 class,它提供了禁用滾輪和快捷方式縮放作為在設計器視圖中編輯組件時獲得的屬性中的實際選項:

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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM