Results 1 to 4 of 4

Thread: Close a program

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ilirska Bistrica, Slovenia
    Posts
    242

    Arrow

    Hello!

    How can I close some other program through my own program in VB? How can I minimize/maximize/set focus it?
    Zvonko Bostjancic
    Ilirska Bistrica, Slovenia
    [email protected]
    Using VS6 Professional with SP3
    Programming mostly in VB and I've started to learn VC++ & MFC

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    To close a window send a WM_CLOSE message to the window by it's handle (hwnd) using sendMessage api. Use FindWindow to find a window by it's caption or classname. Use showwindow to minimize/maximize your window. YOu could use SetForegroundWindow to activate a window, or in vb use appactivate
    Code:
    'in declarations
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Const WM_CLOSE = &H10
    Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
    Private Const SW_SHOWMINIMIZED As Long = 2
    Private Const SW_SHOWMAXIMIZED As Long = 3
    Private Const SW_RESTORE As Long = 9
    'in code
    wnd = FindWindow("NotePad", vbNullString)'find for instance notepad
    ShowWindow wnd, SW_SHOWMINIMIZED'to minimize
    ShowWindow wnd, SW_SHOWMAXIMIZED'to maximize
    ShowWindow wnd, SW_SW_RESTORE ' to restore
    SendMessage wnd, WM_CLOSE, 0, 0 'to close
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3
    Guest
    It's a good idea to Destroy the window once it's been closed.

    Code:
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
    Const WM_CLOSE = &H10
    Const WM_DESTROY = &H2
    
    Private Sub Command1_Click()
    
        MyHwnd = FindWindow(0&, "Calculator")
        SendMessage MyHwnd, WM_CLOSE, 0, 0
        SendMessage MyHwnd, WM_DESTROY, 0, 0
        
    End Sub

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    uh, i forgot the setfocus thing;
    Code:
    'in declarations
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
    Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    
    'in code
        wnd = FindWindow("NotePad", vbNullString)
        SetForegroundWindow wnd
    'or
        AppActivate "Calculator"
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

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