Use custom cursor in Delphi

Source: Internet
Author: User

Developers all want their programs to have a friendly interface, which is lively and expressive.
The Force cursor is essential. By default, Windows provides 22 standard cursors for calling in programs,
For a programmer seeking for new changes, the standard cursor cannot meet the requirements, and you need to use your own
How to use your own cursor in a program?
● Acquisition of cursor Resources
To use a custom cursor, you must first obtain the cursor. There are two types of cursor files: static cursor
(. Cur) and dynamic cursor (. Ani ). You can use a ready-made cursor file, such as
A group of cursor files (in the c: \ Program Files \ Borland \ Delphi 3 \ images \ cursors directory
If you have installed Windows 98 desktop theme, you can go to \ windows \ plus! Directory
To many cursor files. You can also create a cursor file by yourself, for example, using the image that comes with Delphi
The editor can create a static cursor file (. cur), but the image editor cannot create a color cursor,
To create a colored cursor, you must use other tools, such as the VC ++ resource editor. Dynamic Cursor
It must be made using specialized tools and software, and the dynamic cursor can be used to achieve the animation effect of the cursor, dynamic light
The file structure of the standard file is similar to that of the AVI file. It consists of the text description area, information area, and time control area.
And Data zone. In Windows, one frame is played by the specified time in the file time control area.
Place the cursor or icon image in the file data area to achieve the animation effect.

In fact, the analysis file structure shows that the static cursor file (. cur) is connected with the icon file (. ICO ).
The structure is very similar. The main difference between the two types of files is only the file identification code of the file header and whether there is a trace.
Point (hot spot ). Put the file identifier in the first three bytes of the file. The icon file is 00 00 01, light
The icon file has no tracking point, and the tracking point information of the cursor file is placed in
00 0a-00 0d four bytes, 00 0a-00 0b records the X coordinate of the tracking point, 00 0c-00
0d records Y coordinates, the record order is high in front, after the low position, the icon file 00 0a-00
0d is the Reserved Bit. Knowing the difference between the two, you can implement it by manually or by compiling a small program.
Convert the icon file to the cursor file. Compared with the cursor file, the icon file is much easier to find.
Color is also richer.

In addition to the cursor file, you can also create a resource file (. Res) that contains light
Resource, Delphi can also be called in the program. You can use image editor or Microsoft's
Resource editor creates a resource file (VC ++ resource editor is recommended ). Delphi defaults
Create a resource file with the same name as the project name. If your project name is test. DPR
The resource file name is test. Res. However, if you place the cursor in this file, it cannot be called in the program.
You must create your own resource file. When naming a cursor resource, be sure not to associate it with an existing resource.
After the resource is created, use the compilation command $ R in the program to add the resource file to the program. For example
If the source file is my. Res, add a {$ r My. Res} line under implementation of the main form.
.
● Use a custom cursor in a program
Delphi uses a custom cursor by calling Windows API functions. Delphi's
The Screen Object definition has a cursors attribute, which is declared as property
Cursors [index: interger]: hcursor; the cursors attribute actually records the usage
All cursor resource handles, index is the index number of each resource. Default 22 types provided by Delphi
The cursor resource is also in it, and the index value is-21 ~ 0. To use your own cursor, call the API
Function loadcursorfromfile (for the cursor file) or use loadcursor (for the resource file
) To obtain the corresponding cursor handle. These two functions are defined in the Windows unit. The function declaration is:
Function loadcursorfromfile (lpfilename: pansichar): hcursor; stdcall;
// Lpfilename is the name of the cursor file. (BOTH labels are applicable. You only need to specify the cursor file.
.)

Function loadcursor (hinstance: hinst; lpcursorname: pansichar ):
Hcursor; stdcall;
// Hinstance is the application handle, and lpcursorname is the name of the cursor resource. Capital
The source file can only contain static cursor resources. resource files do not support resources in the format of dynamic cursor.
For details about the two functions, refer to the Win32 help of Delphi.

After obtaining the cursor handle, add the handle value to the cursors array. Note that the index is not
It must be the same as the existing index number. Otherwise, the existing cursor will be overwritten. To use this indicator, you only need
You can assign the index value of the cursor to the cursor attribute of the component. Delphi searches for the element based on the index number.
Cursors array, locate the cursor handle, and use the setcursor function to assign this handle to the component.
It must be noted that when the program ends, you do not have to call the deletecursor function to release the cursor resource,
Delphi Automatically releases them.
● Program example
Unit unit1;

Interface

Uses

Windows, messages, sysutils, classes, graphics, controls,
Forms, dialogs;

Type

Tform1 = Class (tform)

Procedure formcreate (Sender: tobject );

Private

{Private Declarations}

Public

{Public declarations}

End;

VaR

Form1: tform1;

Implementation

{$ R *. DFM}

{$ R My. Res} // load the resource file my. Res

Procedure tform1.formcreate (Sender: tobject );

Const

Crmy1 = 1;

Crmy2 = 2;

Crmy3 = 3;

VaR

Result1, result2, result3: integer;

Begin

Result1: = loadcursorfromfile ('My. cur ′);

If result1 <> 0 then // If the returned value is 0, the call fails!

Screen. cursors [crmy1]: = result1

Else

Showmessage ('An error occurred while loading the static cursor file! ′);

Result2: = loadcursorfromfile ('My. wan ′);

If result2 <> 0 then

Screen. cursors [crmy2]: = result2

Else

Showmessage ('loading dynamic cursor file error! ′);

Result3: = loadcursor (hinstance, 'mycursor ′);

Hinstance is a long integer variable defined in the System Unit and its value is the sentence of the application.
Handled by Delphi.

When loading the resource file's cursor resource, if the cursor name is an integer (VC ++ resource Encoding
The default name provided by the logstore to the resource is an integer). You must use the API function makeintresource to convert the integer
Convert to the pansichar type and pass it to the loadcursor function.

Example: Result: = loadcursor (hinstance, makeintresource (101 ))}

If result3 <> 0 then

Screen. cursors [crmy3]: = result3

Else

Showmessage ('An error occurred while loading the cursor resource in the resource file! ′);

// Use the loaded cursor. The cursors [] array is a global variable and can be called anywhere in the program.

If result1 <> 0 then

Screen. cursor: = crmy1;

If result2 <> 0 then

Form1.cursor: = crmy2;

If result3 <> 0 then

Screen. cursor: = crmy3;

End;

End.

The program is debugged in Windows95 and Delphi3.0 through

Use custom cursor in 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.