Gå til innhold

Hvordan kan jeg Stoppe et annet program / vindu?


Anbefalte innlegg

Hvordan kan jeg Stoppe et annet program / vindu?

 

Jeg prøver å få til dette ved hjelp av windows-api.

Her er et eksempel som jeg trodde skulle virke.

 

Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long

Declare Function FindWindow32 Lib "user32" Alias "FindWindowA" _

(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

 

 

Sub main()

‘-- Starter et program

mServer_Handle = Shell("C:windowssystem32sol.exe")

‘-- Finner programvinduet

hwnd = FindWindow32(vbNullString, "Solitaire")

‘-- Prøver å stoppe det.

tmp2 = DestroyWindow(hwnd)

End Sub

 

Har prøvd å finne andre api-kall som jeg kan bruke, men jeg har ikke nok api-dokumentasjon til å finne ut av det.

 

Jeg prøver dette med Windows Xp, og VB 5.0.

 

:o

Lenke til kommentar
Videoannonse
Annonse

Kan ikke se noen umiddelbar feil i den koden... :-?

 

 

men jeg fant et prog som du kanskje vil like

http://www.allapi.net/agnet/appdown.htm

 

og et eksempel....

 

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long

Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

Const SW_SHOWNORMAL = 1

Const WM_CLOSE = &H10

Const gcClassnameMSWord = "OpusApp"

Const gcClassnameMSExcel = "XLMAIN"

Const gcClassnameMSIExplorer = "IEFrame"

Const gcClassnameMSVBasic = "wndclass_desked_gsk"

Const gcClassnameNotePad = "Notepad"

Const gcClassnameMyVBApp = "ThunderForm"

Private Sub Form_Load()

   'KPD-Team 1998

   'URL: http://www.allapi.net/

   'E-Mail: [email protected]

   Dim WinWnd As Long, Ret As String, RetVal As Long, lpClassName As String

   'Ask for a Window title

   Ret = InputBox("Enter the exact window title:" + Chr$(13) + Chr$(10) + "Note: must be an exact match")

   'Search the window

   WinWnd = FindWindow(vbNullString, Ret)

   If WinWnd = 0 Then MsgBox "Couldn't find the window ...": Exit Sub

   'Show the window

   ShowWindow WinWnd, SW_SHOWNORMAL

   'Create a buffer

   lpClassName = Space(256)

   'retrieve the class name

   RetVal = GetClassName(WinWnd, lpClassName, 256)

   'Show the classname

   MsgBox "Classname: " + Left$(lpClassName, RetVal)

   'Post a message to the window to close itself

   PostMessage WinWnd, WM_CLOSE, 0&, 0&

End Sub

Lenke til kommentar

Takk, dette var bra, jeg skriver på norsk, og får svar så fort!

 

Jeg fant eksemplet under på den adressen du anbefalte, det tror jeg løser problemet mitt.

 

How can I close an application?

You can always use the FindWindow-function and the PostMessage-function to find the wanted application, and then send it a message that it has to close itself. One disadvantage: you'll need the exact caption of this application.

 

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Const WM_CLOSE = &H10

Private Sub Form_Load()

Dim winHwnd As Long

Dim RetVal As Long

winHwnd = FindWindow(vbNullString, "Calculator")

If winHwnd <> 0 Then

PostMessage winHwnd, WM_CLOSE, 0&, 0&

Else

MsgBox "The Calculator is not open."

End If

End Sub

 

Jeg løste problemet i går, ved hjelp av et API-kall som heter EndTask, men Microsoft advare mot å bruke det fordi de kanskje ikke vil støtte det i fremtiden.

Lenke til kommentar

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
×
×
  • Opprett ny...