Learn to view VCL source code (1)

Source: Internet
Author: User
Tags call back
Learn VCL source code (1) Finger 12:45:13 reply to Top Modification and Deletion

People who are new to Delphi generally only know the Drag Control and write some event code.
To learn about Delphi, you must read the source code of VCL. As the first article, I will introduce the simplest ttimer Control
Encapsulation, starting with the simplest, allows you to have a basic understanding of VCL encapsulation.
Next, go to the topic, open Delphi, drag a ttimer control, switch to the Code view, and find
Timer1: ttimer definition. Press ctrl and click ttimer To Go To The ttimer class definition code of the extctrls unit. First
It is inherited directly from tcomponent, to see the definition of private
Finterval: Cardinal; // trigger interval of the timer
Fwindowhandle: hwnd; // component handle
Fontimer: tpolicyevent; // ontimer event Function
Fenabled: Boolean; // whether the timer is available or not
Procedure updatetimer; // update the timer method, which is described in detail below
Procedure setenabled (value: Boolean); // Set Method of the enabled attribute
Procedure setinterval (value: Cardinal); // Set Method of the interval attribute
Procedure setontimer (value: tpolicyevent); // Set Method of the ontimer event
Procedure wndproc (var msg: tmessage); // Message Processing Function
The definition of protected is a timer method. Most of the definitions of protected are used for subclass inheritance.
Is a dynamic method (the difference between dynamic and virtual is simply in the storage space and
For different considerations about the row speed, it is okay to know that it is the same as the virtual method)
The constructor and constructor methods in public are not much said. As long as the allocatehwnd function and deallocatehwnd function are known
To obtain a handle and release the resource.
The two attributes and one event of published are clear to everyone. We will not mention them here. The following mainly introduces the following two functions.
The wndproc function processes ttimer message loops. This control only has one wm_timer message. After receiving this message
The code that runs the timer dynamic function, while timer calls the ontimer event processing code written by the user.
Procedure ttimer. wndproc (var msg: tmessage );
Begin
With MSG do
If MSG = wm_timer then
Try
Timer;
Except
Application. handleexception (Self );
End
Else
Result: = defwindowproc (fwindowhandle, MSG, wparam, lparam );
End;
The updatetimer function uses two API functions settimer and killtimer. The settimer function has four parameters,
On msdn, we can find the handle, id value, trigger interval, and pointer to the processing function. settimer function is successful.
If the function pointer of the last parameter is null, A wm_timer is sent every interval of interval milliseconds.
The window handle specified by the message to the first interval parameter. The second parameter is used only to set multiple times for the same window handle.
Because the ttimer class is re-allocated with an hwnd when it is created, it is directly used here.
Constant 1. When the final parameter of settimer is not blank, Windows will call back this function first. killtimer will destroy the specified
Timer.
Procedure ttimer. updatetimer;
Begin
Killtimer (fwindowhandle, 1 );
If (finterval <> 0) and fenabled and assigned (fontimer) then
If settimer (fwindowhandle, 1, finterval, nil) = 0 then
Raise eoutofresources. Create (snotimers );
End;
From the code, we can see that no matter which attribute or event of ttimer is modified (in fact, only 2 attributes and 1 event ),
First killtimer and then settimer to generate a new timer.

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.