How to get the 'absolute' position of a widget? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
HansLjy
Earned a small fee
Earned a small fee
Posts: 19
Joined: Fri May 21, 2021 6:44 am

How to get the 'absolute' position of a widget?

Post by HansLjy »

I use panels and boxsizers to manage the layout of my app, at the same time I want to draw a bitmap on the panels. It seems dc.drawbitmap use 'absolute' position, that is, the position relative to the whole window. Also I notice that the GetPosition function returns a position relative to the parent widget. As a result, I can't draw a bitmap relative to the panel. Is there any solution to this?
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: How to get the 'absolute' position of a widget?

Post by PB »

What do you mean? If you want to draw on a window, you draw on its dc with coordinates relative to the upper left corner, with window and DC coordinates generally matching each other.

If you want to draw on other windows (generally a very bad idea), you can easily convert the coordinates between parent/child/sibling windows.
HansLjy
Earned a small fee
Earned a small fee
Posts: 19
Joined: Fri May 21, 2021 6:44 am

Re: How to get the 'absolute' position of a widget?

Post by HansLjy »

PB wrote: Sat Jun 05, 2021 7:23 am What do you mean? If you want to draw on a window, you draw on its dc with coordinates relative to the upper left corner, with window and DC coordinates generally matching each other.

If you want to draw on other windows (generally a very bad idea), you can easily convert the coordinates between parent/child/sibling windows.
Thanks for the reply. What I really want is to draw an image in a window with position relative to a certain panel in this window, rather than the window itself.

Actually I have something like a 'image panel', I want to use it like a panel so that I could use boxsizers to manage the layout. At the same time I want to draw image in this panel, the image should be positioned strictly in the panel.
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: How to get the 'absolute' position of a widget?

Post by PB »

HansLjy wrote: Sat Jun 05, 2021 7:38 am [At the same time I want to draw image in this panel, the image should be positioned strictly in the panel.
Why not draw using this panel?

If you have a wxFrame and want to draw over its child wxPanel (once again, a bad idea), I think it would conflict with the child drawing (and would generally be drawn under the child). But in theory, the DC coordinates of the parent should still match those of the child window. E.g., if the child window is at (100,100) returned by its GetPosition(), the parent DC coordinates drawing should match it?
HansLjy
Earned a small fee
Earned a small fee
Posts: 19
Joined: Fri May 21, 2021 6:44 am

Re: How to get the 'absolute' position of a widget?

Post by HansLjy »

PB wrote: Sat Jun 05, 2021 7:44 am
HansLjy wrote: Sat Jun 05, 2021 7:38 am [At the same time I want to draw image in this panel, the image should be positioned strictly in the panel.
Why not draw using this panel?

If you have a wxFrame and want to draw over its child wxPanel (once again, a bad idea), I think it would conflict with the child drawing (and would generally be drawn under the child). But in theory, the DC coordinates of the parent should still match those of the child window. E.g., if the child window is at (100,100) returned by its GetPosition(), the parent DC coordinates drawing should match it?
My appology. After some experiments I found out that dc.drawbitmap use a coordinate relative to the current panel (namely, I can draw 'using this panel'). I have taken for granted that the dc use absolute position since all my early experiments are done with a single panel covering the whole window. This is indeed a stupid question to ask, lol.
Post Reply