How to make a form modal for an external application window?

I wish to make my Form1 modal in respect to another application, for instance Notepad, providing that I have that application’s window handle. So, as for modal forms, access to the application is blocked until the modal Form1 is closed. How to do that, using API functions?
npaunAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

loheyCommented:
How do you show the form? form1.show? Probably, you can try "form1.show vbmodal"?
0
npaunAuthor Commented:
I doesn't matter: form1.show vbModal will make Form1 modal only for MY application that have called it, not for the external application as I need it...
0
loheyCommented:
There is an API to let the form always on top. Let me find it out for you.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

loheyCommented:
0
npaunAuthor Commented:
that is a method to set a from as topmost, not to make it modal. Modal form means to be in front of an application, and to prevent any other interaction with that application until the modal form is closed.
0
vb_elmarCommented:
You can use the Setparent function. Description: The SetParent function changes the parent window of the specified child window.

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function Putfocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
Const GW_HWNDNEXT = 2
Dim mWnd As Long
Function InstanceToWnd(ByVal target_pid As Long) As Long
    Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long
    'Find the first window
    test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
    Do While test_hwnd <> 0
        'Check if the window isn't a child
        If GetParent(test_hwnd) = 0 Then
            'Get the window's thread
            test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)
            If test_pid = target_pid Then
                InstanceToWnd = test_hwnd
                Exit Do
            End If
        End If
        'retrieve the next window
        test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
    Loop
End Function
Private Sub Form_Load()
    'KPD-Team 1999
    'URL: http://www.allapi.net/
    'E-Mail: KPDTeam@Allapi.net
    Dim Pid As Long
    'Lock the window update
    LockWindowUpdate GetDesktopWindow
    'Execute notepad.Exe
    Pid = Shell("c:\windows\notepad.exe", vbNormalFocus)
    If Pid = 0 Then MsgBox "Error starting the app"
    'retrieve the handle of the window
    mWnd = InstanceToWnd(Pid)
    'Set the notepad's parent
    SetParent mWnd, Me.hwnd
    'Put the focus on notepad
    Putfocus mWnd
    'Unlock windowupdate
    LockWindowUpdate False
End Sub
Private Sub Form_Unload(Cancel As Integer)
    'Unload notepad
    DestroyWindow mWnd
    'End this program
    TerminateProcess GetCurrentProcess, 0
End Sub
0
npaunAuthor Commented:
@vb_elmar, thanks for post
SetParent, at least in the given example (which I already found and checked before, and now checked again) does not set my Form1 as modal form for Notepad; what is does is that it encapsulates Notepad window inside of the Form1 client area. That is not what I need, I need to set Form1 as modal form for e.g. Notepad. If it actually can be done by SetParent, I don't have example for that...
0
vb_elmarCommented:
It also works reversal. I replaced the Form1 hwnd with the notepad hwnd. That worked:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Const GW_HWNDNEXT = 2
Dim mWnd As Long

Function InstanceToWnd(ByVal target_pid As Long) As Long
    Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long
    'Find the first window
    test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
    Do While test_hwnd <> 0
        'Check if the window isn't a child
        If GetParent(test_hwnd) = 0 Then
            'Get the window's thread
            test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)
            If test_pid = target_pid Then
                InstanceToWnd = test_hwnd
                Exit Do
            End If
        End If
        'retrieve the next window
        test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
    Loop
End Function

Private Sub Form_Load()
Timer1.Interval = 2000
    Dim Pid As Long
    'Execute notepad.Exe
    Pid = Shell("c:\windows\notepad.exe", vbNormalFocus)
    If Pid = 0 Then MsgBox "Error starting the app"
    'retrieve the handle of the window
    mWnd = InstanceToWnd(Pid)
    'Set notepad as parentwindow
    SetParent Me.hwnd, mWnd
End Sub

Private Sub Timer1_Timer()
    If WindowState = 0 Then Move 0, 0, 150 * 15, 100 * 15
    Form1.Show
    Form1.Refresh
    Beep
End Sub

Open in new window

0
npaunAuthor Commented:
@vb_elmar
in this example, if I haven't done something wrong, the From1 is now encapsulated inside the Notepad window, but I don't see how it can be used for my problem to make Form1 modal for Notepad (or any other external application specified by its window handle)... and by Modal, I mean in the same sense as in VB6 Form1.show vbModal, but to be modal in respect to that external application and not my VB project...
0
vb_elmarCommented:
Use my 1st code sample where notepad is encapsulated inside Form1. Then add a Form2 to the VB project. Use the command Form2.show vbModal. Then Form2  is modal in respect to Form1 with the Notepad application in it. I guess that's the thing you wanted.
0
npaunAuthor Commented:
hm, I see your point, interesting…
Well, technically, the Form2 now is modal in respect to the Notepad… but at expense of Notepad being encapsulated inside of Form1… unfortunately, this is not acceptable for my purposes, for various reasons…

However, looking further how to solve this problem, I’ve just stumbled to this link
http://allapi.mentalis.org/vbexamples/vbexample.php?vbexample=DSMODAL&category=MISC
which works exactly as what I need. The only minor drawback is that it uses a small external dll which is not a problem, although it would be better If would have the entire code needed for it at my control… I checked this dll binary, and it seems it is heavily based on hooking…

Anyway, thank you for the help!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
npaunAuthor Commented:
I found a solution on the net that exactly solve my problem
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.