How can I get cursor position on the form? - delphi

I need to get cursor position on the form, how would I do that?

Pass Mouse.CursorPos to TForm.ScreenToClient().
Mouse.CursorPos is returned in the screen coordinate system. TForm.ScreenToClient() converts points into the form's client coordinate system.
In fact all TControl descendants offer the ScreenToClient() method, and its inverse, ClientToScreen(), to convert between coordinate systems.

Related

Get HTML element by position

I am using TWebBrowser in Delphi.
I need help to get an HTML element by its position.
The element I need is in a frame. Using elementFromPoint():
Document.elementFromPoint(X, Y)
I am getting the frame itself, but not what is inside of it.
I tried to get it using this:
WebBrowser.OleObject.Document.Frames
But this gives me an access error when transferring frames.
Using Document.elementFromPoint() is the correct approach. What you are not taking into account is that the HTML is parsed in a hierarchical DOM tree and elementFromPoint() is not recursive.
You are asking the browser's top-level Document to find an immediate child element at a given X,Y coordinate within the Document. In this case, that is a frame element.
A frame is an embedded window that holds another Document. You need to access the frame's Document and ask it to find a child element at the target X,Y coordinate within the frame. And so on, and so on, until you finally reach the bottom-most child.
Note that elementFromPoint() takes client coordinates that are relative to the top-left corner of the Document you are calling elementFromPoint() on. So, when you want to search a child frame's Document, you need to first subtract the frame's own top-left X,Y coordinate (within its parent Document) from the target X,Y coordinate before calling elementFromPoint() on the frame's Document.

Series Lines and EndPoints with TeeChart

Having a ChartSeries, I would like display a certain text on a label moving the mouse above the ChartValues. But only the line is active for me and not the EndPoint (definitely the Value…)
I would like if the EndPoint (a Circle, actually) would be active instead of the line and moving the mouse above this circle, my text can be appeared. Thanks.
You could try the MarkTips tool.
If you have a TLineSeries with Pointer.Visible=true, the mark tips tool will appear when you have the mouse over the line and over the pointer. If you only want it when you have the mouse over the series pointer, you could have a TLineSeries with Pointer.Visible=false and also a TPointSeries with the same data, and the MarkTips tool assigned to the TPointSeries.

Drawing rectangle with canvas

With this code I want to draw a rectangle:
procedure TForm1.Button1Click(Sender: TObject);
var rectangle:Trect;
begin
fx:=400;
fy:=400;
sc1:=base/fx;
sc2:=altezza/fy;
sc:=max(sc1, sc2);
lx:=fx*sc;
ly:=fy*sc;
xc:=base/2;
yc:=altezza/2;
x1:=xc-(lx/2); x2:=xc+(lx/2); y1:=yc-(ly/2); y2:=yc+(ly/2);
panel1.Repaint;
panel1.Canvas.Brush.color:= clblack;
panel1.Canvas.line((panel1.width div 2),0,(panel1.Width div 2), panel1.Height);
panel1.Canvas.line(0,(panel1.height div 2), panel1.Width,(panel1.Height div 2));
panel1.canvas.brush.style:=bsclear;
Rectangle:=rect(x1, y1, x2, y2);
end;
But there is a problem because I have to use only integer values.
Is it possible to use real values for drawing a rectangle with TCanvas?
The simple answer is no. Graphic devices as represented by TCanvas use a coordinate system with integral coordinates. If your coordinates are real values then you need to use some form of mapping between your coordinate system and the integral device coordinates.
However, in this instance it looks like it's not that complex. You don't need real valued coordinates per se. You only have real values because you used real division. Perhaps all you need to do is use integer division, div, rather than real division. Or perhaps you would prefer Round.
A bigger problem is that your code is in the wrong place. You cannot paint in a button handler. Windows will not remember what you painted. The next time the window is invalidated it will ask the panel to refresh itself, and your rectangle will be gone. Painting code needs to be inside an overriden Paint method or equivalent. Perhaps you need a paint box control.

Delphi, TTreeView: how to get the screen coordinates of the given node and its icon?

Please help me to get the screen coordinates of the state icon rectangle of the given TTreeNode in a TreeView (I mean the icons specified in the TTreeView.StateImages property).
There is a TTreeView.GetHitTestInfoAt(X, Y: integer): : THitTests function, but that is not quite what I'm looking for; it says whether the given coordinates correspond to the label, or to the icon, or to the state icon of the item, but I need to know what part of the icon was clicked.
(The reason is that I want to implement the TreeView nodes with two checkboxes for each item, and I use StateImages to simulate the checkboxes (one state is a checked item, the other state is an unchecked item). As I understand, to know which one of the checkboxes is clicked I need to compare the cursor coordinates with the state icon coordinates. How can I get them?)
You can send the control a tvm_GetItemRect message which will tell you the client coordinates of the item's bounding box. Use that and what you know of the relative positions of the label text and the icons to determine where the mouse was clicked in the icon.
Instead of GetHitTestInfoAt, you might prefer to send a tvm_HitTest message instead since it will give you the hit-test information and an item handle at once; a handle is what tvm_GetItemRect requires.
You don't need the screen coordinates since all the coordinates involved so far are client coordinates, but you can call ClientToScreen if you really want screen coordinates.

Delphi RichEdit, get y-pixel start of an arbitrary line

I have a richedit containing lines using different fonts, styles, languages etc.
I am drawing in a gutter. I would like to start my drawing at the same y pixel position as the corresponding line.
Send the control an em_PosFromChar message. It returns the client coordinates of the character at the given index, although the documentation doesn't say what the coordinates represent (upper left corner, baseline center, or what). You're looking for the character's baseline.
Use em_LineIndex to get a character index for a given line number, if you don't already know the index of a character you're interested in.

Resources