|
-
Jul 2nd, 2000, 05:49 PM
#1
Thread Starter
Addicted Member
Hi all,
As I,m not too familiar with API's, I'm having a problem closing Notepad from my app. I can create a new text file, write and save to it, then later open the file in Notepad.
My question is...how do I close Notepad from my app.
I've tried this:
ShellExecute 0&, "Close", "C:\Programs\Accessories\Notepad"
but its obviously wrong. Can anyone give me the correct code to close Notepad please.
GRAHAM
-
Jul 2nd, 2000, 06:31 PM
#2
Hyperactive Member
-
Jul 2nd, 2000, 06:33 PM
#3
Fanatic Member
Code:
Option Explicit
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
Private Const WM_CLOSE = &H10
Private Sub Form_Load()
Dim lng_Dialog As Long
'PURPOSE: Get the handle of an application by the classname
lng_Dialog = Findwindow(vbNullString, "Untitled - Notepad")
Call SendMessage(lng_Dialog, WM_CLOSE, 0, 0&)
End Sub
Instead of "Untitled - Notepad", put in your application's name. If you don't know what it is, just open it and right click on the Taskbar and select Task Manager. Or you can just look at the title on the upper left hand of the application. Before doing anything further, just open a regular Notepad session and run this code against it.
Make sure you don't misspell otherwise it won't be able to get the handle.
[Edited by Nitro on 07-03-2000 at 07:38 AM]
Chemically Formulated As:
Dr. Nitro
-
Jul 2nd, 2000, 06:55 PM
#4
Thread Starter
Addicted Member
Thanks anyway Paul, and thanks for the code Nitro.
Am I right in assuming that the ShellExecute API is only used to open an application and does not support closing.
It seems strange to have to find the open Window, then send a Close message to it.
Nitro,is there a single API that will handle both Open and Close, just to simplify matters, or doesn't one exist. There are so many API calls, its hard to be familiar with most of them
Thanks again
GRAHAM
Edit
(Thats strange...it didn't wordwrap??)
[Edited by GRAHAM on 07-03-2000 at 07:59 AM]
-
Jul 2nd, 2000, 07:01 PM
#5
GRAHAM's solution will probably do the job for you because you most likely know the exact Notepad caption, but just in case you don't, you can do the following. Put this code in a module.
Code:
Option Explicit
Public g_bFound As Boolean
Public Const WM_CLOSE = &H10
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Public Function EnumProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
Dim sString As String * 255
Call GetWindowText(hwnd, ByVal sString, 255)
If left(sString, 1) <> Chr(0) Then
sString = left(sString, InStr(sString, Chr(0)) - 1)
If InStr(sString, "Notepad") Then
Call SendMessage(hwnd, WM_CLOSE, 0, 0&)
g_bFound = True
End If
End If
EnumProc = hwnd
End Function
Then call the routine with this.
Code:
Do Until g_bFound
Call EnumWindows(AddressOf EnumProc, 0&)
DoEvents
Loop
-
Jul 2nd, 2000, 07:05 PM
#6
Fanatic Member
GRAHAM, I don't know any API that Open and Close a file. I don't think it exist but someone might be able to enlighten my knowledge on this.
As for the word wrap, it is because I place the code. Once there is code in a thread, it no longer wraps. This is what Denniswrenn told me.
Have a nice day!
Chemically Formulated As:
Dr. Nitro
-
Jul 2nd, 2000, 07:18 PM
#7
Thread Starter
Addicted Member
-
Jul 2nd, 2000, 07:26 PM
#8
_______
//
..I couldn't find one either..there is one
shell that will close a dos window after the batch
file has completed running but it doesn't pertain
to exe as the command.com isn't involved...
"A myth is not the succession of individual images,
but an integerated meaningful entity,
reflecting a distinct aspect of the real world."
___ Adolf Jensen
-
Jul 2nd, 2000, 07:43 PM
#9
Thread Starter
Addicted Member
Thanks for looking Wayne.
Strange isn't it... you'd think it would be logical to
have a Shell call that supported Close as well as Open.
GRAHAM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|