Details about using API functions in Delphi

Source: Internet
Author: User
Don't get it wrong. Here, Dai Fei is not the princess of England who has sold Yu, but the Chinese translation of Delphi, a signboard product of Inprise! There have been a lot of articles about delphi. Are you impressed by its powerful development functions? The emergence of visual programming has turned many friends into a program dream. Indeed, the combination of several controls plus a few statements may be a piece of software. However, are you unable to use other people's components? Want to learn more about Windows programming? Switch master Delphi? OK. Continue.

Int21h under DOS is very important for developing DOS programs, and it is necessary to understand API functions for developing programs under windows. Components are like moves, and API functions are like internal functions. With deep internal forces, learning moves are easy. Therefore, the more API functions you have, the more likely you are to become a Delphi expert or even a Windows Programming expert. Therefore, C ++, VB, and Delphi support API function calls without exception. c ++ and Delphi fully support API functions, while VB only supports some API functions (there are also a few, there are already hundreds of), especially Delphi is the most convenient to call API functions, it has reached the realm of merging with API functions. Calling API functions is almost the same as using their own functions. Next I will explain in detail how to use API functions in Delphi. I will write a small example for each statement to help you better understand how to use them. If you carefully read this article, I believe you will have a deeper understanding of Delphi programming. But never give up halfway. Fear of complexity and complexity. Remember: hard work will be done if you want to exercise your power! Learning programming is not about playing games. You cannot cheat on FPE.

(1) controls and message Functions

① Syntax: anypopup: bool;

Unit: Windows. PAS (The unit Delphi will add in uses, the same below)

Purpose: Determine whether any pop-up window exists on the screen.

Return Value: bool. If a pop-up menu exists, true is returned.

Note: For this function, the pop-up menu contains all visible and inclusive top-level windows, whether pop-up or overlapping windows

Example:

Procedure tform1.button1click (Sender: tobject );

Begin

If (anypopup) then

Label1.caption: = 'pop-ups found: true'

Else

Label1.caption: = 'pop-ups found: false ';

End;

② Syntax: enablewindow (hwnd: hwnd; benable: bool): bool; Unit: Windows. Pas

Purpose: Allow or disable all mouse and keyboard input in the specified window

Return Value: bool. If the returned result is true, Windows is disabled. Otherwise, false is returned.

Example:

Procedure tform1.button1click (Sender: tobject );

Begin

If (isw.wenabled (edit1.handle) then

Begin

Enablewindow (edit1.handle, false );

Button1.caption: = 'Enable Windows ';

Edit1.text: = 'this window is disabled ';

End

Else

Begin

Enablewindow (edit1.handle, true );

Button1.caption: = 'Disable Windows ';

Edit1.text: = 'this window is enabled ';

End;

End;

③ Syntax: flashwindow (hwnd: hwnd; binvert: bool): bool;

Unit: Windows. Pas

Purpose: display the specified window in a flash. This means that the title and description of the window will change, and it seems that the switch from activity to inactive or reverse switch. This function is usually applied to non-active windows to attract users' attention.

Return Value: bool. If the window is active before the call, true is returned.

Note: This function is usually used in combination with a counter to generate a continuous flickering effect.

In Windows NT and windowsfor Workgroup, The binvert parameter is ignored.

But it is not ignored in Windows 95.

Example:

Procedure tform1.timer1timer (Sender: tobject );

Begin

Flashwindow (form1.handle, true );

Flashwindow (application. Handle, true );

End;

④ Syntax: setwindowtext (hwnd: hwnd; lpstring: pchar): bool;

Unit: Windows. Pas

Purpose: set the title text or control content of the window.

Return Value: true if the setting is successful. Otherwise, false is returned.

Example:

Procedure tform1.button1click (Sender: tobject );

VaR

Thetext: pchar;

Textlen: integer;

Begin

Textlen: = getwindowtextlength (form1.handle );

Getmem (thetext, textlen );

Getwindowtext (form1.handle, thetext, textlen + 1 );

Edit1.text: = string (thetext );

Freemem (thetext );

End;

Procedure tform1.button2click (Sender: tobject );

Begin

Setwindowtext (form1.handle, pchar (edit1.text ));

End;

⑤ Syntax: iswindow (hwnd: hwnd): bool;

Unit: Windows. Pas

Purpose: Determine whether a window handle is valid.

Return Value: True is returned; otherwise, false is returned.

Example:

Procedure tform1.button1click (Sender: tobject );

Begin

If (iswindow (button1.handle) then

Button1.caption: = 'true'

Else

Button1.caption: = 'false ';

End;

How are you doing? Today is the first time. I will introduce some easy-to-accept functions. Otherwise, my friends will say no to it. I don't know if my friends can accept this Orchestration format? Also, I will introduce the categories of API functions (controls and message functions/hardware and system functions/menu functions/text and font functions/print functions) separately, however, I will not introduce all the API functions. Otherwise, I will be suspected of having to cheat in the draft fee, and I am still unable to provide examples for every statement. I will only introduce the functions that are commonly used, you can also introduce some secret but useful API functions.

Attached tips (Delphi Tips:

If there is such a directory:

C:/Windows/Media/temp/ABC/sound/chime.wav

I hope it can be shortened:

C:/Windows/../sound/chime.wav

How to write a program?

Answer:

Try the following procedure:

Function shortenfilename (S: string): string;

VaR drive, curdrive: String [2];

Dir, curdir: String [80];

Name: String [20];

Ext: String [5];

I: byte;

Begin

For I: = 1 to length (s) do s [I]: = upcase (s [I]);

S: = fexpand (s );

Fsplit (S, Dir, name, ext );

Drive: = copy (Dir, 1, 2 );

Dir: = copy (Dir, 4, length (DIR)-3 );

Getdir (0, curdir );

Curdrive: = copy (curdir, 1, 2 );

Curdir: = copy (curdir, 4, length (curdir)-3) + '/';

If drive = curdrive then begin

If copy (Dir, 1, length (curdir) = curdir then begin

I: = length (curdir );

If length (DIR) <> I then dir: = dir + '/';

Shortenfilename: = copy (Dir, I + 1, length (DIR)-i-1) + name + ext;

End else shortenfilename: = copy (s, 3, length (S)-2 );

End else shortenfilename: = s;

End;

Note: This article is only written by a friend who improves Delphi's ability. It is not a general entry-level tutorial. Therefore, all the functions and basic skills of Delphi in this article are not described. Please refer to the online help manual of Delphi.

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.