Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Create a owner form with API functions

Total Hit ( 3067)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Starting with VB5, the Show method supports a second optional argument, that lets you specify which form owns the form that is about to be displayed:

Click here to copy the following block
Form2.Show , Me

A owned form is always displayed in front of its owner, and when the owner is unloaded, VB automatically unloads the owned form as well. The second argument is mostly used only when the form is being displayed not modally, because modal forms are always displayed in front of their owners.
The problem with this approach, however, is that it can't work with forms contained in ActiveX DLLs and EXEs, because form references don't work across project boundaries. The answer is the following API function:

Click here to copy the following block
Private Declare Function SetWindowLong& Lib "user32" Alias "SetWindowLongA" _
  (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long)
Const GWL_HWNDPARENT = (-8)

Function SetOwner(ByVal HwndtoUse, ByVal HwndofOwner) As Long
  SetOwner = SetWindowLong(HwndtoUse, GWL_HWNDPARENT, HwndofOwner)
End Function

The SetOwner function returns the handle of the previous owner of the window, so you must restore it before exiting. In the following example this function is used with a form included in the current project, but you can easily extend it to work with forms from other applications. The only thing to do is letting the external application have the hWnd of the form in the main application that must become the owner window:


Click here to copy the following block
Dim frm As Form2
Dim oldOwner As Long

Private Sub cmdShowForm_Click()
  ' show the form
  Set frm = Form2
  frm.Show
  ' make it owned by the current form
  oldOwner = SetOwner(frm.hwnd, Me.hwnd)
End Sub

Private Sub cmdUnloadForm_Click()
  ' restore original owner
  SetOwner frm.hwnd, oldOwner
  ' unload the form
  Unload frm
End Sub


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.