Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 145
  • Last Modified:

Closing Windows Applications programatically

I am using D1 and WIN 3.11
I need a way of closing other open applications from within my ap.  The other programs which may be open, I have written myself in Delphi, but the Title Bars do not contain a caption, so I can't use the obvious API call.
Can someone please provide a code snippet for me.

Thanx in advance
0
jdthedj
Asked:
jdthedj
  • 4
  • 3
  • 2
1 Solution
 
ronit051397Commented:
Closing a notepad document:
SendMessage(FindWindow('Notepad', nil), wm_Close, 0, 0);
0
 
jdthedjAuthor Commented:
Sorry, Ronit, but that won't work because as I mentioned, the ap does not have a caption in the Title Bar.
0
 
JimBob091197Commented:
Ronit's answer doesn't need a caption in the title bar!  Replace 'notepad' with your form class name:

E.g.  SendMessage(FindWindow(PChar('TMyForm'), nil), WM_CLOSE, 0, 0);

JB
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
jdthedjAuthor Commented:
Hi JimBob - thanks for replying.  I've just tried your suggestion and I still can't get the Ap to close - any other ideas??  JD

0
 
JimBob091197Commented:
Mmm...  I've used this method in Delphi 2 & 3, so I assume it should work in Delphi 1.  (The Win api help is the same for Win 3.1 and Win 95.)

You can check it by creating a simple Delphi app, with form class TMyForm.  Create a second app which calls SendMessage(...);  If your first app has no code preventing it from closing, then it should close!  Try also with notepad and Ronit's SendMessage code.

It's possible that Delphi 1 works a little differently from Delphi 2 & 3.  If this is the case try:
In your app to close:
  Application.Title := 'Test App';

In your calling app:
  SendMessage(FindWindow('TApplication', 'Test App'), WM_CLOSE, 0, 0);

I don't think this will work, but try it anyway...

JB
0
 
jdthedjAuthor Commented:
Thanks JimBob

I ran a test as you suggested and it worked. I cut and pasted the code to my own calling ap and it didn't work.  I finally realised that both forms were called TMainForm!!
There's 50 points up for grabs if you turn this into an answer  (or should Ronit come back and claim it).

Thanks again.
0
 
ronit051397Commented:
Jimi deserves the points, mainly for his patience.
0
 
JimBob091197Commented:
Oh, well, if you insist...   ;-)

0
 
jdthedjAuthor Commented:
Thanks to both you guys - and sorry, Ronit - My ignorance of WINAPI calls and things I had read led me to believe that you needed a caption to work with.
JD
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now