Eu sempre ouvi dizer que a tela em Delphi é simples e conveniente de usar. Agora eu uso a tela para implementar uma representação gráfica de um mecanismo de árvore simples. O sistema suporta seleção, movimento, salvamento de árvores, abertura de árvores, etc. Recursão e ponteiros são usados para conveniência.
O programa está escrito em uma bagunça, bem -vindo ao comunicado: [email protected]
O código -fonte é o seguinte:
unidade u_tree;
interface
usos
Windows, mensagens, sysutils, aulas, gráficos, controles, formulários, diálogos,
Extctrls, stdctrls, jpeg, menus, iniFiles32;
tipo
Tobj = registro
Objid: string;
Centerx: Inteiro;
Centery: Inteiro;
Typeno: Inteiro;
Selecionado: booleano;
Fnode: string;
mostrou: booleano;
fim;
Tfrm_tree = classe (tform)
Painel1: tpanel;
PaintBox1: TPaintBox;
Painel2: tpanel;
Label1: Tlabel;
Button2: tbutton;
Button1: tbutton;
Button3: tbutton;
Button4: tbutton;
Button5: tbutton;
Button6: tbutton;
MAINMENU1: TMAINMENU;
FadeStream1: tmenuitem;
Randomrandomselection1: tmenuitem;
Savedialog1: TSAVedialog;
OpenDialog1: TOPENDIALOG;
Button7: tbutton;
Procedimento PaintBox1MouseDown (remetente: Tobject; Button: TmouseButton;
Turno: tshiftState;
Procedimento FormCreate (remetente: Tobject);
Procedimento Button1Click (remetente: Tobject);
Procedimento Button2Click (remetente: Tobject);
Procedimento PaintBox1Paint (remetente: Tobject);
Procedimento Button3Click (remetente: Tobject);
Procedimento Button4Click (remetente: Tobject);
Procedimento Button5Click (remetente: TOBJECT);
Procedimento Button6Click (remetente: Tobject);
Procedimento PaintBox1MouseUp (remetente: Tobject; Button: TmouseButton;
Turno: tshiftState;
Procedimento PaintBox1MouSEMove (remetente: Tobject; Shift: TSHIFTSTATE; X,
Y: número inteiro);
Procedimento FadeStream1Click (remetente: TOBJECT);
procedimento RandomRandomSelection1Click (remetente: TOBJECT);
Procedimento Button7Click (remetente: TObject);
Privado
{Declarações privadas}
Toolno: Inteiro;
BEGINX, BELKY, ENDX, ENDY: INTEGER;
Clicou: booleano;
Olst: tlist;
Selid: String;
Raiz: booleano;
Searilid: Inteiro;
procedimento desenhado (id: string);
Procedimento addObj (id: string; x, y: número inteiro; typeno: número inteiro; selecionado: boolean; fnode: string; mostrou: booleano);
function getObj (id: string): Tobj;
função getpobj (id: string): ponteiro;
função getSelect: Tobj;
função HAVESELECT: boolean;
função clickobj (x, y: inteiro): string;
procedimento desenhado;
Procedimento setSelected (x, y: número inteiro);
Função setShowsel (x, y: inteiro): Tobj;
procedimento setFNode (id: string);
Procedimento setCNode (id: string);
procedimento Clearshowed;
Procedimento ClearCanvas;
procedimento moveobj (dx, dy: número inteiro);
Procedimento Movenode (DX, DY: Inteiro; ID: String);
procedimento movelocal (dx, dy: inteiro);
//procedimento
público
{Declarações públicas}
fim;
var
Frm_tree: tfrm_tree;
Implementação
{$ R *.dfm}
{TFform1}
procedimento tfrm_tree.Drawnode (id: string);
var
Oldbrushcolor: tcolor;
OldPencolor: Tcolor;
obj: Tobj;
Começar
obj: = getObj (id);
com frm_tree.paintbox1.Canvas faça
Começar
Se obj..
Começar
OldBrushColor: = Brush.color;
OldPencolor: = Pen.color;
se obj. selecionado então
Começar
Pen.Color: = RGB (255,0,0);
fim;
Pincel.color: = $ 00FF31FF;
Ellipse (obj.centerx-10, obj.centery-10, obj.centerx+10, obj.centery+10);
Pen.Color: = $ 00FF31FF;
Se obj.typeno> 0 então
Começar
moveto (obj.centerx, obj.centery);
lineto (getobj (obj.fnode) .centerx, getobj (obj.fnode) .centery);
fim;
Pen.Color: = OldPencolor;
pincel.color: = OldBrushColor;
fim;
fim;
fim;
procedimento tfrm_tree.paintbox1mousedown (remetente: tabject;
Botão: TmouseButton;
var
Curobj: Tobj;
Começar
se botão = mbleft então
Começar
ferramenta de casono de
1:
Começar
Searilid: = Searilid+1;
Se raiz então
Começar
Addobj (inttoStr (Searilid), x, y, 0, false, '', verdadeiro);
DrawNode (inttostr (Searilid));
Raiz: = false;
fim
outro
Começar
Se hasveselect, então
Começar
Addobj (inttoStr (Searilid), x, y, 1, falso, getSelect.objid, true);
DrawNode (inttostr (Searilid));
LABEL1.CAPTION: = 'Adicione o nó, ID:'+inttostr (Searilid);
fim
outro
Começar
Label1.Caption: = 'Selecione o nó!';
fim;
fim;
fim;
2:
Começar
setSelected (x, y);
fim;
3: // View
Começar
// clearcanvas;
CUROBJ: = setShowsel (x, y);
Se Curobj.objid <> '' então
Começar
ClearShowed;
CUROBJ: = setShowsel (x, y);
Curobj.Showed: = true;
setfnode (Curobj.fnode);
setCNode (Curobj.objid);
Desenhado;
fim;
fim;
4: // celular
Começar
Se clickobj (x, y) <> '' então clique: = true;
beginx: = x;
começo: = y;
fim;
5:
Começar
Se clickobj (x, y) <> '' então clique: = true;
beginx: = x;
começo: = y;
fim;
fim;
fim
outro
Começar
setSelected (x, y);
fim;
fim;
procedimento tfrm_tree.formCreate (remetente: tabjas);
Começar
OlST: = tlist.create;
Ferramentano: = 0;
Raiz: = true;
Selid: = '';
Searilid: = 0;
Clicou: = false;
com o PaintBox1.Canvas faça
Começar
pincel.color: = clwhite;
Fillrect (Rect (0,0, PaintBox1.Width, PaintBox1.Height));
fim;
fim;
procedimento tfrm_tree.button1Click (remetente: tabjas);
Começar
Ferramentano: = 1;
fim;
procedimento tfrm_tree.button2Click (remetente: tabjas);
Começar
Ferramentano: = 2;
fim;
procedimento tfrm_tree.addobj (id: string; x, y, typeno: integer;
selecionado: booleano;
var
Obj: ^Tobj;
Começar
novo (obj);
obj.objid: = id;
obj.centerx: = x;
obj.centery: = y;
obj.typeno: = typeno;
obj.selected: = selecionado;
obj.fnode: = fnode;
obj.showed: = mostrado;
Olst.add (obj);
fim;
função tfrm_tree.getObj (id: string): Tobj;
var
I, J: Inteiro;
Começar
j: = olst.count;
para i: = 0 a j-1 fazer
Começar
Se Tobj (olST.Items [i]^). objid = id então
Começar
Resultado: = Tobj (olSt.items [i]^);
Quebrar;
fim;
fim;
fim;
função tfrm_tree.getSelect: Tobj;
var
I, J: Inteiro;
Começar
j: = olst.count;
para i: = 0 a j-1 fazer
Começar
Se Tobj (olST.Items [i]^). Selecionado então
Começar
Resultado: = Tobj (olSt.items [i]^);
Quebrar;
fim;
fim;
fim;
função tfrm_tree.haveselect: boolean;
var
I, J: Inteiro;
Começar
Resultado: = false;
j: = olst.count;
para i: = 0 a j-1 fazer
Começar
Se Tobj (olST.Items [i]^). Selecionado então
Começar
Resultado: = true;
Quebrar;
fim;
fim;
fim;
procedimento tfrm_tree.drawfull;
var
I, J: Inteiro;
Começar
//PaintBox1.Canvas.FillRect(rect(0,0,PaintBox1.Width.PaintBox1.Height));
ClearCanvas;
j: = olst.count;
para i: = 0 a j-1 fazer
Começar
Desenhado (tobj (olst.items [i]^). Objid);
fim;
fim;
procedimento tfrm_tree.paintbox1paint (remetente: tabjas);
Começar
Desenhado;
fim;
procedimento tfrm_tree.setSelected (x, y: número inteiro);
var
I, J: Inteiro;
Começar
j: = olst.count;
para i: = 0 a j-1 fazer
Começar
Tobj (olST.Items [i]^). Selecionado: = false;
if (tobj (olst.items [i]^). Centerx-10 <x) e (Tobj (olSt.items [i]^). Centerx+10> x)
e (Tobj (olST.Items [i]^). Centery-10 <y) e (tobj (olst.items [i]^). Centery+10> y) então
Começar
Tobj (olST.Items [i]^). Selecionado: = true;
LABEL1.CAPTION: = 'Selecionado o ID do nó:'+ Tobj (olSt.items [i]^). Objid;
fim;
fim;
Desenhado;
fim;
procedimento tfrm_tree.button3Click (remetente: tabjas);
Começar
Ferramentano: = 3;
fim;
function tfrm_tree.setshowsel (x, y: inteiro): Tobj;
var
I, J: Inteiro;
Começar
j: = olst.count;
para i: = 0 a j-1 fazer
Começar
Tobj (olST.Items [i]^). Selecionado: = false;
if (tobj (olst.items [i]^). Centerx-10 <x) e (Tobj (olSt.items [i]^). Centerx+10> x)
e (Tobj (olST.Items [i]^). Centery-10 <y) e (tobj (olst.items [i]^). Centery+10> y) então
Começar
Tobj (olST.Items [i]^). Mostrou: = true;
LABEL1.CAPTION: = 'Procure o ID do nó:'+ Tobj (olST.items [i]^). Objid;
Resultado: = Tobj (olSt.items [i]^);
Quebrar;
fim;
fim;
fim;
procedimento tfrm_tree.clearshowed;
var
I, J: Inteiro;
Começar
j: = olst.count;
para i: = 0 a j-1 fazer
Começar
Tobj (olST.Items [i]^). Mostrou: = false;
fim;
fim;
procedimento tfrm_tree.setfnode (id: string);
var
Curobj:^Tobj;
Começar
se id <> '' então
Começar
// novo (Curobj);
Curobj: = getpobj (id);
enquanto Curobj^.Typeno = 1 do
Começar
Curobj^.Showed: = true;
CUROBJ: = getpobj (Curobj^.fNode);
fim;
Curobj^.Showed: = true;
// descarte (Curobj);
fim;
fim;
procedimento tfrm_tree.setcnode (id: string);
var
Curobj:^Tobj;
I, J: Inteiro;
Começar
// curobj: = getobj (id);
j: = olst.count;
para i: = 0 a j-1 fazer
Começar
Se Tobj (olST.Items [i]^). fnode = id então
Começar
Curobj: = getpobj (tobj (olst.items [i]^). objid);
Curobj^.Showed: = true;
setCNode (Curobj^.Objid);
fim;
fim;
fim;
procedimento tfrm_tree.clearCanvas;
Começar
//Paintbox1.canvas
PaintBox1.Canvas.FillRect (Rect (0,0, PaintBox1.Width, PaintBox1.Height));
fim;
procedimento tfrm_tree.button4Click (remetente: tabjas);
Começar
Clicou: = false;
PaintBox1.Canvas.FillRect (Rect (0,0, PaintBox1.Width, PaintBox1.Height));
Olst.clear;
Raiz: = true;
Selid: = '';
Searilid: = 0;
{com pintaindbox1.canvas
Começar
Pen.Width: = 2;
PEN.COLOR: = Clblack;
Pen.style: = psclear;
Pincel.style:=bssolid;
Pincel.color: = clwhite;
Retângulo (0,0, pinturbox1.width, pintaindbox1.height);
fim;}
fim;
procedimento tfrm_tree.button5Click (remetente: tabjas);
var
I, J: Inteiro;
Começar
j: = olst.count;
para i: = 0 a j-1 fazer
Começar
Tobj (olST.Items [i]^). mostrou: = true;
fim;
Desenhado;
fim;
function tfrm_tree.getpobj (id: string): pointer;
var
I, J: Inteiro;
Começar
Resultado: = nil;
j: = olst.count;
para i: = 0 a j-1 fazer
Começar
Se Tobj (olST.Items [i]^). objid = id então
Começar
Resultado: = olst.items [i];
Quebrar;
fim;
fim;
fim;
function tfrm_tree.Clickobj (x, y: inteiro): string;
var
I, J: Inteiro;
Começar
Resultados: = '';
j: = olst.count;
setSelected (x, y);
para i: = 0 a j-1 fazer
Começar
if (tobj (olst.items [i]^). Centerx-10 <x) e (Tobj (olSt.items [i]^). Centerx+10> x)
e (Tobj (olST.Items [i]^). Centery-10 <y) e (tobj (olst.items [i]^). Centery+10> y) então
Começar
LABEL1.CAPTION: = 'Clique no nó ID:'+ Tobj (olST.items [i]^). Objid;
Resultado: = Tobj (olSt.items [i]^). Objid;
Quebrar;
fim;
fim;
fim;
procedimento tfrm_tree.button6Click (remetente: tabjas);
Começar
Ferramentano: = 4;
fim;
procedimento tfrm_tree.moveobj (dx, dy: inteiro);
var
I, J: Inteiro;
Começar
j: = olst.count;
para i: = 0 a j-1 fazer
Começar
Tobj (olST.Items [i]^). Centerx: = Tobj (olSt.items [i]^). Centerx+dx;
Tobj (olST.items [i]^).
fim;
// drawfull;
fim;
procedimento tfrm_tree.paintbox1mouseup (remetente: tabjas; botão: tmouseButton;
Turno: tshiftState;
Começar
ferramenta de casono de
4:
Começar
Se clicar, então
Começar
endX: = x;
endy: = y;
moveobj ((endx-beginx), (endy-beginy));
fim;
Clicou: = false;
fim;
5:
Começar
Clicou: = false;
fim;
fim;
fim;
procedimento tfrm_tree.paintbox1mouseMove (remetente: tabject; shift: tShiftState;
X, y: número inteiro);
Começar
se (clicado) então
Começar
ferramenta de casono de
4:
Começar
moveobj ((x-beginx), (y-beginy));
beginx: = x; iniciar: = y;
Desenhado;
fim;
5:
Começar
MoveNode ((x-beginx), (y-beginy), getSelect.objid);
movelocal ((x-beginx), (y-beginy));
beginx: = x; iniciar: = y;
Desenhado;
fim;
fim;
fim;
fim;
procedimento tfrm_tree.fadeStream1Click (remetente: tabjas);
var
selfile: string;
curid: string;
Curobj: Tobj;
LSTDATE: TINIFILE32;
I, J: Inteiro;
Começar
j: = olst.count;
se savedialog1.execute então
Começar
Selfile: = savedialog1.FileName;
lstdate: = tinifile32.create (selfile+'. dat');
lstdate.writeInteger ('title', 'num', j);
para i: = 0 a j-1 fazer
Começar
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, 'Selected', Curobj.Selected);
lstdate.writestring (curid, 'fnode', Curobj.fnode);
lstdate.WriteBool (Curid, 'mostrou', Curobj.Showed);
fim;
fim;
fim;
procedimento tfrm_tree.randomrandomSelection1Click (remetente: tabject);
var
selfile: string;
// curid: string;
LSTDATE: TINIFILE32;
I, J: Inteiro;
Começar
Se OpenDialog1.Execute então
Começar
selfile: = opendialog1.filename;
Clicou: = false;
PaintBox1.Canvas.FillRect (Rect (0,0, PaintBox1.Width, PaintBox1.Height));
Olst.clear;
Raiz: = true;
Selid: = '';
Searilid: = 0;
lstdate: = tinifile32.create (selfile);
j: = lstdate.readInteger ('title', 'num', 0);
para i: = 1 a j faz
Começar
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), 'selecionado', true), lstdate.readstring (inttostr (i), 'fnode', ''), lstdate.readbool (inttostr (i), 'mostrou', verdadeiro));
fim;
Searilid: = j;
Raiz: = false;
Desenhado;
fim;
fim;
procedimento tfrm_tree.button7Click (remetente: tabjas);
Começar
Ferramentano: = 5;
fim;
procedimento tfrm_tree.movenode (dx, dy: inteiro; id: string);
var
I, J: Inteiro;
Curobj:^Tobj;
Começar
j: = olst.count;
para i: = 0 a j-1 fazer
Começar
Se Tobj (olST.Items [i]^). fnode = id então
Começar
Curobj: = getpobj (tobj (olst.items [i]^). objid);
CUROBJ^.CENTERX: = CUROBJ^.CENTERX+DX;
CUROBJ^.CENTERY: = CUROBJ^.CENTERY+DY;
MoveNode (DX, Dy, Curobj^.Objid);
fim;
fim;
fim;
procedimento tfrm_tree.movelocal (dx, dy: número inteiro);
var
I, J: Inteiro;
// Curobj: Tobj;
Começar
j: = olst.count;
para i: = 0 a j-1 fazer
Começar
Se Tobj (olST.Items [i]^). Selecionado então
Começar
Tobj (olST.Items [i]^). Centerx: = Tobj (olSt.items [i]^). Centerx+dx;
Tobj (olST.items [i]^).
Quebrar;
fim;
fim;
fim;
fim.