This article mentioned that the content may have been seen in many places, the author is also the same, but also read a lot of VCL source code, plus the experience of the actual writing of components, and patching such an article. Therefore, all remarks is a personal point of view, and experience is described only for reference only.
You can reprint, copy, but you must join the author's signature Aweay. If you are used for commercial purposes, you must agree.
System Requirements
If you want to do it together, then you should look here, otherwise you can skip directly. C Builder6 Updata4 (Tools of God, BCB) Windows2k or Higher strongly recommended that you have a lot of problems in WinNT, BCB in Win9X, and very unstable, even if you don't care, there is A very deadly problem, the BCB help file is displayed under Win9x (because the BCB's help index keyword exceeds Win9x restrictions), which is difficult to refer to the help. Delphi6: ((necessary) What? Is it wrong, there is no mistake, if you have to go deep into the VCL to view the source code, there is no more suitable than using Delphi6, after installing Delphi6, put the VCL Source directory Join the Search Path so you can hold down the Ctrl key in the editor, click the mouse directly to jump to the source code is very convenient, more than what grep is much more.
Start
For the Message Mechanism of the VCL, you can refer to the cker http://www.9cbs.net/develop/read_article.asp?id=8131
I don't introduce it, but I am still very vague about writing components, and many times don't use those methods to handle the message, and what is the unique cm_xxxxxxxx message? How to join your own incident? I will do a detailed introduction in the discussion later.
Standing on the shoulders of the giant
The first thing to write components is to determine the problem we inherit from there. Choose a good ancestor class is the first step in writing a good component, so how to choose the stone of the mountains? The general rules are like this: 1. For display, you need to handle the keyboard event, and the components of the container are inherited from TCUSTOMCONTROL 2. For display, you need to do not process the keyboard event, you need to handle the mouse Event from TGRAPHICSCONTROL inheritance 3. For contacts without interface, similar to TopEndialog / TXPMenu inherited 4. If you want to extend a specified control, such as TPANEL, you can inherit from Tcustompanel, not from TPANEL Direct inheritance.
Note that the above paragraph 4, basically all components have the parent class of TCUSTOMXXX, which is also the inheritance object of VCL encouragement. The reason is that you can customize the visibility of component properties, the most important thing is that their constructor and destructor are Virtual.
This article mainly introduces the components of 1, 2 rules, and 3, 4 relatively simple, don't discuss it.
Draw oneself
The component is to be displayed on the form, must appear in a certain look, then you must draw yourself, everyone knows that it is possible to handle the WM_PAINT message, from the CKER's article, we can get a lot of methods to handle this news. For example: __ fastCall WndProc (TMESSAGE MSG) {Switch (MSG-> MSG) {CASE WM_Paint: // Our processing code ...} or simply use the message map, but these are not the best way.
From the components after TControl have a Paint this virtual method, we can automatically draw this method, which is equivalent to handling WM_Paint because: Procedure TgraphicControl.WmpAint (var message: twmpaint); begin if message.dc < > 0 dam.handle: = mess; fin or canvas.handle: = 0; end; finally canvas.unlock; end; end; end; The above code snippet illustrates this, According to the professional-level components studied, they are to draw themselves by overloading this function.
Note The above code snippet is to use the method I mentioned above (Delphi6), it is not very affordable, ^ _ ^.
In the Paint party we can freely draw, in the back article, I will pay for how to draw high efficiency.
In many cases, we need to redraw yourself. For example, I have to give netizens a few days ago, when the width of the line changes, we must redraw yourself, otherwise it will not reflect the change of the attribute, I have seen many friends using repaint ( ) Method, this is not the best way, we should use invalidate (), why? Leave it to everyone to see the source code, even if you review the topic above. Code Demo: Void __fastcall tline :: setLineWidth (int value) {// Todo: add your source code here {flinewidth! = Value) {flinewidth = value; invalidate ();}}