Respond When the User Rotates the Mouse Wheel

image_pdfimage_print


   
  
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="300" Width="300">
    <Canvas>
        <Slider Canvas.Top="10" Canvas.Left="20" Name="sldSlider" 
                Minimum="0" Maximum="1000" Value="500"
                Width="250" MouseWheel="Slider_MouseWheel"/>
        <RichTextBox Canvas.Top="50" Canvas.Left="20" Width="250" Height="100" VerticalScrollBarVisibility="Visible">
            <FlowDocument>
                <List>
                    <ListItem>
                        <Paragraph>
                            <Bold>Bold List Item</Bold>
                        </Paragraph>
                    </ListItem>
                    <ListItem>
                        <Paragraph>
                            <Italic>Italic List Item</Italic>
                        </Paragraph>
                    </ListItem>
                    <ListItem>
                        <Paragraph>
                            <Underline>Underlined List Item</Underline>
                        </Paragraph>
                    </ListItem>
                </List>
                <List>
                    <ListItem>
                        <Paragraph>
                            <Bold>Bold List Item</Bold>
                        </Paragraph>
                    </ListItem>
                    <ListItem>
                        <Paragraph>
                            <Italic>Italic List Item</Italic>
                        </Paragraph>
                    </ListItem>
                    <ListItem>
                        <Paragraph>
                            <Underline>Underlined List Item</Underline>
                        </Paragraph>
                    </ListItem>
                </List>
                <List>
                    <ListItem>
                        <Paragraph>
                            <Bold>Bold List Item</Bold>
                        </Paragraph>
                    </ListItem>
                    <ListItem>
                        <Paragraph>
                            <Italic>Italic List Item</Italic>
                        </Paragraph>
                    </ListItem>
                    <ListItem>
                        <Paragraph>
                            <Underline>Underlined List Item</Underline>
                        </Paragraph>
                    </ListItem>
                </List>
                <Paragraph FontSize="12">
                    Lorem ipsum dolor sit amet, consectetuer adipiscing elit, 
                    sed diam nonummy nibh euismod tincidunt ut laoreet dolore 
                    magna aliquam erat volutpat.
                </Paragraph>
                <Paragraph FontSize="15">
                    Ut wisi enim ad minim veniam, quis nostrud exerci tation 
                    ullamcorper suscipit lobortis nisl ut aliquip ex ea 
                    commodo consequat. Duis autem vel eum iriure.
                </Paragraph>
                <Paragraph FontSize="18">A List</Paragraph>
                <List>
                    <ListItem>
                        <Paragraph>
                            <Bold>Bold List Item</Bold>
                        </Paragraph>
                    </ListItem>
                    <ListItem>
                        <Paragraph>
                            <Italic>Italic List Item</Italic>
                        </Paragraph>
                    </ListItem>
                    <ListItem>
                        <Paragraph>
                            <Underline>Underlined List Item</Underline>
                        </Paragraph>
                    </ListItem>
                </List>
            </FlowDocument>
        </RichTextBox>
        <Rectangle Canvas.Top="160" Canvas.Left="20" Name="shpRectangle"
                   Fill="LightBlue" Width="50" Height="50" 
                   MouseWheel="Rectangle_MouseWheel">
        </Rectangle>
    </Canvas>
</Window>
//File:Window.xaml.cs

using System.Windows;
using System.Windows.Input;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        private void Slider_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            sldSlider.Value += (e.Delta > 0) ? 5 : -5;
        }
        private void Rectangle_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                double newWidth = shpRectangle.Width += (e.Delta > 0) ? 5 : -5;
                if (newWidth < 10) newWidth = 10;
                if (newWidth > 200) newWidth = 200;

                shpRectangle.Width = newWidth;
            }else{
                double newHeight = shpRectangle.Height += (e.Delta > 0) ? 5 : -5;
                if (newHeight < 10) newHeight = 10;
                if (newHeight > 200) newHeight = 200;
                shpRectangle.Height = newHeight;
            }
        }
    }
}