2011-05-29 139 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); 
    } 
}