Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Child WIndow without MDI!!! 13

Status
Not open for further replies.

vb5prgrmr

Programmer
Jul 5, 2002
3,622
US

Q. How do I make Form2 a child of Form1?
A. Use MDI
Q. And if I don't want to use MDI?
A. In VB you can't unless you use a MDI Parent/Child.

OR

Q. How do I make a sub form like in access?
A. Use MDI
...

Well... that is not quite true...

To play with this example start a new standard exe project, you will see Form1 displayed by default. Add a command button and a picture box to form1 (Command1, Picture1) and add the following code...
[tt]
Option Explicit

Private Const HWND_TOP = 0
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOOWNERZORDER = &H200

Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long

Public IsForm2Visible As Boolean

Private Sub Form_Load()
Picture1.ScaleMode = vbPixels
End Sub

Private Sub Form_Resize()

Dim RetValue As Long

Command1.Left = 30
Command1.Top = 30

Picture1.Left = 30
Picture1.Top = 30 + (Command1.Height + Command1.Top)

Picture1.Width = Me.Width - 180
Picture1.Height = Me.Height - (450 + Picture1.Top)

If IsForm2Visible = True Then
RetValue = SetWindowPos(Form2.hwnd, HWND_TOP, 0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight, SWP_NOMOVE + SWP_NOOWNERZORDER)
End If

End Sub

Private Sub Command1_Click()

Dim RetValue As Long

Load Form2

Form2.ShowMeAsChild Picture1.hwnd

RetValue = SetWindowPos(Form2.hwnd, HWND_TOP, 0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight, SWP_NOMOVE + SWP_NOOWNERZORDER)

Form2.Show

End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

Dim F As Form

For Each F In Forms
Unload F
Next

End Sub
[/tt]

Now add a form (Form2). Change the following 2 properties of form2. Set the StartupPosition to Manual and optionally set the MaxButton to false (Optionally because when maximized the form will not resize correctly when the parent is resized.). Now add a command button to form2 (Command1) and add the following code to form2...
[tt]
Option Explicit

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

Private Sub Form_Load()
Command1.Left = 100
Command1.Top = 100
End Sub

Public Sub ShowMeAsChild(ParentHwnd As Long)

Dim RetValue As Long

RetValue = SetParent(Me.hwnd, ParentHwnd)

Form1.IsForm2Visible = True

End Sub

Private Sub Form_Resize()
Me.Caption = "Resized to " & Me.Height & "," & Me.Width
End Sub

Private Sub Command1_Click()
Unload Me
Form1.IsForm2Visible = False
End Sub
[/tt]

Now for continuous forms like in access I will leave that up to the user to play with.

The above has been tested on Win 95 B and Win2k Pro SP2

Have fun and Good Luck

 
Very nice vb5, always nice to learn something new! Star time.

You should consider placing this in the faq section.

Thanks and Good Luck!

zemp
 

Thanks zemp and whom ever also gave me a star. So you really think this should go into a FAQ?

 
really nice
 

Thanks WFadloun

 
That was really good !!!

Sunil
 
Its Fun its Cool, but its not usefull, :D

Use MDI :p

--
Luis MX
 
The setparent is useful in more than one place. The point is, we can contain anything (almost) inside anything (again almost).

If we have window handle for the menu, how interesting it will be to place textboxes on the menu !!!
 
Mr. luisfelipe007 you too are probably Fun and cool and like your post, not useful. Perhaps a useful contribution by you would serve a more nobel cause.
 

Thank you sunila7

luisfelipe007,

Just because you ... cannot see the usefulness ... does not mean that others ... will not see the advantages of code like this, As perhaps vbSun is mentioning.

Thank you vbSun.

Quite right to the point there NotSoVisual, thank you.

The point of the matter is that there is more than one way to accomplish what you want to do. It is up to you to decide which is the best way to achieve your goals.

 
Im agree with vbSun, also vb5prgrmr gave a good point.

When I tested that code I said, what is that for? its easier the use of MDI, all apollogies :)

Best Regards.

--
Luis MX
 
This is useful, and not just in the context displayed here. consider the tasks at hand when trying to create a docking/undocking control for forms(hell, even for other controls). Unfortunately, some peoples perception is only based on what they can 'see' and not on what they can envision.
 
Nicely done. Another something to tinker with. :)

--
Jonathan
 
VB5,
Nice Post... (I clicked you a star, but... I think your maxed Out ;-))
So here is another:
star.gif


VBSun,
Nice Point... (and a star 4 U)

VBDemiGod,
I Love That Quote (take a star too :p)
Unfortunately, some peoples perception is only based on what they can 'see' and not on what they can envision.

Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif

 
Very nice, really should be put in the FAQ's section. I also agree with the majority, this code has some great potential such as what vbsun] said...

 
Very nice, really should be put in the FAQ's section. I also agree with the majority, this code has some great potential such as what vbsun said...

 
I just figured out that the picture preview option in commondialog boxes can be implemented via the setparent.

Just to emphasize that setparent is more useful than we think.
 

Awww, Look ... My first cluster ... Thank you all ...

luisfelipe007,

Yes, there is more than one application of these API's possible here. I have actually used this reciently to contain another exe's window in a main application window (along with a lot more code for communication between the applications).

VBDemiGod,

I also see this as a great tool for control creation and I also like that quote. Thank you.

jonathanmb, Thank you

CubeE101 Thank you (They only show 5 but I belive they still add to the grand total so thank you once again.)

Niphyr, Thank you

vbSun, Hmmm... Time to play ... Thank you

Once again Thank You All...

Now the question is what to title the faq or should it be the title of this thread???

 
What a wonderfull use of setParent API as vbSun's picture preview in commondialog box.1*4u
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top