Loading...
Searching...
No Matches
Working with the keyboard and mouse

Basics

Indicators are able to react to the keyboard and mouse events.
The following methods, which could be overridden in the indicator, are envisaged for these purposes:

RenderControlMouseEventArgs is declared in the OFT.Rendering.Control namespace. In the classic ATAS, KeyEventArgs is System.Windows.Input.KeyEventArgs; the ATAS X platform declares these methods with its own cross-platform key event type (see the «Developing indicators for ATAS X» article for how Windows types are handled).

Each of these methods should return true or false.
If the method returns true, it means that the indicator processed this event and there is no need to call this event in the following indicators.
This logic is required for situations when there are a number of indicators in the chart and only one of them (the one that was the first to process the event) should process the event.
Mouse and keyboard events are not passed to an indicator that is hidden (its Visible property is false) or locked (its Locked property is true).
Also, in some cases, depending on the mouse position, it is necessary to have a possibility to change the cursor. The GetCursor(RenderControlMouseEventArgs e) method is envisaged for this purpose.
Having overridden this method, the cursor (a StdCursor value from the OFT.Rendering namespace) could be returned depending on the mouse position. Return StdCursor.NULL to keep the default cursor.

Example of working with these events:

using System;
using System.Drawing;
using System.Windows.Input;
using Utils.Common.Logging;
public class SampleKeyboardAndMouseProcessor : Indicator
{
private readonly RenderPen _selectionPen = new(Color.Blue, 3);
private readonly RenderFont _font = new("Arial", 20);
private readonly RenderStringFormat _stringFormat = new() { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center };
private bool _selected;
private Rectangle _rectangle = new Rectangle(30, 30, 150, 50);
private Point _pressedPoint;
private Point _lastPoint;
private bool _pressed;
public SampleKeyboardAndMouseProcessor()
: base(true)
{
EnableCustomDrawing = true;
DenyToChangePanel = true;
DataSeries[0].IsHidden = true;
SubscribeToDrawingEvents(DrawingLayouts.Final);
}
#region Overrides of BaseIndicator
protected override void OnCalculate(int bar, decimal value)
{
}
#endregion
protected override void OnRender(RenderContext context, DrawingLayouts layout)
{
context.FillRectangle(Color.Coral, _rectangle);
if (_selected)
context.DrawRectangle(_selectionPen, _rectangle);
context.DrawString(DateTime.Now.ToString("T"), _font, Color.AliceBlue, _rectangle, _stringFormat);
}
public override bool ProcessMouseClick(RenderControlMouseEventArgs e)
{
this.LogInfo("Mouse click");
return false;
}
public override bool ProcessMouseDown(RenderControlMouseEventArgs e)
{
this.LogInfo("Mouse down");
if (e.Button == RenderControlMouseButtons.Left && IsPointInsideRectangle(_rectangle, e.Location))
{
_pressedPoint = _lastPoint = e.Location;
_pressed = true;
return true;
}
return false;
}
public override bool ProcessMouseUp(RenderControlMouseEventArgs e)
{
this.LogInfo("Mouse up");
if (Math.Abs(e.Location.X - _pressedPoint.X) < 4 && Math.Abs(e.Location.Y - _pressedPoint.Y) < 4 && e.Button == RenderControlMouseButtons.Left && IsPointInsideRectangle(_rectangle, e.Location))
{
_selected = !_selected;
_pressed = false;
return true;
}
if (_pressed)
{
_pressed = false;
return true;
}
return false;
}
public override bool ProcessMouseMove(RenderControlMouseEventArgs e)
{
if (_pressed && _selected)
{
var dX = e.Location.X - _lastPoint.X;
var dY = e.Location.Y - _lastPoint.Y;
_lastPoint = e.Location;
_rectangle.X += dX;
_rectangle.Y += dY;
return true;
}
return false;
}
public override bool ProcessMouseDoubleClick(RenderControlMouseEventArgs e)
{
this.LogInfo("Mouse double click");
return false;
}
public override bool ProcessMouseWheel(int delta)
{
this.LogInfo("Mouse wheel {0}", delta);
return false;
}
public override bool ProcessKeyDown(KeyEventArgs e)
{
this.LogInfo("Key down {0}", e.Key);
return false;
}
public override bool ProcessKeyUp(KeyEventArgs e)
{
this.LogInfo("Key up {0}", e.Key);
return false;
}
public override StdCursor GetCursor(RenderControlMouseEventArgs e)
{
if (_selected && IsPointInsideRectangle(_rectangle, e.Location))
return StdCursor.Hand;
return StdCursor.NULL;
}
private bool IsPointInsideRectangle(Rectangle rectangle, Point point)
{
return point.X >= rectangle.X && point.X <= rectangle.X + rectangle.Width && point.Y >= rectangle.Y && point.Y <= rectangle.Y + rectangle.Height;
}
}
Base class for custom indicators.
Definition Indicator.cs:50
Represents an abstract rendering context for drawing on a chart.
Definition RenderContext.cs:17
abstract int DrawRectangle(RenderPen pen, Rectangle rect)
Draws a rectangle using the given pen and rectangle.
int FillRectangle(Color brush1, Color brush2, Rectangle rect)
Fills a rectangle with a gradient using two specified colors.
Definition RenderContext.cs:85
abstract int DrawString(string s, RenderFont font, Color brush, int x, int y)
Draws a string at the specified position using the specified font and brush.
Definition RenderControlMouseEventArgs.cs:9
RenderControlMouseButtons Button
Definition RenderControlMouseEventArgs.cs:35
Point Location
Definition RenderControlMouseEventArgs.cs:33
Defines a particular format for text, including font face, size, and style attributes....
Definition RenderFont.cs:11
Defines an object used to draw lines and curves. This class cannot be inherited.
Definition RenderPen.cs:15
Encapsulates text layout information (such as alignment, orientation and tab stops) display manipulat...
Definition RenderStringFormat.cs:12
Definition IIndicatorAnalyticsService.cs:1
DrawingLayouts
Enumerates the different drawing layouts available for chart drawings.
Definition DrawingLayouts.cs:14
Definition IRenderContextSettings.cs:1
Definition ExceptionCollector.cs:1
RenderControlMouseButtons
Specifies constants that define which mouse button was pressed.
Definition RenderControlMouseButtons.cs:10
Definition RenderAdjustableArrowCap.cs:2
Definition IRenderContextSettings.cs:1
StdCursor
Definition StdCursor.cs:4
Working with the "GetCursor" method