How to hide another active window with VB programming?

Posted on 2004-11-23
Last Modified: 2008-01-09
Hi all,

I've the following problem that need to solved as soos as possible. The scenario here:

I've developed a application using VB6. Each time when start the application, I need to check for an opened window then make it become hidden (same as Form.Hide property). For example, currently, I've 5 active windows (2 IEs, 1 cmd with some program running as server, winamp, and 1 VS6), how can I programming VB to make the cmd or IE hidden or just add it to system tray by codding within my program?

I already try the Shell_NotifyIconA API with the following code

================ my code===================
    cbSize As Long
    hWnd As Long
    uId As Long
    uFlags As Long
    uCallBackMessage As Long
    hIcon As Long
    szTip As String * 64
End Type
Private Const GW_HWNDNEXT = 2  '\2 = next window handle

Private Const NIM_ADD = &H0
Private Const NIM_MODIFY = &H1
Private Const NIM_DELETE = &H2

Private Declare Function GetWindowText Lib "user32" Alias _
"GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, _
ByVal cch As Long) As Long

Private Declare Function GetWindowTextLength Lib "user32" _
  Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long

Private Declare Function GetWindow Lib "user32" _
  (ByVal hWnd As Long, ByVal wCmd As Long) _
  As Long

Private Declare Function GetActiveWindow Lib "user32" _
  () As Long

'Declare the API function call.
Private Declare Function Shell_NotifyIcon Lib "shell32" _
    Alias "Shell_NotifyIconA" _
    (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean

'Dimension a variable as the user-defined data type.

Private Sub cmdHideIt_Click()
    'Call HideWindow(txtWindowTitle.Text, False)
    Call HideWindow("winamp", False)
End Sub

Public Function HideWindow(ByVal strCheckTask As String, _
   ByVal ExactMatch As Boolean) As Boolean

Dim CurrWnd As Long, Length As Long
Dim TaskName As String

HideWindow = False
CurrWnd = GetActiveWindow()
Do While (CurrWnd <> 0)
  Length = GetWindowTextLength(CurrWnd)
  TaskName = Space$(Length + 1)
  Length = GetWindowText(CurrWnd, TaskName, Length + 1)
  TaskName = Left$(TaskName, Len(TaskName) - 1)
  If (Length > 0) Then
    If ExactMatch = True Then
      If (TaskName = strCheckTask) Then
        HideWindow = True
        'If found the window then we hide it
        HideIt (CurrWnd)
        Exit Do
      End If
      If InStr(TaskName, strCheckTask) Then
        HideWindow = True
        'MsgBox CurrWnd
        'MsgBox frmMain.hWnd
        HideIt (CurrWnd)
        Exit Do
      End If
    End If
  End If
  CurrWnd = GetWindow(CurrWnd, GW_HWNDNEXT)
End Function

Public Sub HideIt(ByVal hWnd As Long)
    'Set the individual values of the NOTIFYICONDATA
    nid.cbSize = Len(nid)
    nid.hWnd = hWnd
    nid.uId = vbNull
    nid.uCallBackMessage = WM_MOUSEMOVE
    nid.hIcon = frmMain.Icon
    nid.szTip = "Hidden by LDLP" & vbNullChar
    'Call the Shell_NotifyIcon to add the icon to the taskbar
    'status area
    Shell_NotifyIcon NIM_ADD, nid
End Sub

When debug, the program found the handle of matching window but cannot hide it.

Anybody know the way to do that, please show me.

Thanks alot

.:: LDLP ::.
Question by:dungla
    LVL 19

    Accepted Solution

    The following code will determine the HWND of the first open IE window, and then ask the user if they want to hide or show the window.

    good luck


    Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
    Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Public Const HIDE_WINDOW = 0
    Public Const SHOW_WINDOW = 1


    Dim lngHwnd As Long
    Dim lngRet As Long
    lngHwnd = FindWindow("IEFrame", vbNullString)
    If (MsgBox("Hide or show Window, YES=HIDE, NO=SHOW", vbYesNo) = vbYes) Then
        lngRet = ShowWindow(lngHwnd, HIDE_WINDOW)
        lngRet = ShowWindow(lngHwnd, SHOW_WINDOW)
    End If
    LVL 13

    Author Comment

    Thanks for your code  BrianGEFF719. Well done.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
    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…
    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…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

    754 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

    20 Experts available now in Live!

    Get 1:1 Help Now