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
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
http://blog.9cbs.net/chenbin165/