Replacement for SetWindowPos() 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
Mug
Earned a small fee
Earned a small fee
Posts: 15
Joined: Tue Feb 19, 2013 4:08 pm

Replacement for SetWindowPos()

Post by Mug » Thu Mar 07, 2013 3:13 pm

I'm trying to find a wxWidgets replacement for SetWindowPos() given a HWND but haven't found anything suitable. I don't want to create a new window!

This is the original code:

Code: Select all

::SetWindowPos(hWND, 0, newX, newY, newWidth, newHeight, flags);
And this is the first step I'm trying to achieve

Code: Select all

wxWindow* pWnd; // How can I use this without the 'new'?
pWnd->SetHWND((WXHWND) hWND);
pWnd->SetSize(newX, newY, newWidth, newHeight);
So, how can a get a wxWindow* from the HWND?
Thanks

Radek
Super wx Problem Solver
Super wx Problem Solver
Posts: 286
Joined: Sun Sep 11, 2011 7:17 am

Re: Replacement for SetWindowPos()

Post by Radek » Thu Mar 07, 2013 3:26 pm

You cannot do such things.

(1) You can get a pointer to a wxWindow only for windows managed by wxWidgets. If the window is created otherwise, you cannot get a wxWindow pointer to it even if the window does have a HWND of some kind.
(2) You cannot call a method for an unallocated pointer. The pointer needs to point to something first and only then you can use it.

User avatar
doublemax
Moderator
Moderator
Posts: 15184
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Replacement for SetWindowPos()

Post by doublemax » Thu Mar 07, 2013 3:27 pm

As this is Windows-specific anyway, why don't you just use SetWindowPos()? Without knowing the background behind this question, it's hard to give a proper answer.
Use the source, Luke!

Mug
Earned a small fee
Earned a small fee
Posts: 15
Joined: Tue Feb 19, 2013 4:08 pm

Re: Replacement for SetWindowPos()

Post by Mug » Thu Mar 07, 2013 4:25 pm

Well, thanks for the replies. Doesn't sound good...

I'm trying to convert code, that wasn't written by me, to wxWidgets to run on Windows/Linux/Mac.
Almost all the code is based on mapped HWNDs so I was hoping to get the wxWindow* from these HWNDs.

The original windows are, indeed, wxWindows and the HWNDs are obtained from these windows. I've already modified this part of the code from ... = pWnd->m_hWnd to ... = pWnd->GetHWND()
Now I have a whole bunch of wxWindow HWNDs and want to convert them back to the original wxWindow*

Surely this is possible?

User avatar
doublemax
Moderator
Moderator
Posts: 15184
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Replacement for SetWindowPos()

Post by doublemax » Thu Mar 07, 2013 4:37 pm

If your program also needs to run on Linux and Mac, you'll have to redesign it. Get rid of all the HWND stuff and use wxWindow (and related classes) everywhere.

Porting non-wxWidgets gui code to wxWidgets code can't be done on a line-by-line basis. You need to approach this task at a higher level.
Use the source, Luke!

Radek
Super wx Problem Solver
Super wx Problem Solver
Posts: 286
Joined: Sun Sep 11, 2011 7:17 am

Re: Replacement for SetWindowPos()

Post by Radek » Thu Mar 07, 2013 4:58 pm

If I understand well then the original code was wxWidgets but it was un-wxWidgets-ed by using HWNDs for some reason. The way back is rather complicated because you have bags of numbers (HWNDs) which do belong to some objects but you cannot tell which objects they belong to. You would need to make some kind of database (a list of pairs: HWND - pointer) and having a HWND, you would need to search your list for the corresponding pointer. The ctors of the windows could place their pointers and HWNDs into the database and the dtors can remove the corresponding data from it.

Perhaps, a simpler method would be to forget HWNDs at all and replace "HWND" by "pointer to wxWindow" everywhere in the code. The size of HWND and a pointer is the same so that sizes wouldn't make problem. If you really need the HWND, you can use GetHandle().

Mug
Earned a small fee
Earned a small fee
Posts: 15
Joined: Tue Feb 19, 2013 4:08 pm

Re: Replacement for SetWindowPos()

Post by Mug » Fri Mar 08, 2013 7:29 am

Radek wrote:If I understand well then the original code was wxWidgets but it was un-wxWidgets-ed by using HWNDs for some reason. The way back is rather complicated because you have bags of numbers (HWNDs) which do belong to some objects but you cannot tell which objects they belong to. You would need to make some kind of database (a list of pairs: HWND - pointer) and having a HWND, you would need to search your list for the corresponding pointer. The ctors of the windows could place their pointers and HWNDs into the database and the dtors can remove the corresponding data from it.

Perhaps, a simpler method would be to forget HWNDs at all and replace "HWND" by "pointer to wxWindow" everywhere in the code. The size of HWND and a pointer is the same so that sizes wouldn't make problem. If you really need the HWND, you can use GetHandle().
You are most probably right. Even though I don't understand the code I'll try replacing all the HWNDs by "pointers to the wxWindow". Like you say, I'm sure this would be the cleanest method. Thanks for the suggestion. Now I wonder why I didn't think of it myself. :wink:

Post Reply