Back to Devexpress

Mouse Services

windowsforms-4108-controls-and-libraries-scheduler-services-mouse-services.md

latest3.4 KB
Original Source

Mouse Services

  • May 13, 2021
  • 2 minutes to read

The IMouseHandlerService in the XtraScheduler provides the capability to respond to mouse events. It has the following delegates:

  • OnMouseMove (PortableMouseEventArgs e) - occurs when the user moves the mouse pointer over a control.
  • OnMouseDown (PortableMouseEventArgs e) - occurs when the user presses the mouse button.
  • OnMouseUp (PortableMouseEventArgs e) - occurs when the user releases the mouse button.
  • OnMouseWheel (PortableMouseEventArgs e) - occurs when the mouse wheel moves.

To handle these events you should first create a new class inherited from the DevExpress.Services.MouseHandlerServiceWrapper and override its methods as needed. The following code snippet illustrates how this can be done:

csharp
using DevExpress.Services;
// ...
public class MyMouseHandlerService : MouseHandlerServiceWrapper
{
    IServiceProvider provider;

    public MyMouseHandlerService(IServiceProvider provider, IMouseHandlerService service)
        : base(service)
    {
        this.provider = provider;
    }

    public override void OnMouseWheel(PortableMouseEventArgs e)
    {
        // Do something when the wheel is rotated.
        base.OnMouseWheel(e);
    }
}
vb
Imports DevExpress.Services
' ...
Public Class MyMouseHandlerService
    Inherits MouseHandlerServiceWrapper
    Private provider As IServiceProvider

    Public Sub New(ByVal provider As IServiceProvider, ByVal service As IMouseHandlerService)
        MyBase.New(service)
        Me.provider = provider
    End Sub

    Public Overrides Sub OnMouseWheel(ByVal e As PortableMouseEventArgs)
        ' Do something when the wheel is rotated.
        MyBase.OnMouseWheel(e)
    End Sub
End Class

The provider is the SchedulerControl instance that provides the service.

This class will replace the existing service that handles the mouse events.

The following code sample replaces the existing mouse handling service of the XtraScheduler with a custom one.

csharp
using DevExpress.Services;
// ...
IMouseHandlerService oldMouseHandler = 
    (IMouseHandlerService)schedulerControl1.GetService(
        typeof(IMouseHandlerService));
if (oldMouseHandler != null)
{
    MyMouseHandlerService newMouseHandler = 
        new MyMouseHandlerService(schedulerControl1, oldMouseHandler);
    schedulerControl1.RemoveService(typeof(IMouseHandlerService));
    schedulerControl1.AddService(typeof(IMouseHandlerService), newMouseHandler);
}
vb
Imports DevExpress.Services
' ...
Private oldMouseHandler As IMouseHandlerService = _
    CType(schedulerControl1.GetService(GetType(IMouseHandlerService)), _
        IMouseHandlerService)
If Not oldMouseHandler Is Nothing Then
    Dim newMouseHandler As MyMouseHandlerService = _
        New MyMouseHandlerService(schedulerControl1, oldMouseHandler)
    schedulerControl1.RemoveService(GetType(IMouseHandlerService))
    schedulerControl1.AddService(GetType(IMouseHandlerService), newMouseHandler)
End If

A PortableMouseEventArgs object specifies which mouse button is pressed, how many times the mouse button was pressed and released, the coordinates of the mouse, and the amount the mouse wheel moved.