modal form

Posted on 2003-02-22
Medium Priority
Last Modified: 2010-05-01

How to show a system wide modal form?
I don't mean:

form1.show vbmodal

I mean a way to make a modal form on top of all the running applications on Window.

Take care.
Question by:tocos
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
  • 3
  • 2
LVL 33

Expert Comment

ID: 8001380
hi...u can use the 'SetTopmostWindow Me.hwnd' call to set the focus for the main form on top. the function
would use the 'setwindowpos' api to do that and it would look something like:

Private Declare Sub SetWindowPos Lib "User32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal
X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)

Sub SetTopmostWindow(ByVal hwnd As Long, Optional topmost As Boolean = True)
  Const HWND_TOPMOST = -1
  Const SWP_NOMOVE = &H2
  Const SWP_NOSIZE = &H1
  SetWindowPos hwnd, IIf(topmost, HWND_TOPMOST, HWND_NOTOPMOST), 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE
End Sub

Hope the above solution is of help


Author Comment

ID: 8001398
Hello hongjun,

Thank you for your fast response ..
What you gave here .. is a topmost for over all the application .. but is not modal .. as you still can access all the running applications.

I needed this form as a modal form that will disable accessing of all the running application.

LVL 33

Expert Comment

ID: 8001427
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Author Comment

ID: 8001531
didn't work out for me ..
It only disable the affected form but I'm still accessing any of the running applications.

Accepted Solution

SilentRage earned 150 total points
ID: 8003690
A long time ago I did a lot of research on this.  I found that the best way to sieze control of the machine, is by hiding all the visible windows.  Another very good way is to have your window on top and covering the whole screen, but maybe you prefer how this looks instead:

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal sClassName As String, ByVal sWindowTitle As String) As Long
Private Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Const SW_HIDE = 0
Private Const SW_SHOW = 5
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Const GW_HWNDNEXT = 2

Dim HiddenWindows As New Collection
Private Sub Form_Load()
Dim hParent As Long
    hParent = FindWindow(vbNullString, vbNullString)
    While hParent <> 0
        If IsWindowVisible(hParent) Then
            ShowWindow hParent, SW_HIDE
            HiddenWindows.Add hParent
            Debug.Print hParent
        End If
        hParent = GetWindow(hParent, GW_HWNDNEXT)
    Me.Print "Close this form to bring everything back"
End Sub
Private Sub Form_Unload(Cancel As Integer)
    For Each Window In HiddenWindows
        ShowWindow Window, SW_SHOW
End Sub

Try the code above and see how you like it.  Make sure to only run the compiled exe though.  It seems that from the environment, sometimes a window may be missed.  dunno why.

The only way a user may do anything besides interact with your form, is by pressing the Windows key, and doing CTRL+ALT+DEL.  CTRL+ALT+DEL can be disabled under Win9x, but it's not recommended.

Author Comment

ID: 8006732
Hello SilentRage,

I think this scenario is the best I can get .. thank you


Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

801 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