Attribute VB_Name = "WindowHandle"
Option Explicit

Public Enum RemoveMenuEnum
    rmMove = 1
    rmSize = 2
    rmMinimize = 3
    rmMaximize = 4
    rmClose = 6
End Enum

Public Declare Function GetActiveWindow Lib "User32" () As Long

Public Type Size
        cx As Long
        cy As Long
End Type
Public Declare Function GetCurrentObject Lib "gdi32" (ByVal hdc As Long, ByVal uObjectType As Long) As Long
Public Declare Function GetCurrentDirectory Lib "kernel32" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Public Declare Function GetCurrentProcess Lib "kernel32" () As Long
Public Declare Function GetCurrentProcessId Lib "kernel32" () As Long
Public Declare Function GetCurrentThread Lib "kernel32" () As Long
Public Declare Function GetCurrentThreadId Lib "kernel32" () As Long
Public Declare Function GetDC Lib "User32" (ByVal Hwnd As Long) As Long
Public Declare Function GetDCOrgEx Lib "gdi32" (ByVal hdc As Long, lpPoint As POINTAPI) As Long
Public Declare Function GetDesktopWindow Lib "User32" () As Long
Public Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Public Declare Function GetExitCodeThread Lib "kernel32" (ByVal hThread As Long, lpExitCode As Long) As Long
Public Declare Function GetForegroundWindow Lib "User32" () As Long
Public Declare Function GetMenu Lib "User32" (ByVal Hwnd As Long) As Long
Public Declare Function GetMenuCheckMarkDimensions Lib "User32" () As Long
Public Declare Function GetMenuDefaultItem Lib "User32" (ByVal hMenu As Long, ByVal fByPos As Long, ByVal gmdiFlags As Long) As Long
Public Declare Function GetMenuItemCount Lib "User32" (ByVal hMenu As Long) As Long
Public Declare Function GetMenuItemID Lib "User32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
Public Declare Function GetMenuString Lib "User32" Alias "GetMenuStringA" (ByVal hMenu As Long, ByVal wIDItem As Long, ByVal lpString As String, ByVal nMaxCount As Long, ByVal wFlag As Long) As Long
Public Declare Function GetNextWindow Lib "User32" Alias "GetWindow" (ByVal Hwnd As Long, ByVal wFlag As Long) As Long
Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
Public Declare Function GetObjectType Lib "gdi32" (ByVal hgdiobj As Long) As Long
Public Declare Function GetPriorityClass Lib "kernel32" (ByVal hProcess As Long) As Long
Public Declare Function GetProcessShutdownParameters Lib "kernel32" (lpdwLevel As Long, lpdwFlags As Long) As Long
Public Declare Function GetServiceDisplayName Lib "advapi32.dll" Alias "GetServiceDisplayNameA" (ByVal hSCManager As Long, ByVal lpServiceName As String, ByVal lpDisplayName As String, lpcchBuffer As Long) As Long
Public Declare Function GetSubMenu Lib "User32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
Public Declare Function GetSystemMenu Lib "User32" (ByVal Hwnd As Long, ByVal bRevert As Long) As Long
Public Declare Function GetTextAlign Lib "gdi32" (ByVal hdc As Long) As Long
Public Declare Function GetTextCharacterExtra Lib "gdi32" (ByVal hdc As Long) As Long
Public Declare Function GetTextCharset Lib "gdi32" (ByVal hdc As Long) As Long
Public Declare Function GetTextColor Lib "gdi32" (ByVal hdc As Long) As Long
Public Declare Function GetTopWindow Lib "User32" (ByVal Hwnd As Long) As Long
Public Declare Function GetWindow Lib "User32" (ByVal Hwnd As Long, ByVal wCmd As Long) As Long
Public Declare Function GetWindowDC Lib "User32" (ByVal Hwnd As Long) As Long
Public Declare Function GetWindowLong Lib "User32" Alias "GetWindowLongA" (ByVal Hwnd As Long, ByVal nIndex As Long) As Long
Public Declare Function GetWindowOrgEx Lib "gdi32" (ByVal hdc As Long, lpPoint As POINTAPI) As Long
Private Type POINTAPI
        x As Long
        y As Long
