Delphi custom cursor

Source: Internet
Author: User

Developers all want their own Program There is a friendly interface. At this time, a lively and expressive cursor is essential. By default, Windows provides 22 standard cursors for calling in the program. However, for a new programmer, the standard cursors cannot meet the requirements and must use their own cursors, how can I use my own cursor in a program? ● To use a custom cursor to obtain a cursor resource, 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 set of cursor files that come with Delphi (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 find many cursor files. You can also create a cursor file by yourself. For example, you can use the image editor provided by Delphi to create a static cursor file (. cur), but the image editor cannot create a color cursor. To create a color cursor, you must use other tools, such as VC ++ resource editor. Dynamic cursors must be made using specialized tools and software. dynamic cursors can achieve the animation effect of the cursor. The file structure of the dynamic cursor file is similar to that of the AVI file, it consists of four parts: text description area, information area, time control area, and data area. Windows uses the cursor or icon image included in the playback file data area at the specified time frame in the file time control area to achieve animation effect. In fact, the analysis file structure can be found that the static cursor file (. cur) and 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 tracking point (hot spot ). The file identifier is placed in the first three bytes of the file. The icon file is 00 00 01, and the cursor file is 00 00 02. The icon file has no tracking point, the tracking point information of the cursor file is placed in the four bytes of the file 00 0a-00 0d. 00 0a-00 0b records the X coordinate of the tracking point, and 00 0c-00 0d records the Y coordinate, the record order is always in front of the high position. After the low position, the 00 0a-00 0d of the icon file is reserved. By knowing the differences between the two, you can manually or compile a small program to convert the icon file to the cursor file. Compared with the cursor file, the icon file is much easier to find and more colorful. In addition to the cursor file, you can also create a resource file (. Res) that contains the cursor resource. Delphi can also be called in a program. You can use image editor or Microsoft resource editor to create resource files (VC ++ resource editor is recommended ). Delphi creates a resource file with the same name as the project name for each project by default. 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 name it with an existing resource. After creating the resource, use the compilation command $ R in the program to add the resource file to the program. For example, the resource file is my. add a row {$ r my. res. ● Using Delphi in a program to use a custom cursor is achieved by calling Windows API functions. The Screen Object definition of Delphi has a cursors attribute, which is declared as property cursors [index: interger]: hcursor; the cursors attribute actually records the handles of all the cursor resources used in the application, index indicates the index number of each resource. By default, Delphi provides 22 types of cursor resources, 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, both functions are defined in Windows units. 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 name .) Function loadcursor (hinstance: hinst; lpcursorname: pansichar): hcursor; stdcall; // hinstance is the application handle, and lpcursorname is the name of the cursor resource. Resource files 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 should not be the same as the existing index number; otherwise, the existing cursor will be overwritten. To use this cursor, you only need to assign the cursor index to the cursor attribute of the component. Delphi searches for the cursors Array Based on the index number and finds the cursor handle, use the setcursor function to assign the handle to the component. It should be noted that when the program ends, you do not have to call the deletecursor function to release the cursor resources. Delphi will automatically release 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 resu Lt1, result2, result3: integer; begin result1: = loadcursorfromfile ('My. cur'); If result1 <> 0 then // if the return 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 (' An error occurred while loading the dynamic cursor file! '); Result3: = loadcursor (hinstance,' mycursor '); hinstance is a long integer variable defined in the system unit. Its value is the application handle, which is maintained by Delphi. When loading the resource file's cursor resource, if the cursor name is an integer (the default name assigned to the resource by the resource editor of VC ++ is an integer ), you must use the API function makeintresource to convert the integer 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. the program is successfully debugged in Windows95 and Delphi3.0.

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.