我有一些RichTextBox
:
<RichTextBox x:Name="logTextBox" MouseWheel="LogTextBox_MouseWheel" ... >
我想通过使用ctrl + mousewheel增加字体的大小。我几乎明白了:
private void LogTextBox_MouseWheel(object sender, MouseWheelEventArgs e)
{
if(Keyboard.Modifiers != ModifierKeys.Control)
{
return;
}
if(e.Delta > 0)
{
++logTextBox.FontSize;
}
else
{
--logTextBox.FontSize;
}
}
但是有一个问题。为了使它起作用,我需要向上/向下滚动到文本框的垂直开始/结束。但我希望它可以在整个文本框中的任何位置正常工作。该如何解决?
答案 0 :(得分:2)
改为使用PreviewMouseLogic,并确保设置了Handled标志:
<RichTextBox x:Name="logTextBox" PreviewMouseWheel="LogTextBox_PreviewMouseWheel" />
private void LogTextBox_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
if (Keyboard.Modifiers != ModifierKeys.Control)
{
return;
}
e.Handled = true;
if (e.Delta > 0)
{
++logTextBox.FontSize;
}
else
{
--logTextBox.FontSize;
}
}
答案 1 :(得分:0)
这对于VB.NET效果很好:
Private Sub TextBox1_MouseWheel(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseWheel
If My.Computer.Keyboard.CtrlKeyDown = True Then
If e.Delta > 0 Then
If TrackBar1.Value = 100 Then
Else
TrackBar1.Value += 1
Label1.Font = New Font(Label1.Font.FontFamily, TrackBar1.Value)
TextBox1.Font = Label1.Font
End If
Else
If TrackBar1.Value = 1 Then
Else
TrackBar1.Value -= 1
Label1.Font = New Font(Label1.Font.FontFamily, TrackBar1.Value)
TextBox1.Font = Label1.Font
End If
End If
Return
End If
End Sub