• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 243
  • Last Modified:

How to hide another active window with VB programming?

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 ::.
1 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
dunglaAuthor Commented:
Thanks for your code  BrianGEFF719. Well done.

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now