Solved

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

Posted on 2014-09-03
12
405 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 3
12 Comments
 

Expert Comment

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

Author Comment

by:npaun
ID: 40301041
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
ID: 40301057
There is an API to let the form always on top. Let me find it out for you.
0
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 

Expert Comment

by:lohey
ID: 40301059
0
 

Author Comment

by:npaun
ID: 40301097
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
ID: 40301141
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
 

Author Comment

by:npaun
ID: 40301188
@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
ID: 40301311
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
ID: 40303497
@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
ID: 40303748
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
ID: 40304821
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
ID: 40311612
I found a solution on the net that exactly solve my problem
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Six Sigma Control Plans

728 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