How to get top/left of parent window in component while it is being moved?

I just want to display, in a Label, the current top/left of the parent window as it is moved around on the screen, to help me with the initial placement of windows. I can’t quite figure out where to go to get that. Thanks!

getPosition(); // Returns the component's top-left position as a Point.

That doesn’t do it, I don’t want the component’s top/left, which is 0,0. I want the window’s screen position top/left.

getScreenPosition() does it.

Now I just need to figure out what method gets call while a window is being moved around…

Edit: worked it out:
Component::moved() (in the parent window)

Thanks for bearing with my noob questions. :wink:

1 Like

getScreenPosition() returns you screen co-ordinates. Is that what you want? If you want it relative to the parent:

getBounds(); // The rectangle returned is relative to the top-left of the component's parent.
// and so..

Thanks. But I do want screen coordinates, so I can see what the top/left current position of the owning window is, relative to the screen.

Just for reference, in case anyone else finds this useful someday, I wanted to put a Label in the lower right corner of each of my windows that, for development purposes, showed me the window’s position and size, like this:

Add a label in the window’s component:

Label positionLabel;

Add a function in the window’s component:

void myWindowComponent::updatePositionLabel()
    auto pt = getScreenPosition();
    positionLabel.setText(String (pt.x) + ", " + String (pt.y)+ " - " +
                            String (getWidth()) + " x " + String (getHeight()), dontSendNotification);

In the myWindowComponent’s resized() function:


Override the owning Window’s moved() function:

void myWindow::moved()
		// if you’ve got a reference to the window’s component:

		// or:
    dynamic_cast<MyWindowComponent *>(getContentComponent())->updatePositionLabel();

Moving or resizing the window changes the displayed coordinates and/or dimensions.

1 Like