Delphi implements drag and change of the control part during running, and decouples the control type.

Source: Internet
Author: User

Method 1: Use a custom control to inherit an existing control in Delphi, such as panel

Then rewrite its events such as mousedown, mousemove, and mouseup.

Disadvantage: This method can only achieve the effect of a certain control, with high dependence and low reusability.

Method 2: Use the idesignhook interface provided by Delphi to edit the existing control when it is switched to the design stage.

Help information in Delphi:

Idesignerhook is an interface that allows component writers to interact with the Form Designer in the IDE.

Unit

Forms

Description

Idesignerhook provides access to the form currently under construction in the Form Designer, and influences the behavior of the designer by specifying whether the object under construction is a form (as opposed a composite component ), drawing the grid that assists in layout, and so on.

Component writers can access the current idesignerhook interface using the designer property of a form.

We use methods in the implementation interface to design controls during runtime.

Here, we must first switch the runtime controller to the design time. In this case, the protected method of tcomponent has a setdesigning. The descriptions in the help section of Delphi are as follows:

Ensures that components inserted at design time have their design-mode flag set.

Delphi Syntax:

Procedure setdesigning (value: Boolean; setchildren: Boolean = true );

What is the protection method? Protected, oh, no, isn't the external class inaccessible? There is a clever way to crack it. We use youyuan to crack it. in Delphi, the classes in the same unit are mutual youyuan classes, but we cannot write our own programs into the Delphi library, so we can inherit from it, in the unit to call it, inherit it and write a tcrackcomponent = Class (tcomponent) end;

Then, for the object to call this method, convert it to tcrackcomponent first, for example

Tcrackcomponent, (form). setdesigning (false, true );

When switching to the design stage, we will find that this is actually just changing a label, indicating that this control is designed, and there is no change on the surface. Now let's go back to the idesignhook interface. In this interface, we need to implement an isdesignmsg method.

The help is described as follows:

Determines when the designer shocould handle a Windows message.

Delphi Syntax:

Function isdesignmsg (Sender: tcontrol; var message: tmessage): Boolean;

Description

Isdesignmsg is called for each message sent to a component in the designer. This method returns true if the message is a design message, meaning one the designer shocould handle for the component.

All the messages of the control are passed into this method in this interface, and whether to intercept is determined by the return value. If it is true, it is intercepted. For better understanding, we enter the control unit file, find the wndproc Method

Here, it first obtains the form to which the control belongs. If the control belongs to a form, the message is sent to the form. designer is an object that implements the idesignhook interface. If isdesignmsg returns true, the message is intercepted.

Through the above instructions, we also know that this form. if no value is assigned to the designer, the message will not be intercepted. Therefore, we need to write the content of the control operation to a class that implements the idesignhook interface and assign the value to the designer.

Then, the isdesignmsg in this class processes the message, for example, intercepting all mouse and keyboard events.

What should I do if I want to design only the controls in a form control? Let's take a look at the source code in tcomponent. setdesigning.

Procedure tcomponent. setdesigning (value, setchildren: Boolean );

VaR

I: integer;

Begin

If value then

Include (fcomponentstate, csdesigning) else

Exclude (fcomponentstate, csdesigning );

If setchildren then

For I: = 0 to componentcount-1 do components [I]. setdesigning (value );

End;

It uses comonents to set all the controls it manages to true or false. That is to say, only the sub-built owner is the control you want to operate on can you set it as the design time. What should I do? We can also imitate its recursion and traverse the child control in the control. The Code is as follows:

Procedure setdesigning (root: twincontrol );

VaR I: integer;

Begin

Tcrackcomponent (Root). setdesigning (true, false );

For I: = 0 to root. ControlCount-1 do

Begin

Setdesigning (twincontrol (root. controls [I]);

End;

End;

I am grateful to wr960204 for his article on this topic, which has benefited me a lot. This article is based on his understanding, summary, and improvement.

 

Author: DJ dance

Welcome to reprint, reprint please indicate the source of http://djbone.cnblogs.com

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.