Link to home
Start Free TrialLog in
Avatar of rustymoling
rustymoling

asked on

(C#) How can I find the screen location of a treeview item?

The location changes, because the treeview is scrolling. I am drawing an additional bitmap next to a highlighted item. If I could find the coordinates of a given item, I'd have no problem drawing that bitmap in the required location.

Thanks anyone who can help!
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

If you are using winforms, check mouse move event:
http://msdn.microsoft.com/en-us/library/system.windows.uielement.mousemove(v=VS.85).aspx

private void MouseMoveHandler(object sender, MouseEventArgs e)
{
    // Get the x and y coordinates of the mouse pointer.
    Point position = e.GetPosition(this);
    double pX = position.X;
    double pY = position.Y;

    // Asuming your treeview width is 100 and scrollbar width is 10.
    position.Y = 100-2*10;//position.Y;
 // Convert to a point in the TreeView's coordinate system. (these are the coordinates you need)
            position  = treeview1.PointToClient(position );
            // Get the node underneath the mouse if required to check if piicking the right node.
            TreeNode node = treeview1.GetNodeAt(pt);
}

Avatar of rustymoling
rustymoling

ASKER

Well, you are getting coordinates from the event args of a Mouse Move event - and I don't want to use that. Things are changing in my tree view as a result of a background process. And there are more things in the treeview than can fit on the screen, so the scrollbar is active. And users minimize and maximize this window, which sometimes strangely alters what list of items is visible on the screen.

To me, it seems simplest to locate which treenode is the currently relevant one, and get hold of its current screen position, but I haven't had any luck with that - I don't know which properties it might have to help me with that, and I don't know how to cast it to anything that might have the properties I'm looking for.
ASKER CERTIFIED SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Exactly what I needed, thank you.