Results 1 to 3 of 3

Thread: Close 1 specific window

  1. #1

    Thread Starter
    Addicted Member Jakys's Avatar
    Join Date
    Dec 1999
    Location
    Norway
    Posts
    180

    Question

    I saw once a code for closing windows by entering the windowtitle. I need that or another code to close a window! plzzz help...

    Jakys

  2. #2
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    This might help...

    Part of title:
    This code uses EnumWindows to retrieve the title of all
    windows in the system, compares it with the partial name,
    and stops when a match is made. Code modified from a sample
    available at http://www.thescarms.com
    Code taken from a Q&A On Experts Exchange Posted By: Erick37


    '~~~~~~~MODULE CODE~~~~~~~~~~~~

    Option Explicit

    Public Const WM_CLOSE = &H10
    Public Const MAX_PATH = 260

    Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    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
    Declare Function EnumWindows Lib "user32" _
    (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
    (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

    Private sAppTitle As String
    Public glHwnd As Long

    Public Function fEnumWindowsCallBack(ByVal hwnd As Long, ByVal lpData As Long) As Long
    Dim lResult As Long
    Dim sWndName As String

    fEnumWindowsCallBack = 1
    sWndName = Space$(MAX_PATH)

    lResult = GetWindowText(hwnd, sWndName, MAX_PATH)
    sWndName = Left$(sWndName, lResult)
    'Search Title for our string
    If (InStr(1, sWndName, sAppTitle, vbTextCompare) > 0) Then
    Debug.Print sWndName
    glHwnd = hwnd
    fEnumWindowsCallBack = 0
    End If
    End Function

    Public Function SearchWindows(sApp As String, hwnd As Long) As Long
    sAppTitle = sApp
    glHwnd = 0
    Call EnumWindows(AddressOf fEnumWindowsCallBack, hwnd)
    SearchWindows = glHwnd
    End Function

    '~~~~~~~FORM CODE~~~~~~~~~~~~~~

    Option Explicit

    Private Sub Command1_Click()
    Dim sApp As String
    'Find notepad with partial name
    sApp = "notepa"
    glHwnd = SearchWindows(sApp, Me.hwnd)
    'End application if found
    If glHwnd > 0 Then
    PostMessage glHwnd, WM_CLOSE, 0&, 0&
    End If
    End Sub

  3. #3
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    This code can close any external program,

    [code]
    'Module modSystem.bas
    Option Explicit
    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 GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

    Public Type POINTAPI
    X As Long
    Y As Long
    End Type

    Public xPt As POINTAPI

    Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As String) 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 WM_SYSCOMMAND = &H112
    Public Const SC_CLOSE = &HF060&


    Public dl&
    Public xhwnd&


    Public Function CloseExtApplication(ByVal lpWindowName As String) As Integer
    xhwnd = FindWindow(0&, lpWindowName)
    If xhwnd <> 0 Then
    dl = SendMessage(xhwnd, WM_SYSCOMMAND, SC_CLOSE, 0&)
    CloseExtApplication = 1
    Else
    CloseExtApplication = 0
    End If
    End Function

    Public Function Get_WindowName() As String
    Dim hWndOver&
    Dim xWndText$
    Dim xReturn As String
    dl = GetCursorPos(xPt)

    hWndOver = WindowFromPoint(xPt.X, xPt.Y)
    If hWndOver <> 0 Then
    xReturn = String(255, Chr(0))
    dl = GetWindowText(hWndOver, xReturn, 100)
    If dl <> 0 Then
    xWndText = Left(xReturn, InStr(1, xReturn, Chr(0), vbBinaryCompare) - 1)
    Get_WindowName = xWndText
    Else
    Get_WindowName = ""
    End If
    Else
    Get_WindowName = ""
    End If


    End Function


    'Form frmMain.frmOption Explicit
    Dim xFile$
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

    Dim sMove As Boolean


    Private Sub chkUnload_Click()
    On Error Resume Next
    If chkUnload.Value Then
    Image2.Enabled = True
    lblTips(2).Enabled = True
    txtTitle.Enabled = True
    txtTitle.SetFocus
    Else
    Image2.Enabled = False
    lblTips(2).Enabled = False
    txtTitle.Enabled = False
    End If

    End Sub

    Private Sub cmdDlg_Click()
    With CDlg
    .DialogTitle = "Open Application"
    .Filter = "Application (*.exe)|*.exe"
    .FileName = App.Path & "\*.exe"
    .ShowOpen
    If StrComp(Right(.FileName, 5), "*.exe", vbBinaryCompare) <> 0 Then
    txtApp.Text = .FileName
    txtApp.SelStart = Len(txtApp)
    xFile = .FileName
    Shell .FileName, vbNormalFocus
    End If
    End With
    End Sub

    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    If chkUnload Then
    If CloseExtApplication(txtTitle) <> 1 Then
    Beep
    Sleep 250
    Beep
    Sleep 250
    Beep
    MsgBox "Fail to unload the specified application!", vbExclamation + vbOKOnly, "Diu Nia Sing"
    End If
    End If
    End Sub


    Private Sub Image2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = vbLeftButton Then
    sMove = True
    Screen.MouseIcon = Image2.Picture
    Screen.MousePointer = vbCustom
    End If
    End Sub


    Private Sub Image2_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    sMove = False
    Screen.MousePointer = vbDefault
    txtTitle.Text = Get_WindowName
    End Sub

    Private Sub lblTips_Click(Index As Integer)
    'if chkunload then
    End Sub

    [/code/




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width