4

What is the Win32 equivalant of Form2.Show vbModeless, Form1 that allows me to set the owner (not the parent) to an arbitrary hWnd and set the default position, etc?
As far as I'm aware, this can only be set when creating a window.

Deanna
  • 23,876
  • 7
  • 71
  • 156
  • I've got around this for now by positioning it myself before I call `.Show` (Taking multiple monitors into account of course) – Deanna Aug 04 '11 at 13:17
  • Apart from CreateWindow[Ex], you can use `SetWindowLong[Ptr]` with `GWL_HWNDPARENT` as 'nIndex' to set the owner window. – Sertac Akyuz Aug 04 '11 at 17:18
  • Found this article on DevX. It's your question almost verbatim. http://www.devx.com/vb2themax/Tip/18318 – jveazey Aug 05 '11 at 03:36
  • I've not looked at DevX in years, I didn't realise it was still around :) That does exactly what I wanted, but has a quirk with multiple monitors (exactly what I wanted this code to fix) so I'll stick with the custom code to get the monitor and center it. I may adjust later because of the always on top ability though. Thanks – Deanna Aug 05 '11 at 08:01
  • FYI, in VB6 unloading an owner form unloads all "owned by it" ones too which is a major difference with just API owned hWnd's. – wqw Aug 06 '11 at 10:11

1 Answers1

3

I'm not sure what you are trying to accomplish, but I think you might be looking for the SetParent function.

Declare Function SetParent Lib "user32.dll" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long

hWndChild is the handle to the child window.
hWndNewParent is the handle to the new parent. Pass null (0 if I remember correctly) to make the desktop the owner.

You can find more information about it here.

MarkJ
  • 30,070
  • 5
  • 68
  • 111
jac
  • 9,666
  • 2
  • 34
  • 63
  • No, I was trying to set the owner (disabled when modal, some focus, etc) and NOT the parent (embedded in another window) Thanks though. – Deanna Aug 04 '11 at 17:05
  • Yes, this is correct for toplevel windows. SetParent() is recommended over SetWindowLongPtr() to set the owner window. – Hans Passant Aug 04 '11 at 17:29
  • @Hans - You're wrong, you can't set the owner window with SetParent, even a top-level window will be parented in the 'hWndNewParent' window. – Sertac Akyuz Aug 04 '11 at 18:11
  • @Sertac - no, SetParent() doesn't set the WS_CHILD style bit on a toplevel window. Check the MSDN docs. Also note the use of the *hwndParent* argument of CreateWindowEx() to create an owned window. The GWL_HWNDPARENT value for SetWindowLong/Ptr() has been removed from the documentation. – Hans Passant Aug 04 '11 at 18:45
  • @Hans - What MSDN means about the WS_CHILD flag is, "the API is setting the parent but you take care of your style yourself". There's no mention about ownership in SetParent's documentation. In fact the only documentation I can find about the subject is *"After creating an owned window, an application cannot transfer ownership of the window to another window."* in [Window Features](http://msdn.microsoft.com/en-us/library/ms632599%28v=vs.85%29.aspx). GWL_HWNDPARENT might have been removed from the docs, but currently it is the only thing that works after a window has been created. – Sertac Akyuz Aug 04 '11 at 19:19
  • With all due respect, why don't you do a quick test. – Sertac Akyuz Aug 04 '11 at 19:21
  • @Hans - I showed the Declared parameters as long because the other tag is VB6 which does not have an IntPtr type. – jac Aug 04 '11 at 22:12