How to draw a window at Mouse coordination in OnMouseMove
Created at 27 Feb 2021, 13:39
How to draw a window at Mouse coordination in OnMouseMove
27 Feb 2021, 13:39
Hi there,
I try to draw a small window similar to Market Snapshot window when user select the cursor. I'd like to my custom data. However, when I draw, the window is drawn a bit off from my mouse pointer. See the sample.
The blue dot is my mouse pointer.
Here is my code. The DataForm is just a plain WinForm.
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
namespace cAlgo
{
[Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
public class CustomData : Indicator
{
[Parameter(DefaultValue = 0.0)]
public double Parameter { get; set; }
[Output("Main")]
public IndicatorDataSeries Result { get; set; }
protected DataForm DataTip;
protected override void Initialize()
{
Chart.MouseMove += OnMouseMove;
Chart.MouseLeave += OnMouseLeave;
DataTip = new DataForm();
DataTip.Show();
DataTip.Visible = false;
}
public override void Calculate(int index)
{
// Calculate value at specified index
// Result[index] = ...
}
protected void OnMouseMove(ChartMouseEventArgs obj)
{
Chart chart = obj.Chart;
if (chart != null)
{
if (!DataTip.Visible)
{
DataTip.Visible = true;
DataTip.Show();
DataTip.TopMost = true;
}
var x = (int)Math.Round(obj.MouseX, 0);
var y = (int)Math.Round(obj.MouseY, 0);
DataTip.Location = new System.Drawing.Point(x, y);
Print("mouse X {0} mouse Y {1}", obj.MouseX, obj.MouseY);
}
}
protected void OnMouseLeave(ChartMouseEventArgs obj)
{
if (obj.Chart != null)
{
DataTip.Visible = false;
}
}
}
}
Thanks in advance
Noppanon
Replies
... Deleted by UFO ...
amusleh
01 Mar 2021, 11:29 ( Updated at: 01 Mar 2021, 11:30 )
The issue with your code is that you are using mouse position on chart not screen, the window form location (x,y) is based on your screen coordinates not chart.
To solve the issue you have two option, either use the chart controls or use Windows API to get the location of mouse on your screen.
Here is an example of how you can show a tooltip with chart controls:
If you want to use a Windows form then you have to use the Windows API to get the mouse location on screen.
For using Windows API you can use my GlobalHook library:
Example:
If you use a windows form as a tooltip and move it alongside mouse cursor it will use too much resource so I recommend you to use chart controls instead.
@amusleh