/ Forside / Teknologi / Udvikling / VB/Basic / Nyhedsindlæg
Login
Glemt dit kodeord?
Brugernavn

Kodeord


Reklame
Top 10 brugere
VB/Basic
#NavnPoint
berpox 2425
pete 1435
CADmageren 1251
gibson 1230
Phylock 887
gandalf 836
AntonV 790
strarup 750
Benjamin... 700
10  tom.kise 610
lukke aplikation
Fra : Jensen


Dato : 10-01-02 20:57

Hi NG.

Hvordan lukker man en 3.parts applikation via VB, f.eks. Notepad ?

Jensen, Denmark




 
 
Jens (10-01-2002)
Kommentar
Fra : Jens


Dato : 10-01-02 21:17

> Hvordan lukker man en 3.parts applikation via VB, f.eks. Notepad ?


I et modul :

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


I en form :

Private Sub Form_Load()

CloseApplication "Unavngivet - Notesblok"
End

End Sub

Public Function CloseApplication(ByVal sAppCaption As String) As Boolean
Dim lHwnd As Long
Dim lRetVal As Long
lHwnd = FindWindow(vbNullString, sAppCaption)
If lHwnd <> 0 Then
lRetVal = PostMessage(lHwnd, WM_CLOSE, 0&, 0&)
End If
End Function


Mvh
Jens



Nick Jensen (10-01-2002)
Kommentar
Fra : Nick Jensen


Dato : 10-01-02 21:49

Tak for svarene

Når nu navnet på aplikationen skal være 100% korrekt, er der så en
måde at finde navnet, hvis man nu f.eks. ikke ved hvilket dokument
der er åbnet i notepad hvordan lukker man det så ???

På forhånd tak

Nick


On Thu, 10 Jan 2002 21:16:57 +0100, "Jens" <til@infojens.dk> wrote:


>> Hvordan lukker man en 3.parts applikation via VB, f.eks. Notepad ?
>
>
>I et modul :
>
>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
>
>
>I en form :
>
>Private Sub Form_Load()
>
> CloseApplication "Unavngivet - Notesblok"
> End
>
>End Sub
>
>Public Function CloseApplication(ByVal sAppCaption As String) As Boolean
> Dim lHwnd As Long
> Dim lRetVal As Long
> lHwnd = FindWindow(vbNullString, sAppCaption)
> If lHwnd <> 0 Then
> lRetVal = PostMessage(lHwnd, WM_CLOSE, 0&, 0&)
> End If
>End Function
>
>
>Mvh
>Jens
>
>


Mikkel Bundgaard (10-01-2002)
Kommentar
Fra : Mikkel Bundgaard


Dato : 10-01-02 21:25

"Jensen" <cabsoft@cabsoft.dk> wrote in message
news:3c3df287$0$5460$edfadb0f@dspool01.news.tele.dk...
> Hi NG.
>
> Hvordan lukker man en 3.parts applikation via VB, f.eks.
> Notepad ?
>
> Jensen, Denmark
Hej Jensen

Nedenstående kodestump lukker notepad via API kaldet
PostMessage.

' --------- KODE START
Option Compare Database
Option Explicit

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 KillNotePad()
Dim WinWnd As Long, Ret As String
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
End If

'Post a message to the window to close itself
PostMessage WinWnd, WM_CLOSE, 0&, 0&
End Sub

Når den spørger om navnet på programmet, skal det være 100%'
præcist det der står i Taskbaren. F.eks. Untitled - Notepad, hvis
du ønsker at lukke notepad (og bruger en engelsk Windows).

Håber du kan bruge det til noget
--
Mikkel Bundgaard
IT University of Copenhagen
http://officehelp.gone.dk
Codito, Ergo Sum



Carsten Suurland (10-01-2002)
Kommentar
Fra : Carsten Suurland


Dato : 10-01-02 23:23

Hej Jensen

Her får du den RIGTIGE måde at gøre det på:

I et modul:

' Constants used
Public Const GW_HWNDNEXT = 2
Public Const GWW_HINSTANCE = (-6)
Public Const WM_CLOSE = &H10

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 Declare Function GetWindow Lib "user32" Alias "GetWindow" (ByVal hwnd
As Long, ByVal wCmd As Long) As Long

Public Declare Function GetWindowWord Lib "user32" Alias "GetWindowWord"
(ByVal hwnd As Long, ByVal nIndex As Long) As Integer

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

Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA"
(ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Function GetWinHandle (hInstance as Long) As Long
Dim tempHwnd as long

' Grab the first window handle that Window's finds:
tempHwnd = FindWindow(0&, 0&)

' Loop until there are no more window handles
Do Until tempHwnd = 0
' Check if you have the applications Parent window:
If GetParent(tempHwnd) = 0 Then
' Check the instance handle for the app:
If hInstance = GetWindowWord(tempHwnd, GWW_HINSTANCE) Then
' Found a match:
GetWinHandle = tempHwnd
Exit Do
End If
End If
tempHwnd = GetWindow(tempHwnd, GW_HWNDNEXT)
Loop
End Function

I en knap

Sub Command1_Click ()
Dim hInst As Long ' Instance handle from Shell function.
Dim hWndApp As Long ' Window handle from GetWinHandle.
Dim lRetVal As Long

' Shell to an application and get its window hndle
hInst = Shell("calc.exe")
hWndApp = GetWinHandle(hInst)

' Now close the application
lRetVal = PostMessage(hWndApp, WM_CLOSE, 0&, 0&)
End Sub

/Carsten Suurland



Søg
Reklame
Statistik
Spørgsmål : 177557
Tips : 31968
Nyheder : 719565
Indlæg : 6408885
Brugere : 218888

Månedens bedste
Årets bedste
Sidste års bedste