End Type
Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
End Type
Public Declare Function GetWindowPlacement Lib "User32" (ByVal Hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long
Private Type WINDOWPLACEMENT
        Length As Long
        flags As Long
        showCmd As Long
        ptMinPosition As POINTAPI
        ptMaxPosition As POINTAPI
        rcNormalPosition As RECT
End Type
Public Declare Function GetWindowText Lib "User32" Alias "GetWindowTextA" (ByVal Hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function GetWindowTextLength Lib "User32" Alias "GetWindowTextLengthA" (ByVal Hwnd As Long) As Long
Public Declare Function GetWindowThreadProcessId Lib "User32" (ByVal Hwnd As Long, lpdwProcessId As Long) As Long
Public Declare Function GetWindowWord Lib "User32" (ByVal Hwnd As Long, ByVal nIndex As Long) As Integer
Public Declare Function SetAbortProc Lib "gdi32" (ByVal hdc As Long, ByVal lpAbortProc As Long) As Long
Public Declare Function SetActiveWindow Lib "User32" (ByVal Hwnd As Long) As Long
Public Declare Function SetBkColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
Public Declare Function SetBkMode Lib "gdi32" (ByVal hdc As Long, ByVal nBkMode As Long) As Long
Public Declare Function SetClassLong Lib "User32" Alias "SetClassLongA" (ByVal Hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function SetClassWord Lib "User32" (ByVal Hwnd As Long, ByVal nIndex As Long, ByVal wNewWord As Long) As Long
Public Declare Function SetForegroundWindow Lib "User32" (ByVal Hwnd As Long) As Long
Public Declare Function SetMenu Lib "User32" (ByVal Hwnd As Long, ByVal hMenu As Long) As Long
Public Declare Function SetMenuDefaultItem Lib "User32" (ByVal hMenu As Long, ByVal uItem As Long, ByVal fByPos As Long) As Long
Public Declare Function SetMenuItemBitmaps Lib "User32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long, ByVal hBitmapUnchecked As Long, ByVal hBitmapChecked As Long) As Long
Public Declare Function SetParent Lib "User32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Public Declare Function SetTextAlign Lib "gdi32" (ByVal hdc As Long, ByVal wFlags As Long) As Long
Public Declare Function SetTextCharacterExtra Lib "gdi32" Alias "SetTextCharacterExtraA" (ByVal hdc As Long, ByVal nCharExtra As Long) As Long
Public Declare Function SetTextColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
Public Declare Function SetTextJustification Lib "gdi32" (ByVal hdc As Long, ByVal nBreakExtra As Long, ByVal nBreakCount As Long) As Long
Public Declare Function SetWindowExtEx Lib "gdi32" (ByVal hdc As Long, ByVal nX As Long, ByVal nY As Long, lpSize As Size) As Long
Private Declare Function SetWindowLong Lib "User32" Alias "SetWindowLongA" (ByVal Hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function SetWindowOrgEx Lib "gdi32" (ByVal hdc As Long, ByVal nX As Long, ByVal nY As Long, lpPoint As POINTAPI) As Long
Public Declare Function SetWindowPlacement Lib "User32" (ByVal Hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long
Public Declare Function SetWindowText Lib "User32" Alias "SetWindowTextA" (ByVal Hwnd As Long, ByVal lpString As String) As Long
Public Declare Function SetWindowWord Lib "User32" (ByVal Hwnd As Long, ByVal nIndex As Long, ByVal wNewWord As Long) As Long
Public Declare Function ShowWindow Lib "User32" (ByVal Hwnd As Long, ByVal nCmdShow As Long) As Long

Private Declare Function RemoveMenu Lib "User32" _
    (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long

Public Declare Function BringWindowToTop Lib "User32" (ByVal Hwnd As Long) As Long


Const GWL_STYLE = (-16)
Const GWL_EXSTYLE = (-20)
Const WM_GETTEXT = &HD
Const WM_GETTEXTLENGTH = &HE
Const GWL_HWNDPARENT = (-8)
Const WM_COMMAND = &H111
Const MIN_ALL = 419
Const MIN_ALL_UNDO = 416
Const MAX_PATH = 260
Const SW_SHOW = 5
Const SW_RESTORE = 9
Const SW_SHOWMINIMIZED = 2
Const SW_SHOWMAXIMIZED = 3
Const SW_SHOWNORMAL = 1
Public Const SWP_HIDEWINDOW = &H80
Public Const SWP_SHOWWINDOW = &H40
Public Const SWP_NOMOVE = &H2
Public Const WM_SETTEXT = &HC
Public Const WM_CHAR = &H102
Public Const WM_ENABLE = &HA
Public Const WM_ERASEBKGND = &H14
Public Const WM_LBUTTONDBLCLK = &H203
Public Const WM_LBUTTONDOWN = &H201
Public Const WM_LBUTTONUP = &H202
Public Const WM_MBUTTONDBLCLK = &H209
Public Const WM_MBUTTONDOWN = &H207
Public Const WM_MBUTTONUP = &H208
Public Const WM_MENUCHAR = &H120
Public Const WM_MOUSEMOVE = &H200
Public Const WM_NCLBUTTONDBLCLK = &HA3
Public Const WM_NCLBUTTONDOWN = &HA1
Public Const WM_NCLBUTTONUP = &HA2
Public Const WM_NCMBUTTONDBLCLK = &HA9
Public Const WM_NCMBUTTONDOWN = &HA7
Public Const WM_NCMBUTTONUP = &HA8
Public Const WM_NCMOUSEMOVE = &HA0
Public Const WM_NCRBUTTONDBLCLK = &HA6
Public Const WM_NCRBUTTONDOWN = &HA4
Public Const WM_NCRBUTTONUP = &HA5
Public Const WM_RBUTTONDBLCLK = &H206
Public Const WM_RBUTTONDOWN = &H204
Public Const WM_RBUTTONUP = &H205
Public Const WM_SETFOCUS = &H7
Public Const WM_SETREDRAW = &HB
Public Const WM_SYSKEYDOWN = &H104
Public Const WM_SYSKEYUP = &H105
Public Const WM_UNDO = &H304
Public Const WM_VSCROLL = &H115
Private Const WM_CLOSE = &H10
Public Const WM_DESTROY = &H2
Public Const WM_QUIT = &H12
Const MAXIMIXE_BUTTON = &HFFFEFFFF
Const MINIMIZE_BUTTON = &HFFFDFFFF
Const MF_BYPOSITION = &H400


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 Declare Function GetWindowRect Lib "User32" _
    (ByVal Hwnd As Long, lpRect As RECT) As Long

Public Declare Function GetCursorPos Lib "User32" _
    (lpPoint As POINTAPI) As Long
    
Public Declare Function WindowFromPoint Lib "User32" _
    (ByVal xPoint As Long, ByVal yPoint As Long) As Long
    
Public Declare Function FindWindow Lib "User32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    
Public Declare Function PostMessage Lib "User32" Alias "PostMessageA" _
    (ByVal Hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
    ByVal lParam As Long) As Long

Public Function GetWindowCaption(NHwnd As Long) As String
    Dim Buffer As String * 128
    Dim Length As Long
    Length = MAX_PATH
    GetWindowText NHwnd, Buffer, Length
    GetWindowCaption = Trim(Buffer)
End Function

Public Function GetWindowTextInside(Hwnd As Long) As String
    Dim lLength As Long
    Dim sText   As String
    lLength = SendMessage(Hwnd, WM_GETTEXTLENGTH, 0, ByVal 0&)
    sText = Space$(lLength + 1)
    Call SendMessage(Hwnd, WM_GETTEXT, lLength + 1, ByVal sText)
    GetWindowTextInside = Left$(sText, lLength)
End Function

Public Function SetWindowTextInside(Hwnd As Long, NewText As String)
    Dim lLength As Long
    lLength = Len(NewText)
    Call SendMessage(Hwnd, WM_SETTEXT, lLength + 1, ByVal NewText)
End Function

Public Function DblClickOnWindow(Hwnd As Long)
    Call SendMessage(Hwnd, WM_LBUTTONDBLCLK, 1, 0)
End Function

Public Function HideStartButton(Optional Choice As Long = 0)
    Dim FHWnD As Long
    'ShowWindow 131150, Choice
    FHWnD = FindWindow("Shell_traywnd", "")
    ShowWindow FHWnD, Choice
    'EnableWindow FHwnD, False
End Function

Public Function HideQuickLaunch(Optional Choice As Long = 0)
    ShowWindow 65660, Choice
    ShowWindow 65630, Choice
End Function

Public Function HideTray(Optional Choice As Long = 0)
    ShowWindow 196662, Choice
End Function

Public Function HideDesktop(Optional Choice As Long = 0)
    ShowWindow 65684, Choice
End Function

Public Sub RemoveMenuFromHwnd(ByVal Hwnd, ByVal MenuToRemove As RemoveMenuEnum)
    Dim lSysMenu As Long
    Dim lStyle   As Long

    lSysMenu = GetSystemMenu(Hwnd, 0&)
    Call RemoveMenu(lSysMenu, MenuToRemove, MF_BYPOSITION)
    
    Select Case MenuToRemove
        Case rmClose
            Call RemoveMenu(lSysMenu, MenuToRemove - 1, MF_BYPOSITION)
            
        Case rmMinimize, rmMaximize
            If MenuToRemove = rmMaximize Then
                lStyle = lStyle And MAXIMIXE_BUTTON
            Else
                lStyle = lStyle And MINIMIZE_BUTTON
            End If
            Call SetWindowLong(Hwnd, GWL_STYLE, lStyle)
    End Select
End Sub

