Results 1 to 4 of 4

Thread: closing NotePad from vb

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2000
    Posts
    16

    Question

    Can anyone offer advice on how to close notepad from vb. I don't need to save what ever is in notepad -- i just need to shut it down.

    Thanks a million.

  2. #2
    Lively Member
    Join Date
    Mar 2000
    Location
    Fort Lauderdale, FL USA
    Posts
    112
    Try using the CloseHandle Function

    Example:

    Code in Module:
    Declare Function FindWindow Lib "user32.dll"_ Alias "FindWindowA" (ByVal lpClassName As Any, ByVal_ lpWindowName As Any) As Long

    Declare Function CloseHandle Lib "kernel32.dll" (ByVal_ hObject As Long) As Long


    Code in Form:
    Dim hwnd As Long ' receives handle to the found window
    Dim retval As Long ' generic return value


    'Find window
    hwnd = FindWindow(CLng(0), "Notepad")

    'Close handle
    retval = CloseHandle(hFile)
    Damonous

  3. #3
    Lively Member
    Join Date
    Jan 2000
    Location
    Dallas, TX
    Posts
    89

    Try this...

    ---Module---

    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, lParam As Any) As Long
    Public Const WM_CLOSE = &H10
    Public Const WM_SHOWWINDOW = &H18

    Option Explicit

    Private Declare Function GetWindow Lib "user32" _
    (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    Private Declare Function GetParent Lib "user32" _
    (ByVal hwnd As Long) As Long
    Private Declare Function GetWindowTextLength Lib _
    "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
    Private Declare Function GetWindowText Lib "user32" _
    Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal _
    lpString As String, ByVal cch As Long) As Long
    Const GW_HWNDFIRST = 0
    Const GW_HWNDNEXT = 2

    ---Form---

    Private Sub Command1_Click()

    LoadTaskList

    End Sub

    Public Sub LoadTaskList()
    Dim CurrWnd As Long
    Dim Length As Long
    Dim TaskName As String
    Dim Parent As Long

    List1.Clear
    CurrWnd = GetWindow(Process.hwnd, GW_HWNDFIRST)

    While CurrWnd <> 0
    Parent = GetParent(CurrWnd)
    Length = GetWindowTextLength(CurrWnd)
    TaskName = Space$(Length + 1)
    Length = GetWindowText(CurrWnd, TaskName, Length + 1)
    TaskName = Left$(TaskName, Len(TaskName) - 1)
    If Length > 0 Then
    If TaskName <> Me.Caption Then
    List1.AddItem TaskName
    End If
    End If
    CurrWnd = GetWindow(CurrWnd, GW_HWNDNEXT)
    DoEvents
    Wend

    End Sub

    Private Sub Command2_Click()
    Dim winHwnd As Long
    Dim RetVal As Long

    winHwnd = FindWindow(vbNullString, List1.Text)
    Debug.Print winHwnd
    If winHwnd <> 0 Then
    RetVal = PostMessage(winHwnd, WM_CLOSE, 0&, 0&)
    If RetVal = 0 Then
    MsgBox "Error posting message."
    Else
    MsgBox List1.Text & " closed..."
    End If
    Else
    MsgBox "The Notepad is not open."
    End If

    End Sub

    Hope this helps...
    ande211

  4. #4
    Lively Member
    Join Date
    Mar 2000
    Location
    Fort Lauderdale, FL USA
    Posts
    112
    Sorry mallet, that last line should be:

    retval = CloseHandle(hwnd)
    Damonous

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