In-depth C ++ Builder writing your own components-in-depth analysis of VCL inheritance and message mechanism (1)

Source: Internet
Author: User

The content mentioned in this article may have been seen in many places, and the author has also read a lot of VCL source code, and his own experiences in writing components, piece together such an article. Therefore, all comments are personal opinions and experience descriptions for your reference only.

You can reprint and copy the data, but you must add the author's signature Aweay. If it is used for commercial purposes, it must be approved by the author.

System Requirements

If you want to do it together, you should take a look here, otherwise you can skip it directly.
C ++ Builder6 + updata4 (God creation tool, BCB)
Windows2k or higher (required)
The author strongly recommends that you use WinNT. BCB has many problems in Win9x and is unstable. Even if you don't care about this, there is a very fatal problem, the BCB help file is not completely displayed Under Win9x (because the number of BCB Help Index keywords exceeds the limit of Win9x), which is very difficult for reference.
Delphi6 (required)
What
? If you want to go deep into VCL to view the source code, there is no more suitable solution than using Delphi6. After all Delphi6 is installed
Add the Source directory to the Search Path so that you can press Ctrl in the editor and click the mouse to jump directly to the Source code, which is much easier than grep.

Start

For the message mechanism of VCL, you can refer to
Http://www.csdn.net/develop/read_article.asp? Id = 8131

I will not introduce the repeated content, but the message mechanism above is vague for writing components, and many times it is not used to process messages, what else is the unique CM_XXXXXXXX message of the component? How can I add my own events? I will discuss these issues in detail later.

Standing on the shoulders of giants

The first thing to write components is to determine the issues we inherit from. Selecting a good ancestor class is the first step to write a good component. How can we choose another stone? General rules are as follows:
1. For interface display, the keyboard event must be handled, and not the container components inherit from TCustomControl.
2. For interface display, if you do not need to handle keyboard events, you need to handle mouse events inherited from TGraphicsControl.
3. If no interface is displayed, controls like TOpenDialog/TXpMenu inherit from TComponent
4. If you want to extend a specified control, such as TPanel, you 'd better inherit from TCustomPanel instead of from TPanel.

Note that the above 4th rules basically all components have a TCustomXXX parent class, which is also an inherited object encouraged by VCL because you can customize the visibility of component attributes, most importantly, their constructor and destructor are virtual.

This article mainly introduces the components of rules 1 and 2, which are relatively simple and will not be discussed in depth.

Draw yourself

The component must appear in a certain way on the form, so you must draw yourself. Everyone knows that you can process the WM_PAINT message, we can come up with many methods to process this message, such:
_ Fastcall WndProc (TMessage msg)
{
Switch (msg-> msg)
{
Case WM_PAINT:
// Our processing code
...
}

You can also simply use the macro for message ing, but these are not the best methods.

All components after TControl have the virtual method of painting. We only need to reload this method to automatically draw, which is equivalent to processing wm_painting, because:
Procedure TGraphicControl. WMPaint (var Message: TWMPaint );
Begin
If Message. DC <> 0 then
Begin
Canvas. Lock;
Try
Canvas. Handle: = Message. DC;
Try
Paint;
Finally
Canvas. Handle: = 0;
End;
Finally
Canvas. Unlock;
End;
End;
End;
The code snippet above illustrates this. According to the professional components I have studied, this function is reloaded to draw my own.

Note that the above code snippet is obtained by pressing the left mouse button several times using the method mentioned above (with delphi6 installed). Is it very affordable? ^ _ ^.

In the painting, we can draw it freely. In the subsequent articles, I will show you how to draw it efficiently.

In many cases, we need to re-paint ourselves. For example, when the width of the line changes, we must re-paint ourselves for the underlined component I made to netizens a few days ago. Otherwise, the attribute changes cannot be reflected, I have seen many of my friends use
Repaint () method. This is not the best method. We should use Invalidate (). Why? Let's take a look at the source code, even if we review the above knowledge.
Code Demonstration:
Void _ fastcall TLine: SetLineWidth (int value)
{
// TODO: Add your source code here
If (FLineWidth! = Value)
{
FLineWidth = value;
Invalidate ();
}
}

Related Article

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.