C ++ Builder master advancement (3) Designing DBTreeView components with BCB

zhaozj2021-02-16  38

C builder master advanced

nxyc_twz@163.com

I bought this book "C Builder Depth Adventures", and regardless of this writing, the author's unique writing angle makes me. So I have an idea that I can write my experience in software development into a series of arts into a series of articles on 9CBS, which can not only improve their programming levels, but also with many masters on 9CBS. How do you talk to each other? In my writing plan, there will be a series of content including "C Builder Master Advanced", "InterBase Skills", "ODAC Use Detailed". The "C Builder Master Advanced" will mainly explain the application of the API function, write components with BCB, and the application in the MIS system, etc .; "Interbase Skills" (my favorite database server) is mainly Tell the various skills; "ODAC uses detailed explanation" (the preferred choice when using BCB is connected to Oracle) is mainly based on the help of translation ODAC, and can supplement the skills I am practical. I hope my thoughts can get everyone's support!

(3) Designing the DBTREEVIEW component with BCB

First, system analysis

Now use BCB, more and more, but you have a real thing, how many of your controls are written in C ? The answer will definitely make people can't stand it. Since it is unable to endure how to write a set of components belonging to BCB? I hope that from me, the masters can work together to create a component that really belongs to BCB's own! I don't want to tell the basics of BCB design components because they have a detailed introduction in the C Builder 5 Developer Guide.

Our DBTreeView is derived from the TtreeView component, so it will have all the features of the TTREEVIEW component, which we have to do is to have data perception, direct communication with data directly. To construct the TREE-shaped structure, we should make each node feature: Each node has a unique identifier ID and a parent ID ParentID, (the top-level node is empty) In order to display the content, it is also required to Display field fdisplayfield. The working principle of this component is very clear: through the ID of each node and the ParentID positioning data, then assign the relevant field content to the FDISPLAYFIELD display.

Second, the development frontier

The heaviest technology in this component is not in the data. Here, the data-aware technology is introduced:

To make a component become data perception, we must give it a required data connection to communicate with data training data, this data connection class is TFieldDataLink. Data perceived components have their own data connection classes, and data connection is responsible for creating, initializing, and destroying components. Establishing a connection usually takes 3 steps:

1. State the data connection class (tfielddataLink) to a member of the component;

2. Disclaimer properly, write access attributes;

3. Initialize the data connection.

Third, design process

// DBTREEVIEW header file

/ Add the following three lines in the header file, because / data connection classes need declarations in these three headers

#include

#include

#include

// Define an event to respond to setting a node bitmap event

Typedef void __fastcall (__closure * tdbtvsetimageIndexevent) (System :: Tobject * Sender, int ID, int parentid, int tent, int & imageindex, int & selectedindex); // Define node structure

Struct TDBTREENODATA

{

INT ID; // node unique identifier

INT PARENTID; // Node The parent logo, when it is empty, this node is the top node

}

Class Package TdbtreeView: Public TtreeView

{

Private:

TfieldDataLink * fdataLink; // Declaration data connection member

Ansistring fparentidfield; // parent identity field

Ansistring fprimaryidfield; // Main signature field

Ansistring fdisplayfield; // Display field

BOOL FACTIVE; / / Whether is an active state

BOOL FALLOWMODIFYDB; / / Do not modify

TDBTVSetImageIndexEvent FonseTimageIndex; // Declaration Event

Void __fastcall setActive (bool value); // setting status

Void __fastcall setDataSource (DB :: TDataSource * Value); // Setting the data source

TDataSource * __fastcall getDataSource (); // get data source

Void __fastcall clearllnodes (); // Delete all nodes

Void __fastcall fixtreenodes (int parentid, ttreenode * node); // Fill node

Void __fastcall FillChildtreenodes (int ParentID, Ttreenode * node, Bool Nest = false); // Fill a child node

Void __fastcall adddtonode (ttreenode * node, tdbtreenodedata & data); // Add data to node

protected:

/ / The following method is used to override TreeView by one method.

Virtual void __fastcall loaded (void); // load method

Virtual void __fastcall notification (classes :: tcomponent * Acomponent, classes :: top); // event

Dynamic void __fastcall edit (const tagtvitema & item); // edit method

Dynamic void __fastcall expand (ttreenode * node); // Expand method

Dynamic void __fastcall keydown (word & key, classes :: tshiftstate shift); / / keyboard event

Dynamic void __fastcall change (ttreenode * node); // node change event

Virtual Bool_fastcall CustomDrawItem (Ttreenode * Node, TcustomDrawState Stage, Bool & PaintImages); // Auto Node Event

PUBLIC:

__fastcall tdbtreeview (tComponent * Owner); // Constructor

__fastcall ~ tdbtreeView (); // Destructure function TDBTREENODEDATA __FASTCALL GETNODEDATA (TTREENODE * NODE); // Get specified node data

Void __fastcall fullxpand (void); // Expand all nodes

__Property Bool Active = {Read = Factive, Write = SetActive}; // Attribute Activity Status

__publish:

// The following attributes defined in the Properties Editor

__Property Bool AllowModifyDB = {Read = FallowModifyDB, Write = FallowModifyDB}; // Whether to allow editing

__property ansistring parentidfield = {read = fparentidfield, Write = fparentidfield}; // parent logo

__Property Ansistring PrimaryidField = {Read = fprimaryidfield, Write = fprimaryidfield}; // Master ID

__Property Ansistring DisplayField = {read = fdisplayfield, Write = fdisplayfield}; // Display field

__property tdataasource * DataSource = {read = getDataSource, Write = setDataSource}; // data source

__property tdbtvsetimageIndexevent oversetimageindex = {read = FonseTimageIndex, Write = FonseTImageIndex}; // Setting Node Bitchart Event

}

// The following class is used to fill the list list

Class TfieldNameProperty: Public TStringProperty

{

PUBLIC:

TPROPERTYATTRIBUTES __FASTCALL GETATITRIBUTES (VOID)

{

Return tpropertyAttributes () << pavaluelist;

}

Void __fastcall getValues ​​(Classes :: TgetstrProc PROC);

}

"Not complete"

转载请注明原文地址:https://www.9cbs.com/read-26451.html

New Post(0)