Principle of message sending program using API

Source: Internet
Author: User

Principle of using the API message sending program by shaoyun 2010-03-20

Very simple: the most basic application of several API functions. The code I wrote is also the simplest implementation method. Of course, the higher-order point is implemented through the Hook method.

Principle:

Use the Form class nameFindwindowTo obtain the window handle.FinddomainwexObtain the form handle of the editing box.

WithSendmessageFunction sendingWm_gettextGet the text in the edit box, add your own message, and send it.Wm_settextMessage to set the content of the corresponding child form of the Target Program

Here is an example code. click the button to append a text section to notepad. In practice, you also need to set a timer to cyclically detect the target program to send messages.

Code

1 procedure tform1.btn1click (Sender: tobject );
2 VaR
3 starget, smsg: string;
4 htargetwnd, hedit: thandle;
5 szbuff: array [0 .. 255] of char;
6 begin
7 starget: = 'notepad ';
8 htargetwnd: = findwindow (pwidechar (starget), nil );
9 If htargetwnd <> 0 then
10 begin
11 setforegroundwindow (htargetwnd );
12 hedit: = find1_wex (htargetwnd, 0, pwidechar ('edit'), nil );
13 if hedit <> 0 then
14 begin
15 sendmessage (hedit, wm_gettext, sizeof (szbuff), INTEGER (@ szbuff ));
16 smsg: = szbuff + 'append information! ';
17 sendmessage (hedit, wm_settext, 0, INTEGER (pwidechar (smsg )));
18 end;
19 end;
20 end;

Code test environment: Windows XP SP3 + Delphi 2010

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.