Results 1 to 5 of 5

Thread: closing a window from my vb program?

  1. #1

    Thread Starter
    Addicted Member Eric_B's Avatar
    Join Date
    May 2001
    Location
    home sweet home
    Posts
    130

    closing a window from my vb program?

    How can close a window from my vb program?

  2. #2
    New Member
    Join Date
    Nov 2000
    Posts
    13

    Cool

    How can I close an application?

    You can use the FindWindow and PostMessage API functions to find a window and then close it. This example show how you can close down a Window with a caption of "Calculator".

    Dim winHwnd As Long
    Dim RetVal As Long
    winHwnd = FindWindow(vbNullString, "Calculator")
    Debug.Print winHwnd
    If winHwnd <> 0 Then
    RetVal = PostMessage(winHwnd, WM_CLOSE, 0&, 0&)
    If RetVal = 0 Then
    MsgBox "Error posting message."
    End If
    Else
    MsgBox "The Calculator is not open."
    End If

    For this code to work, you must have declared the API functions in a module in your project. You must put the following in the declarations section of the 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

    Tip by James Limm
    http://www.vbworld.com/api/tip2.html

  3. #3

    Thread Starter
    Addicted Member Eric_B's Avatar
    Join Date
    May 2001
    Location
    home sweet home
    Posts
    130
    but that would free the resources or not?

  4. #4
    Matthew Gates
    Guest
    Take a look at this thread for the code's Megatron and Nucleus posted.

  5. #5

    Thread Starter
    Addicted Member Eric_B's Avatar
    Join Date
    May 2001
    Location
    home sweet home
    Posts
    130
    Thanks

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