How to write your own visual control with Delphi

zhaozj2021-02-11  202

How to write your own visual control with Delphi -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------- Visual Component is actually a class (Class), to write a class, you can Written in the * .pas file. But to write a control, you must use a package (package). Select New from the File menu, create a new package, which is the package that stores and installs the control. Then click the Add button in the Package window to add an element (Unit). Select New Component at the top of the dialog that pops up. Because all attributes, methods, events, events cannot be compiled by themselves, so you need to select ancestors (or "parent class" or "base class"), then add your own properties, methods, events. . Select the ancestor class in the drop-down box after Anadem Type. Since the written visual control must be drawn, TGRAPHICCONTROL is selected as ancestral class. Then enter the name of the new control (class) in the Class name box, generally starting with "T". Palette Page is used to select a control page name in the window of the new control in the window of Delphi, such as "Standard", this can be takeny. Add the path and file name of the new control file in Unit File Name, click the OK button. The new control is added. You can now write code for the control. The following is to write a scroll bar that can customize the picture as an example, and the method of writing visual controls is described. According to the above method, select TGRAPHICCONTROL for ancestral class, the name of the new control is TPIGHORIZONTALSCROLLER. After selecting the file path and the file name, click the OK button to start writing the code. Each control will be created and deleted, so these two processes must be prepared first. For each process in the control, you must first define before, and then write it later. There are three processes or attributes: 1. Definition in private is used inside the control, people who use this control cannot be seen; Second, the definition defined after protected is generally not seen, only in others The control is visible as a ancestors to write other controls as ancestors; III. Only those defined after publicness allows others to call in the program; four, can be seen in the property window (Object Inspector) after PublisHed. Since the creation and deletion process is automatically executed when the control is created during the programming process, it is also possible to be called when the control is created during the program run, so it defines it after public (1). (This sequence number indicates the position of the code in the attached source program, and the same below) may now not know what should be written in these two processes, how to dear. We will talk below. We first add some properties to this control. We define a max attribute to set or read the maximum value of the scroll bar. Because the properties are generally not directly used in the program, there is a variable to define a variable, and the attribute corresponds to, or the value is modified or read. Because it is only used inside the control, we define it after private (2). (Generally, the variables associated with the attribute are defined after "F", such as fmax), and then define the attribute. This attribute needs to be seen in the Object Inspector window, so define it again.

The definition syntax is: Property : Read Write Other variables and attributes are similar to definition (for example MIN Minimum, Value Current Value, etc.). Below we define several properties and variables to set images of the scroll bar (because the picture variable is special, you will talk about it separately). We define the LeftButTonuppicture, LeftButTondownPicture (Press the Left Buttons) or the like as the Tbitmap type (must define the corresponding variable). Everyone must notice that in the attached source, the corresponding variables corresponding to the read attributes specified after the read attribute are f ..., and the Write specified is not one Variables, but a set ..., this is a custom process. The definition of the process as: Procedure (Value: ) Because you need to do other things when you write this class properties, you can't use a variable. Processing should be handled in one process. This process is generally defined after protected. During this class, use a variable to the Tbitmap type to assign a value to the Tbitmap type, which is due to the variable of this type cannot be used directly. After defining the properties of the variables of these Tbitmap types, the CREATE process and the DESTROY process need to be written in the DESTROY process. Because Tbitmap is also a class, you must create ⑸ in the CREATE process, and you must release (free) during the DESTROY process. The inherited statement referred to here is to indicate that the process is inherited from the ancestral class class. (This must not fall). Because we have written a visual control, you must draw on the control. Our ancestors of this control have a Canvas object in TGRAPHICCONTROL, we can use it directly to draw. If you are not familiar with the use of the canvas, it is best to find a book to see. The following to do is drawing. How to draw a picture on the control? There is a PAINT event in the ancestor TGRAPHICCONTROL, which will automatically trigger when the control needs to be redrawn. What we have to do now is to write a program for this incident. First define a Canvas object after protected. Since it is already in the ancestral class, there is no need to add any explanation. We will use this object to draw. Next, you want to define a PAINT process to write the code of the plot control. First define the PAINT process after the public. Since it is triggered by the ancestors, not by the user calls, it must be added override, otherwise, the control will never be called because the Paint process will never be called without a visual control. Let's write code ⑽ of the Paint process. The variables such as T_HEIGHT in the PAINT of this article are used to save the size of the button, the slider, etc. in the scroll bar, this part of the program and the ordinary application in the ordinary application are not big, most of them are Canvas for operation, I believe you will understand. It is worth noting that the determination of the FautSize variable is the case of the FautSize variable, and the Boolean variable associated with the attribute autosize of the control is used to set whether the size of this control varies with the size of the image. Note that in the code of the control, the attribute is generally called, but the variable corresponding to it is used. If the program is edited here, even if it is finally given a new control, but it can't scroll.

转载请注明原文地址:https://www.9cbs.com/read-4163.html

New Post(0)