J'ai toujours entendu dire que la toile à Delphi est simple et pratique à utiliser. Maintenant, j'utilise la toile pour implémenter une représentation graphique d'un simple mécanisme d'arbre. Le système prend en charge la sélection des nœuds, le mouvement, les arbres de sauvegarde, les arbres d'ouverture, etc. La récursivité et les pointeurs sont utilisés pour la commodité.
Le programme est écrit en désordre, bienvenue pour communiquer: [email protected]
Le code source est le suivant:
unité u_tree;
interface
usages
Windows, messages, systèmes, classes, graphiques, contrôles, formulaires, dialogues,
Extctrls, stdctrls, jpeg, menus, inifiles32;
taper
Tobj = enregistrer
Objid: String;
CenterX: entier;
CENTERE: INTEGER;
TYPENO: entier;
Sélectionné: Boolean;
FNode: String;
montré: booléen;
fin;
Tfrm_tree = classe (tform)
Panel1: Tpanel;
Paintbox1: TPAINTBOX;
Panel2: Tpanel;
Label1: Tlabel;
Button2: TBUTTON;
Button1: Tbutton;
Button3: Tbutton;
Button4: Tbutton;
Button5: Tbutton;
Button6: Tbutton;
MainMenu1: TmainMenu;
Fadestream1: tMenuItem;
RandomrandomSelection1: tMenuItem;
SavEDialog1: Tsavedialog;
OpenDialog1: TOPDENDIAG;
Button7: Tbutton;
Procédure Paintbox1Moudown (expéditeur: tobject; bouton: tmousebutton;
Shift: tshiftstate; x, y: entier);
Procédure FormCreate (expéditeur: tobject);
Procédure Button1Click (expéditeur: tobject);
Procédure Button2Click (expéditeur: tobject);
Procédure Paintbox1paint (expéditeur: tobject);
Procédure Button3Click (expéditeur: tobject);
Procédure Button4Click (expéditeur: tobject);
Procédure Button5Click (expéditeur: tobject);
Procédure Button6Click (expéditeur: tobject);
Procédure Paintbox1MouseUp (Sender: Tobject; Bouton: TMouseButton;
Shift: tshiftstate; x, y: entier);
Procédure Paintbox1MousMove (expéditeur: Tobject; Shift: TshiftState; x,
Y: entier);
Procédure fadestream1click (expéditeur: tobject);
procédure randomrandomselection1click (expéditeur: tobject);
Procédure Button7Click (expéditeur: tobject);
Privé
{Déclarations privées}
ToolNo: entier;
débutx, début, endx, endy: entier;
Cliquez sur: Boolean;
OLST: tlist;
Selid: String;
Racine: booléen;
Searilid: entier;
Procédure DrawNode (ID: String);
Procédure addObj (id: string; x, y: entier; typeno: entier; sélectionné: booléen; fnode: chaîne; montré: booléen);
fonction getOBj (id: string): tobj;
fonction getpobj (id: string): pointeur;
Fonction GetSelect: Tobj;
Fonction Haveselect: Boolean;
fonction ClickObj (x, y: entier): chaîne;
Procédure Drawfull;
Procédure setSelected (x, y: entier);
fonction setShowsel (x, y: entier): tobj;
procédure setFnode (id: string);
procédure setCnode (id: string);
procédure claire;
procédure clearcanvas;
Procédure MoveObj (dx, dy: entier);
Procédure me momeNode (dx, dy: entier; id: string);
procédure movelocal (dx, dy: entier);
//procédure
publique
{Déclarations publiques}
fin;
var
Frm_tree: tfrm_tree;
Mise en œuvre
{$ R * .dfm}
{Tform1}
Procédure tfrm_tree.Drawnode (id: string);
var
OldBrushColor: TCOLOR;
OldPencolor: Tcolor;
OBJ: Tobj;
Commencer
obj: = getObj (id);
avec frm_tree.paintbox1.Canvas faire
Commencer
Si obj.showed alors
Commencer
OldBrushColor: = Brush.Color;
OldPencolor: = Pen.Color;
Si obj.selected alors
Commencer
Pen.color: = RGB (255,0,0);
fin;
Brush.Color: = 00FF31FF;
Ellipse (obj.centerx-10, obj.centery-10, obj.centerx + 10, obj.centery + 10);
Pen.color: = 00FF31FF;
Si obj.typeno> 0 alors
Commencer
MOVETO (obj.centerx, obj.centery);
lineTo (getOBj (obj.fnode) .centerx, getOBJ (obj.fnode) .Centery);
fin;
Pen.Color: = OldPencolor;
Brush.Color: = OldBrushColor;
fin;
fin;
fin;
Procédure tfrm_tree.paintbox1mousedown (expéditeur: tobject;
Bouton: TMouseButton;
var
Curobj: Tobj;
Commencer
Si bouton = mbleft alors
Commencer
tools de cas de
1:
Commencer
Searilid: = Searilid + 1;
Si root alors
Commencer
AddObj (inttoStr (Searilid), x, y, 0, false, '', true);
DrawNode (intToStr (Searilid));
Root: = false;
fin
autre
Commencer
Si haveselect alors
Commencer
AddObj (inttoStr (Searilid), x, y, 1, false, getSelect.objid, true);
DrawNode (intToStr (Searilid));
label1.caption: = 'Ajouter le nœud, id:' + intToStr (Searilid);
fin
autre
Commencer
label1.caption: = 'Veuillez sélectionner le nœud!';
fin;
fin;
fin;
2:
Commencer
SetSelected (x, y);
fin;
3: // Voir
Commencer
// ClearCanvas;
curobj: = setsshowsel (x, y);
Si curobj.objid <> '' alors
Commencer
ClearShowed;
curobj: = setsshowsel (x, y);
curobj.showed: = true;
setFnode (curobj.fnode);
setCnode (curobj.objid);
Drawfull;
fin;
fin;
4: // mobile
Commencer
Si clickoBj (x, y) <> '' puis cliquez sur: = true;
débutX: = x;
Beginy: = y;
fin;
5:
Commencer
Si clickoBj (x, y) <> '' puis cliquez sur: = true;
débutX: = x;
Beginy: = y;
fin;
fin;
fin
autre
Commencer
SetSelected (x, y);
fin;
fin;
Procédure tfrm_tree.formCreate (expéditeur: tobject);
Commencer
Olst: = tlist.create;
ToolNo: = 0;
Root: = true;
Selid: = '';
Searilid: = 0;
cliqué: = false;
avec Paintbox1.Canvas faire
Commencer
Brush.color: = clwhite;
FillRect (REct (0,0, peinturebox1.width, pewpox1.height));
fin;
fin;
Procédure tfrm_tree.button1Click (expéditeur: tobject);
Commencer
ToolNo: = 1;
fin;
procédure tfrm_tree.button2click (expéditeur: tobject);
Commencer
TOLLONO: = 2;
fin;
Procédure tfrm_tree.addobj (id: string; x, y, typeno: entier;
Sélectionné: Boolean; Fnode: String;
var
Obj: ^ tabj;
Commencer
nouveau (obj);
obj.objid: = id;
obj.centerx: = x;
OBJ.Centery: = Y;
obj.typeno: = typeno;
obj.selected: = sélectionné;
obj.fnode: = fnode;
obj.showed: = montré;
Olst.add (obj);
fin;
fonction tfrm_tree.getObj (id: string): tobj;
var
I, J: entier;
Commencer
J: = Olst.Count;
pour i: = 0 à j-1 do
Commencer
Si Tobj (olst.items [i] ^). objid = id alors
Commencer
Résultat: = tobj (olst.items [i] ^);
Casser;
fin;
fin;
fin;
fonction tfrm_tree.getSelect: tobj;
var
I, J: entier;
Commencer
J: = Olst.Count;
pour i: = 0 à j-1 do
Commencer
si Tobj (olst.items [i] ^). Sélectionné alors
Commencer
Résultat: = tobj (olst.items [i] ^);
Casser;
fin;
fin;
fin;
fonction tfrm_tree.haveselect: boolean;
var
I, J: entier;
Commencer
Résultat: = false;
J: = Olst.Count;
pour i: = 0 à j-1 do
Commencer
si Tobj (olst.items [i] ^). Sélectionné alors
Commencer
Résultat: = true;
Casser;
fin;
fin;
fin;
Procédure tfrm_tree.Drawfull;
var
I, J: entier;
Commencer
//Paintbox1.canvas.fillrect(rect(0,0,paintbox1.width,paintbox1.height));
ClearCanvas;
J: = Olst.Count;
pour i: = 0 à j-1 do
Commencer
Drawnode (tobj (olst.items [i] ^). Objid);
fin;
fin;
Procédure tfrm_tree.paintbox1paint (expéditeur: tobject);
Commencer
Drawfull;
fin;
procédure tfrm_tree.setselected (x, y: entier);
var
I, J: entier;
Commencer
J: = Olst.Count;
pour i: = 0 à j-1 do
Commencer
Tobj (olst.items [i] ^). Selected: = false;
if (tobj (olst.items [i] ^). CenterX-10 <x) et (tobj (olst.items [i] ^). CenterX + 10> x)
et (tobj (olst.items [i] ^). Centery-10 <y) et (tobj (olst.items [i] ^).
Commencer
Tobj (olst.items [i] ^). Selected: = true;
Label1.caption: = 'sélectionné l'ID de nœud:' + tobj (olst.items [i] ^). Objid;
fin;
fin;
Drawfull;
fin;
Procédure tfrm_tree.button3Click (expéditeur: tobject);
Commencer
ToolNo: = 3;
fin;
fonction tfrm_tree.setShowsel (x, y: entier): tobj;
var
I, J: entier;
Commencer
J: = Olst.Count;
pour i: = 0 à j-1 do
Commencer
Tobj (olst.items [i] ^). Selected: = false;
if (tobj (olst.items [i] ^). CenterX-10 <x) et (tobj (olst.items [i] ^). CenterX + 10> x)
et (tobj (olst.items [i] ^). Centery-10 <y) et (tobj (olst.items [i] ^).
Commencer
Tobj (olst.items [i] ^). Montré: = true;
Label1.caption: = 'Regardez le nœud ID:' + tobj (olst.items [i] ^). Objid;
Résultat: = tobj (olst.items [i] ^);
Casser;
fin;
fin;
fin;
procédure tfrm_tree.clelatehowed;
var
I, J: entier;
Commencer
J: = Olst.Count;
pour i: = 0 à j-1 do
Commencer
Tobj (olst.items [i] ^). A montré: = false;
fin;
fin;
procédure tfrm_tree.setfnode (id: string);
var
Curobj: ^ Tobj;
Commencer
Si id <> '' alors
Commencer
// new (curobj);
curobj: = getPobj (id);
tandis que curobj ^ .typeno = 1 do
Commencer
curobj ^ .showed: = true;
curobj: = getPobj (curobj ^ .fnode);
fin;
curobj ^ .showed: = true;
// disposer (curobj);
fin;
fin;
procédure tfrm_tree.setcnode (id: string);
var
Curobj: ^ Tobj;
I, J: entier;
Commencer
// curobj: = getObj (id);
J: = Olst.Count;
pour i: = 0 à j-1 do
Commencer
Si Tobj (olst.items [i] ^). fnode = id alors
Commencer
curobj: = getPobj (tobj (olst.items [i] ^). objid);
curobj ^ .showed: = true;
setCnode (curobj ^ .objid);
fin;
fin;
fin;
Procédure tfrm_tree.clearCanvas;
Commencer
//Paintbox1.canvas
Pendingbox1.canvas.fillrect (rect (0,0, pendingbox1.width, pendingbox1.height));
fin;
Procédure tfrm_tree.button4click (expéditeur: tobject);
Commencer
cliqué: = false;
Pendingbox1.canvas.fillrect (rect (0,0, pendingbox1.width, pendingbox1.height));
Olst.clear;
Root: = true;
Selid: = '';
Searilid: = 0;
{avec Paintbox1.Canvas faire
Commencer
Pen.Width: = 2;
Pen.Color: = Clblack;
Pen.style: = psClear;
Brush.style:=bssolid;
Brush.color: = clwhite;
Rectangle (0,0, peinture1.width, pewpox1.height);
fin;}
fin;
procédure tfrm_tree.button5click (expéditeur: tobject);
var
I, J: entier;
Commencer
J: = Olst.Count;
pour i: = 0 à j-1 do
Commencer
Tobj (olst.items [i] ^). Montré: = true;
fin;
Drawfull;
fin;
fonction tfrm_tree.getpobj (id: string): pointeur;
var
I, J: entier;
Commencer
Résultat: = nil;
J: = Olst.Count;
pour i: = 0 à j-1 do
Commencer
Si Tobj (olst.items [i] ^). objid = id alors
Commencer
Résultat: = olst.items [i];
Casser;
fin;
fin;
fin;
fonction tfrm_tree.clickobj (x, y: entier): chaîne;
var
I, J: entier;
Commencer
Résultats: = '';
J: = Olst.Count;
SetSelected (x, y);
pour i: = 0 à j-1 do
Commencer
if (tobj (olst.items [i] ^). CenterX-10 <x) et (tobj (olst.items [i] ^). CenterX + 10> x)
et (tobj (olst.items [i] ^). Centery-10 <y) et (tobj (olst.items [i] ^).
Commencer
Label1.caption: = 'cliquez sur l'ID de nœud:' + tobj (olst.items [i] ^). Objid;
Résultat: = tobj (olst.items [i] ^). Objid;
Casser;
fin;
fin;
fin;
procédure tfrm_tree.button6click (expéditeur: tobject);
Commencer
ToolNo: = 4;
fin;
Procédure tfrm_tree.moveObj (dx, dy: entier);
var
I, J: entier;
Commencer
J: = Olst.Count;
pour i: = 0 à j-1 do
Commencer
Tobj (olst.items [i] ^). CenterX: = tobj (olst.items [i] ^). CenterX + dx;
Tobj (olst.items [i] ^). Centery: = tobj (olst.items [i] ^). Centery + Dy;
fin;
// Drawfull;
fin;
Procédure tfrm_tree.paintbox1mouseup (expéditeur: tobject; bouton: tmouseButton;
Shift: tshiftstate; x, y: entier);
Commencer
tools de cas de
4:
Commencer
Si vous cliquez alors alors
Commencer
endx: = x;
endy: = y;
MOVEOBJ ((endx-beginx), (endy-beginy));
fin;
cliqué: = false;
fin;
5:
Commencer
cliqué: = false;
fin;
fin;
fin;
Procédure tfrm_tree.paintbox1mousmove (expéditeur: tobject; shift: tshiftState;
X, y: entier);
Commencer
si (cliquez sur) alors
Commencer
tools de cas de
4:
Commencer
MOVEOBJ ((X-Beginx), (Y-Beginy));
débutX: = x; début: = y;
Drawfull;
fin;
5:
Commencer
morenode ((x-beginx), (y-beginy), getSelect.objid);
moveLocal ((x-beginx), (y-beginy));
débutX: = x; début: = y;
Drawfull;
fin;
fin;
fin;
fin;
Procédure tfrm_tree.fadestream1Click (expéditeur: tobject);
var
Selfil: String;
Curid: String;
Curobj: Tobj;
lstdate: tinifile32;
I, J: entier;
Commencer
J: = Olst.Count;
Si SaveDedialog1.Exécute alors
Commencer
Selfil: = SAVEDIALOG1.FILENAME;
lstdate: = tinifile32.create (Selfil + '. dat');
lstdate.writeInteger ('title', 'num', j);
pour i: = 0 à j-1 do
Commencer
curobj: = tobj (olst.items [i] ^);
curid: = curobj.objid;
lstdate.writeString (curid, 'objid', curobj.objid);
lstdate.writeInteger (Curid, 'CenterX', curobj.centerx);
lstdate.writeInteger (Curid, «Centery», Curobj.Centery);
lstdate.writeInteger (curid, «typeno», curobj.typeno);
lstdate.writebool (curid, 'sélectionné', curobj.selected);
lstdate.writeString (curid, 'fnode', curobj.fnode);
lstdate.writebool (Curid, «montré», curobj.showed);
fin;
fin;
fin;
procédure tfrm_tree.randomrandomselection1click (expéditeur: tobject);
var
Selfil: String;
// Curid: String;
lstdate: tinifile32;
I, J: entier;
Commencer
Si openDialog1.excute alors
Commencer
Selfil: = OpenDialog1.FileName;
cliqué: = false;
Pendingbox1.canvas.fillrect (rect (0,0, pendingbox1.width, pendingbox1.height));
Olst.clear;
Root: = true;
Selid: = '';
Searilid: = 0;
lstdate: = tinifile32.Create (Selfil);
j: = lstdate.readInteger ('title', 'num', 0);
pour i: = 1 à j do
Commencer
addObj (lStDate.readString (intToStr (i), 'objid', ''), lstdate.readInteger (intToStr (i), 'CenterX', 0), lstDate.readInteger (IntToStr (i), 'Centery', 0) , lstdate.readInteger (intToStr (i), 'typeno', 0), lStDate.readbool (intToStr (i), 'Selecte lstdate.readbool (intToStr (i), «montré», true));
fin;
Searilid: = J;
Root: = false;
Drawfull;
fin;
fin;
procédure tfrm_tree.button7click (expéditeur: tobject);
Commencer
ToolNo: = 5;
fin;
Procédure tfrm_tree.Movenode (dx, dy: entier; id: chaîne);
var
I, J: entier;
Curobj: ^ Tobj;
Commencer
J: = Olst.Count;
pour i: = 0 à j-1 do
Commencer
Si Tobj (olst.items [i] ^). fnode = id alors
Commencer
curobj: = getPobj (tobj (olst.items [i] ^). objid);
Curobj ^ .Centerx: = Curobj ^ .Centerx + dx;
Curobj ^ .Centery: = Curobj ^ .Centery + Dy;
MOVENODE (DX, DY, CUROBJ ^ .OBJID);
fin;
fin;
fin;
procédure tfrm_tree.movelocal (dx, dy: entier);
var
I, J: entier;
// Curobj: Tobj;
Commencer
J: = Olst.Count;
pour i: = 0 à j-1 do
Commencer
Si Tobj (olst.items [i] ^). Sélectionné alors
Commencer
tobj (olst.items [i] ^). CenterX: = tobj (olst.items [i] ^). CenterX + dx;
tobj (olst.items [i] ^). Centery: = tobj (olst.items [i] ^). Centery + Dy;
Casser;
fin;
fin;
fin;
fin.