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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic forms and setting owners 1

Status
Not open for further replies.

instar4per

Programmer
Jan 21, 2002
48
US
Whee, fun stuff here.

After pawing through many samples of "SetParent", I decided to ask on the forums.

Here is my application:

I have a "control program", who's sole purpose is to load other mini-programs. The mini-programs (.DLL files), consist of a class module, and a form. The control program uses a CreateObject command to load the DLL, then a few CallByName's to execute methods within the DLL to return the hWnd of the form.

I then want this form contained within a picturebox control (or any container control) on my control program.

If this didn't make any sense, that's because I've been up too long trying to figure this out. Please let me have some feedback on what you think may help me here. If you need more information, please feel free to contact me however you can.

Thanks!
-iNSTA
 
VB does not support sub forms like Access. There is no way to stick a form into a container control. Forms are more than a collection of controls.

The closest you can get is create your forms as ActiveX Documents. then you can load these ActiveX Documents into a OLE Container Control.
 
>There is no way to stick a form into a container control

Au contraire.

Create a new project with two forms. On the first place a picturebox and a command button, and drop in the following code:
[tt]
Option Explicit
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Const GWL_HWNDPARENT = (-8)
Private Const GWL_STYLE = (-16)
Private Const WS_CHILD = &H40000000


Private Sub Command1_Click()
SetParent Form2.hwnd, Picture1.hwnd
SetWindowLong Form2.hwnd, GWL_STYLE, GetWindowLong(Form2.hwnd, GWL_STYLE) Or WS_CHILD
Form2.Move 0, 0
Form2.Show
End Sub
[/tt]
On the second form place a command button, and drop in the following code:
[tt]
Option Explicit

' Used to demonstrate that hosted form is fully functional
Private Sub Command1_Click()
MsgBox "Hello"
End Sub
[/tt]
Voila. One functional form happily contained in a picturebox.

(Advice: in a real-world application you should SetParent the child window back to it's original parent and, preferably, clear the WS_CHILD style before closing/unloading the form hosting the container)
 
Worked beautifully, even when stepping through the code!

I would like to thank you for this code, it has ended many hours of needless experimentation with no results.

Thank you so much,
-iNSTA
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top