Create a owner form with API functions

Create a owner form with API functions

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:

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:

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:

Dim frm As Form2Dim oldOwner As LongPrivate 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 SubPrivate Sub cmdUnloadForm_Click()    ' restore original owner    SetOwner frm.hwnd, oldOwner    ' unload the form    Unload frmEnd Sub

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as