Solved

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

Posted on 2014-09-03
12
348 Views
Last Modified: 2014-09-09
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?
0
Comment
Question by:npaun
  • 6
  • 3
  • 3
12 Comments
 

Expert Comment

by:lohey
Comment Utility
How do you show the form? form1.show? Probably, you can try "form1.show vbmodal"?
0
 

Author Comment

by:npaun
Comment Utility
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
 

Expert Comment

by:lohey
Comment Utility
There is an API to let the form always on top. Let me find it out for you.
0
 

Expert Comment

by:lohey
Comment Utility
0
 

Author Comment

by:npaun
Comment Utility
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
 
LVL 17

Expert Comment

by:vb_elmar
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:npaun
Comment Utility
@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
 
LVL 17

Expert Comment

by:vb_elmar
Comment Utility
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
 

Author Comment

by:npaun
Comment Utility
@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
 
LVL 17

Assisted Solution

by:vb_elmar
vb_elmar earned 500 total points
Comment Utility
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
 

Accepted Solution

by:
npaun earned 0 total points
Comment Utility
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
 

Author Closing Comment

by:npaun
Comment Utility
I found a solution on the net that exactly solve my problem
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now