Link to home
Start Free TrialLog in
Avatar of dungla
dunglaFlag for Viet Nam

asked on

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===================
Private Type NOTIFYICONDATA
    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.
Dim nid As NOTIFYICONDATA

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
    Else
      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)
  DoEvents
Loop
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.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
    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 ::.
ASKER CERTIFIED SOLUTION
Avatar of BrianGEFF719
BrianGEFF719
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dungla

ASKER

Thanks for your code  BrianGEFF719. Well done.