Board index » delphi » Win api message ..Help.. Please

Win api message ..Help.. Please

Hi all

Please help me...
I am a newbie to windows messaging, so if you
can help please give samples.

My problem is I want to sent api messages to a program
to do:

1) Open Notepad menu
2) Select file menu
3) Press Save As
4) Enter filename to Save As
5) Press OK button.

   Steps 1 to 5 must be done by Delphi code
   and not by keypress.

Hope someone can help me.

Cheers
Bert

 

Re:Win api message ..Help.. Please


  Tell your professor that I want the credit for this:

procedure TForm1.Button1Click(Sender: TObject);
var  Whnd : HWnd;
begin
     {start Notepad}
     ShellExecute(Handle, 'open', ('Notepad.exe'), nil, nil, SW_NORMAL);
     {pause a bit}
     Sleep(3);

     {get Notepad handle}
     Whnd := FindWindow('Notepad',NIL);

     if Whnd <> 0 then begin                            //got handle
        SetForeGroundWindow(Whnd);
        Keybd_event(VK_MENU, 0, 0, 0);                  //press alt
        Keybd_event(ord('F'), 0, 0, 0);                 //press F (File)
        Keybd_event(ord('A'), 0, 0, 0);                 //press A
(SaveAs)
        Keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);    //keyup all keys
-
        Keybd_event(ord('F'), 0, KEYEVENTF_KEYUP, 0);   // or no more
keyboard
        Keybd_event(ord('A'), 0, KEYEVENTF_KEYUP, 0);

        Clipboard.AsText:='C:\temp.txt';                //place the
filename in clipboard
        Keybd_event(ord(VK_CONTROL), 0, 0, 0);          //press ctrl
        Keybd_event(ord('V'), 0, 0, 0);                 //press V
(Paste)
        Keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0); //keyup all keys
        Keybd_event(ord('V'), 0, KEYEVENTF_KEYUP, 0);
        Keybd_event(ord(VK_TAB), 0, 0, 0);              //press tab
        Keybd_event(VK_TAB, 0, KEYEVENTF_KEYUP, 0);     //keyup
        Keybd_event(ord(VK_TAB), 0, 0, 0);              //press tab
again to Save button
        Keybd_event(VK_TAB, 0, KEYEVENTF_KEYUP, 0);     //keyup
        Keybd_event(ord(VK_RETURN), 0, 0, 0);           //press Enter to
save
        Keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);  //keyup
        end;
end;

  Some runs this didn't work.  Run it again.  There must be a timing
problem.  You could place some more Sleep(3) instructions around to
slow the process down.

Quote
Bert wrote:

> Hi all

> Please help me...
> I am a newbie to windows messaging, so if you
> can help please give samples.

> My problem is I want to sent api messages to a program
> to do:

> 1) Open Notepad menu
> 2) Select file menu
> 3) Press Save As
> 4) Enter filename to Save As
> 5) Press OK button.

>    Steps 1 to 5 must be done by Delphi code
>    and not by keypress.

> Hope someone can help me.

> Cheers
> Bert

Re:Win api message ..Help.. Please


Quote
"Quivis" <qui...@ask.me.first> wrote in message

news:MPG.14a499ade407b935989f3d@207.14.113.10...

Quote
> On Wed, 13 Dec 2000 21:57:39 GMT, GCS had this to say...

> ->      {get Notepad handle}
> ->      Whnd := FindWindow('Notepad',NIL);

> This will only work for the English version, right?  In Swedish it's
> called 'Anteckningar.' So my question would be:

> Is there a way to "translate" this piece of code into something that
> could be used regardless of language used on the system?

The 'Notepad' here does not refer to the window's caption but to its class
name which is always the same regardless of the locale.

Try this sample project. Just drop a button onto a new form and put this in
its event handler (you'll probably need Windows and Messages in your uses
clause):

procedure TForm1.Button1Click(Sender: TObject);
var Whnd: HWND;
begin
  Whnd := FindWindow('Notepad',nil);
  if Whnd<>0 then PostMessage(Whnd,WM_CLOSE,0,0);
end;

Now hit F9 and then start up an instance of Anteckningar. Click on the
button of your form. Got it?

HTH's
GRS

Other Threads