So what i'm doing is display the x and y values of the mouse pointer on a teechart chart using the following code, inside the onmousemove event:
oscilografia.Repaint;
if ((x>236) and (x<927)) and ((y>42) and (y<424)) then
begin
oscilografia.Canvas.Brush.Style := bsSolid;
oscilografia.Canvas.Pen.Color := clBlack;
oscilografia.Canvas.Brush.Color := clWhite;
oscilografia.Canvas.TextOut(x+10,y,datetimetostr(oscilografia.Series[0].XScreenToValue(x))+','+FormatFloat('#0.00',oscilografia.series[0].YScreenToValue(y)));
edit1.Text:=inttostr(x)+' '+inttostr(y);
end;
The code works fine, but a problem happens when i make another series visible by selecting it on the legend: the text inside the box created by canvas.textout isn´t shown anymore.
The box is still there following the mouse, but without any text. So i would like a solution to this.
The basic problem is down to how painting works. Windows do not have persistent drawing surfaces. What you paint onto a window will be overwritten the next time the system needs to repaint it.
You need to arrange that all painting is in response to WM_PAINT
messages. In Delphi terms that typically means that you would put your painting code in an overridden Paint
method.
So the basic process goes like this:
Paint
. Call the inherited Paint
method and then execute your code to display the desired text.OnMouseMove
event handler, if you detect that the mouse coordinates text needs to be updated, call Invalidate
on the chart.Invalidate
will mark that window as being dirty and when the next paint cycle occurs, your code in Paint
will be executed.Note, as an alternative to sub-classing, you can probably use the TChart
event OnAfterDraw
. But I'm not an expert on TChart
, so am not sure. The main points though are as I state above.