How to write your own visual control with Delphi Select BLOG from Chenbin165

xiaoxiao2021-03-06  44

How to write your own visual control (Visual Component) actually is a class (Class), to write a class, you can write directly 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 (small pig horizontal scroll bar). 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. Now let's write mouse events and let us manipulate it. The definition of the process of the mouse event is very similar, but the back should be added to the parameter description, and the mouse event is divided into three, and the Override is added later after defining. Next, write its code later. Note: The mouse event here is mouse ..., not usually onmouse .... But what is the definition of the ⒀ What is the use? The event definition here is to use it to the user, that is, when using the control, it is displayed in the EVENT page in the Object Inspector. The code of these mouse events is also very simple, judge the coordinates of the mouse, draw the corresponding pictures on the canvas, and triggered the corresponding events. It is worth noting that when the custom event is called, the user has to determine whether the user has written code for the event. This is very important, otherwise it will call an error. Everyone noted that the incident just called is customized, the definition method is also very simple, and the definition attribute is similar, just when the type is TNOTIFYEVENT. TNOTIFYEVENT is the default event that is defined as: tnotifyeverEvent = procedure (sender: TOBJECT) If you want to define additional formal events, you must do this: first write = Procedure first after Type first (), for example: tcustoment = procedure (A: Integer; B: String); then defined after public: : , for example: Anevent: tcustomT; After reading these, this whole program you should understand Let it be. If you compile or run an error, pay attention to check the following: 1, whether there is inherited statement during Create and Destroy; 2, Tbitmap type variable Create and Free; Methods to determine if the mouse enters or leave the control: Define the following procedure: Procedure MouseEnter; Message Cm_MouseEnter; Procedure MouseEleAve; Message Cm_MouseLeave; Write the code below. This method is useful for writing a three-state button. Author Blog:

http://blog.9cbs.net/chenbin165/

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

New Post(0)