In my application I am using RichTextBox
es with readonly property set to True.
But the font size can still be changed using the mouse wheel and default windows keyboard shortcut for fontsize change ( Ctrl + shift + > / < ).
How do I disable RichTextBox
font size change?
To disable the key combinations of Control+Shift+<
or Control+Shift+>
, you need to implement the following KeyDown event handler for your RichTextBox control:
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
This code prevents the user from resizing the font in the given RichTextBox
with keyboard commands.
To disable changing the font size by using Ctrl plus the mousewheel, the only way I know how to do this is to make a user control
that inherits from RichTextBox
.
Once you have done that the only thing you need to do is override the WndProc
procedure so that it effectively disables any messages when the scrollwheel is moving and the Ctrl button is pressed. See the code below for implementing a UserControl
derived from the 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
Here's a class that offers disabling both scrollwheel and shortcuts zoom as actual options in the properties you get when editing the components in the designer view:
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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.