Respond When the User Rotates the Mouse Wheel : Mouse « Windows Presentation Foundation « C# / C Sharp






Respond When the User Rotates the Mouse Wheel

Respond When the User Rotates the Mouse Wheel
  
<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;
            }
        }
    }
}

   
    
  








Related examples in the same category

1.A line which monitors the mouse entering its areaA line which monitors the mouse entering its area
2.Mouse Position and TranslateTransformMouse Position and TranslateTransform
3.Mouse PositionMouse Position
4.Detect whether the mouse button is pressed or released using the MouseButtonState property.Detect whether the mouse button is pressed or released using the MouseButtonState property.
5.Handler MouseLeftButtonDown and MouseLeftButtonUp eventsHandler MouseLeftButtonDown and MouseLeftButtonUp events
6.Mouse Enter and leave a BorderMouse Enter and leave a Border
7.Make an object follow the mouse pointer as it moves on the screen.Make an object follow the mouse pointer as it moves on the screen.
8.Use Mouse.AddPreviewMouseDownHandler(myEllipse, PreviewMouseDownEllipse);Use Mouse.AddPreviewMouseDownHandler(myEllipse, PreviewMouseDownEllipse);
9.Use Mouse.AddMouseDownHandler(myEllipse, MouseDownEllipse);Use Mouse.AddMouseDownHandler(myEllipse, MouseDownEllipse);
10.Use Mouse.PreviewMouseDown /Mouse.MouseDown Attribute from Grid elementUse Mouse.PreviewMouseDown /Mouse.MouseDown Attribute from Grid element
11.Grid MouseMoveGrid MouseMove
12.Grid MouseLeftButtonDownGrid MouseLeftButtonDown
13.Grid MouseLeftButtonUpGrid MouseLeftButtonUp
14.UI Element Mouse Clicked EventsUI Element Mouse Clicked Events
15.Handles the MouseDown event on the CanvasHandles the MouseDown event on the Canvas
16.Handles the MouseDown event on the UniformGridHandles the MouseDown event on the UniformGrid
17.Mouse cursor override and clearMouse cursor override and clear
18.Check the mouse event sourceCheck the mouse event source
19.Use the Mouse Wheel action methods that are defined by the IScrollInfo interfaceUse the Mouse Wheel action methods that are defined by the IScrollInfo interface
20.Mouse.LostMouseCaptureEventMouse.LostMouseCaptureEvent
21.Use Mouse.Capture to let a Control capture an eventUse Mouse.Capture to let a Control capture an event
22.Capture Mouse EllipseCapture Mouse Ellipse
23.Get mouse position with Mouse.GetPositionGet mouse position with Mouse.GetPosition
24.Point Hit TestPoint Hit Test
25.Replease mouse with Mouse.Capture(null)Replease mouse with Mouse.Capture(null)
26.Hit Result BehaviorHit Result Behavior