Main Methods of components (methods)
Most components have more than 20 public methods, and window components have more than 40 public methods to choose from. Interestingly, not all of them are widely used. Most functions of a component are completed by attributes. For example, to hide a component, you can call its hide method or set the visible attribute to false. In addition, a component usually has a clear method for its purpose. It may be the most commonly used method for processing individual components.
However, there are several methods worth mentioning, listing them below. Note that some methods are not available to all controls. This is not the most common method for each component, but the most common method for components.
- BroadcastUsed to send messages to all window sub-components
- ClienttoscreenConvert customer zone coordinates to screen coordinates
- ContainscontrolReturns true if a control belongs to the current control.
- HandleallocatedIf a system handle is assigned to the control, true is returned. If no system handle is assigned, a handle is automatically created after the handle attribute is read. Therefore, handleallocated is used to check the existence of the handle, instead of creating it.
- HideHide controls
- InvalidateForce Control re-painting
- PerformSend messages directly to components without passing through windows message system
- RefreshRequires that the component be re-painted immediately and erased before being re-colored
- RepaintThe component is required to be re-painted immediately, and the background of the component is not erased before being re-colored.
- SetboundsYou can set the top, left, width, and height attributes at one time by changing the position and size of the control, which saves time for individual settings.
- SetfocusGive control input focus
- UpdateIf you have a pending draw command, redraw the control immediately.
Next, let's take a look at some events that the component is most likely to respond.
Common events)
Like attributes and methods, some events are most frequently responded. The most commonly used events are as follows:
- OnchangeOccurs when the object or its data changes
- OnclickWhen you left-click a widget
- OndblclickWhen you double-click a widget
- OnenterWhen the component is activated, that is, when the component receives the focus
- OnexitWhen the component loses focus
- OnkeydownWhen a user presses a key on the keyboard, the event is sent by a component with the input focus.
- OnkeypressWhen a user presses a key, the event is sent to the component with the input focus.
- OnkeyupWhen a user releases a key, the event is sent to the component with the input focus.
- OnmousedownWhen a user presses a mouse key, it sends a message to the component where the mouse cursor is located.
- OnmousemoveWhen a user moves the mouse over a widget, the cursor is sent to the widget where the cursor is located.
- OnmouseupWhen a user releases a mouse key, it sends a message to the component where the mouse cursor is located.
- OnpaintWhen the component needs to be re-colored
Process mouse events
Mouse events have a special set. If a component wants to respond to mouse clicks, the simplest thing is to respond to the onclick event. If onmousedown and onmouseup are required, you should note that onclick events are also sent when onmousedown and onmouseup are sent. For example, if you click to cause a series of events, the event response sequence is as follows:
Onmousedown
Onclick
Onmouseup
Similarly, when you double-click a component with the mouse, more events may occur in the application. When the component is double-clicked, the following events are displayed:
Onmousedown
Onclick
Ondblclick
Onmouseup
It must be noted that when the response component has two double-click events and click events, be careful. Note that the double-click event generates four events.
When you press a key on a keyboard, multiple event responses also occur. For example, pressing a key in the edit component causes the onkeydown, onkeypress, onchange, and onkeyup events.
Next we will compile a program to confirm the fact that multiple events occur when the mouse clicks and the buttons.
1. Create an application and click 【File | new | Application];
2. Change the name attribute of form1Mainfrom;
3. Place a memo component on the form and modify the name attributeTestmemoDouble-click the lines attribute to set text content, for example, set the align attributeAlleft;
4. Place a memo component on the form again, and set the name attribute to eventsmemo. Double-click the lines attribute to set the text content, for example, set the align attribute to alclient;
5. Add another timer component on the form and set the name attributeTimer, Set its enabled attributeTrue, Set interval3000That is, a response is sent once every 3 seconds. The current form looks as follows:
5. Double-click the timer component to bring up the code editor and write the ontimer event. The Code is as follows:
Procedure tmainform. timertimer (Sender: tobject); begin testmemo. lines. Clear; {clear the text in testmemo} end;
6. Click the testmemo component, select the events tag from the object inspector, and double-click the onmousedown, onmouseup, onclick, ondblclick, onchange, onkeydown, onkeypress, and onkeyup events. Enter the following code:
procedure TMainForm.TestMemoMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);begin EventsMemo.Lines.Add('OnMouseDown');end;procedure TMainForm.TestMemoMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);begin EventsMemo.Lines.Add('OnMouseUp');end;procedure TMainForm.TestMemoClick(Sender: TObject);begin EventsMemo.Lines.Add('OnClick');end;procedure TMainForm.TestMemoDblClick(Sender: TObject);begin EventsMemo.Lines.Add('OnDblClick');end;procedure TMainForm.TestMemoChange(Sender: TObject);begin EventsMemo.Lines.Add('OnChange');end;procedure TMainForm.TestMemoKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);begin EventsMemo.Lines.Add('OnKeyDown');end;procedure TMainForm.TestMemoKeyPress(Sender: TObject; var Key: Char);begin EventsMemo.Lines.Add('OnKeyPress');end;procedure TMainForm.TestMemoKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);begin EventsMemo.Lines.Add('OnKeyUp');end;
7. At this point, our test program has been completed. Let's test it and see how multiple events occur.
8. Click the mouse in testmemo to generate the following event sequence:
9. Double-click the mouse in testmemo. The event sequence is as follows:
10. When we press the keyboard in testmemo, the sequence of events is as follows:
The above code passes the test in Delphi 7. Download the sample code:Test multiple important events .rar