2011-05-29 138 views
1

在我的應用程序中,我正在使用只讀屬性設置爲True的RichTextBox es。
但仍然可以使用用於字體大小改變(Ctrl鍵 + + >/<)鼠標滾輪和缺省窗鍵盤快捷鍵被改變的字體大小。爲RichTextBox禁用字體大小更改

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

回答

3

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

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 
+0

謝謝!這很好用!但如何禁用鼠標滾輪上的文本放大RichTextBox? – vaichidrewar 2011-05-29 18:54:43

+0

我已編輯我的帖子,以包括禁用鼠標滾輪功能。 – 2011-05-29 19:17:32

+0

非常感謝傑森! – vaichidrewar 2011-05-29 19:20:12

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