ฉันเคยได้ยินเสมอว่าผ้าใบใน Delphi นั้นง่ายและสะดวกในการใช้งาน ตอนนี้ฉันใช้ผืนผ้าใบเพื่อใช้การแสดงกราฟิกของกลไกต้นไม้ง่ายๆ ระบบรองรับการเลือกโหนดการเคลื่อนไหวการออมต้นไม้เปิดต้นไม้ ฯลฯ การเรียกซ้ำและพอยน์เตอร์ใช้เพื่อความสะดวก
โปรแกรมนี้เขียนขึ้นอย่างยุ่งเหยิงยินดีต้อนรับสู่การสื่อสาร: [email protected]
ซอร์สโค้ดมีดังนี้:
หน่วย u_tree;
ส่วนต่อประสาน
ใช้
Windows, ข้อความ, sysutils, คลาส, กราฟิก, การควบคุม, รูปแบบ, กล่องโต้ตอบ
extctrls, stdctrls, jpeg, เมนู, inifiles32;
พิมพ์
TOBJ = บันทึก
objid: สตริง;
centerx: จำนวนเต็ม;
Centery: จำนวนเต็ม;
Typeno: จำนวนเต็ม;
เลือก: บูลีน;
fnode: สตริง;
แสดง: บูลีน;
จบ;
tfrm_tree = คลาส (tform)
Panel1: tpanel;
PaintBox1: tpaintbox;
Panel2: tpanel;
label1: tlabel;
button2: tbutton;
ปุ่ม 1: tbutton;
ปุ่ม 3: tbutton;
ปุ่ม 4: tbutton;
ปุ่ม 5: Tbutton;
ปุ่ม 6: tbutton;
Mainmenu1: tmainmenu;
Fadestream1: tmenuitem;
RandomRandomSelection1: tmenuitem;
SAVEDIALOG1: TSAVEDIALOG;
Opendialog1: topendialog;
ปุ่ม 7: tbutton;
Procedure PaintBox1Mousedown (ผู้ส่ง: tobject; ปุ่ม: tmouseButton;
SHIFT: TSHIFTSTATE;
โพรซีเดอร์แบบฟอร์ม (ผู้ส่ง: tobject);
ขั้นตอน button1click (ผู้ส่ง: tobject);
ขั้นตอน button2click (ผู้ส่ง: tobject);
Procedure PaintBox1Paint (ผู้ส่ง: tobject);
ขั้นตอน button3click (ผู้ส่ง: tobject);
ขั้นตอน button4click (ผู้ส่ง: tobject);
ขั้นตอน button5click (ผู้ส่ง: tobject);
ขั้นตอน button6click (ผู้ส่ง: tobject);
Procedure PaintBox1MouseUp (ผู้ส่ง: tobject; ปุ่ม: tmouseButton;
SHIFT: TSHIFTSTATE;
Procedure Paintbox1mousemove (ผู้ส่ง: tobject; shift: tshiftstate; x, x,
y: จำนวนเต็ม);
ขั้นตอน fadestream1click (ผู้ส่ง: tobject);
ขั้นตอนการสุ่ม RandomSelection1Click (ผู้ส่ง: tobject);
ขั้นตอน button7click (ผู้ส่ง: tobject);
ส่วนตัว
{ประกาศส่วนตัว}
Toolno: จำนวนเต็ม;
bEGINX, เริ่มต้น, endx, endy: จำนวนเต็ม;
คลิก: บูลีน;
olst: tlist;
Selid: String;
รูต: บูลีน;
Searilid: จำนวนเต็ม;
ขั้นตอน DrawNode (ID: String);
โพรซีเดอร์ addobj (id: string; x, y: จำนวนเต็ม; typeno: จำนวนเต็ม; เลือก: บูลีน; fnode: สตริง; แสดง: บูลีน);
ฟังก์ชั่น getObj (id: string): TOBJ;
ฟังก์ชั่น getPoBJ (ID: String): ตัวชี้;
ฟังก์ชั่น getSelect: TOBJ;
ฟังก์ชั่น haveselect: บูลีน;
ฟังก์ชั่น clickobj (x, y: จำนวนเต็ม): สตริง;
ขั้นตอนการวาด
ขั้นตอนการตั้งค่า (x, y: จำนวนเต็ม);
ฟังก์ชั่น setshowsel (x, y: จำนวนเต็ม): TOBJ;
ขั้นตอน setFnode (ID: String);
ขั้นตอน setCnode (ID: String);
ขั้นตอน Clearshowed;
ขั้นตอน ClearCanvas;
ขั้นตอน MoveObj (dx, dy: จำนวนเต็ม);
ขั้นตอน movenode (dx, dy: จำนวนเต็ม; id: string);
ขั้นตอน movelocal (dx, dy: จำนวนเต็ม);
//ขั้นตอน
สาธารณะ
{ประกาศสาธารณะ}
จบ;
วาจา
frm_tree: tfrm_tree;
การดำเนินการ
{$ r *.dfm}
{tform1}
โพรซีเดอร์ tfrm_tree.drawnode (id: string);
วาจา
OldbrushColor: tcolor;
OldPencolor: TCOLOR;
OBJ: TOBJ;
เริ่ม
obj: = getobj (id);
ด้วย frm_tree.paintbox1.canvas do
เริ่ม
ถ้า obj.showed แล้ว
เริ่ม
OldbrushColor: = Brush.Color;
OldPencolor: = Pen.Color;
ถ้า obj.Selected แล้ว
เริ่ม
Pen.Color: = RGB (255,0,0);
จบ;
Brush.Color: = $ 00ff31ff;
วงรี (obj.centerx-10, obj.centery-10, obj.centerx+10, obj.centery+10);
Pen.Color: = $ 00ff31ff;
ถ้า obj.typeno> 0 แล้ว
เริ่ม
Moveto (obj.centerx, obj.centery);
lineto (getobj (obj.fnode) .centerx, getobj (obj.fnode) .centery);
จบ;
Pen.Color: = OldPencolor;
Brush.Color: = OldbrushColor;
จบ;
จบ;
จบ;
โพรซีเดอร์ tfrm_tree.paintbox1mousedown (ผู้ส่ง: tobject;
ปุ่ม: tmouseButton;
วาจา
CUROBJ: TOBJ;
เริ่ม
ถ้าปุ่ม = mbleft แล้ว
เริ่ม
เครื่องมือกรณีไม่มี
1:
เริ่ม
searilid: = searilid+1;
ถ้ารูทนั้น
เริ่ม
addobj (inttostr (searilid), x, y, 0, false, '', true);
DrawNode (inttoStr (searilid));
รูท: = เท็จ;
จบ
อื่น
เริ่ม
ถ้าเลือกแล้ว
เริ่ม
addobj (inttostr (searilid), x, y, 1, false, getselect.objid, true);
DrawNode (inttoStr (searilid));
label1.caption: = 'เพิ่มโหนด, id:'+inttoStr (searilid);
จบ
อื่น
เริ่ม
label1.caption: = 'โปรดเลือกโหนด!';
จบ;
จบ;
จบ;
2:
เริ่ม
setSelected (x, y);
จบ;
3: // ดู
เริ่ม
// ClearCanvas;
curobj: = setShowsel (x, y);
ถ้า curobj.objid <> '' แล้ว
เริ่ม
Clearshowed;
curobj: = setShowsel (x, y);
curobj.showed: = true;
setFnode (curobj.fnode);
setcnode (curobj.objid);
Drawfull;
จบ;
จบ;
4: // มือถือ
เริ่ม
ถ้า clickobj (x, y) <> '' จากนั้นคลิก: = true;
beginx: = x;
เริ่มต้น: = y;
จบ;
5:
เริ่ม
ถ้า clickobj (x, y) <> '' จากนั้นคลิก: = true;
beginx: = x;
เริ่มต้น: = y;
จบ;
จบ;
จบ
อื่น
เริ่ม
setSelected (x, y);
จบ;
จบ;
ขั้นตอน tfrm_tree.formcreate (ผู้ส่ง: tobject);
เริ่ม
olst: = tlist.create;
Toolno: = 0;
รูท: = จริง;
selid: = '';
searilid: = 0;
คลิก: = false;
ด้วย paintbox1.canvas ทำ
เริ่ม
Brush.Color: = ClWhite;
Fillrect (rect (0,0, paintbox1.width, paintbox1.height));
จบ;
จบ;
ขั้นตอน tfrm_tree.button1click (ผู้ส่ง: tobject);
เริ่ม
Toolno: = 1;
จบ;
ขั้นตอน tfrm_tree.button2click (ผู้ส่ง: tobject);
เริ่ม
Toolno: = 2;
จบ;
ขั้นตอน tfrm_tree.addobj (id: string; x, y, typeno: จำนวนเต็ม;
เลือก: บูลีน;
วาจา
obj: ^Tobj;
เริ่ม
ใหม่ (obj);
obj.objid: = id;
obj.centerx: = x;
obj.centery: = y;
obj.typeno: = typeno;
obj.Selected: = เลือก;
obj.fnode: = fnode;
obj.showed: = แสดง;
olst.add (obj);
จบ;
ฟังก์ชั่น tfrm_tree.getobj (id: string): tobj;
วาจา
ฉัน, J: จำนวนเต็ม;
เริ่ม
J: = olst.count;
สำหรับ i: = 0 ถึง J-1 ทำ
เริ่ม
ถ้า Tobj (olst.items [i]^). objid = id แล้ว
เริ่ม
ผลลัพธ์: = tobj (olst.items [i]^);
หยุดพัก;
จบ;
จบ;
จบ;
ฟังก์ชั่น tfrm_tree.getSelect: TOBJ;
วาจา
ฉัน, J: จำนวนเต็ม;
เริ่ม
J: = olst.count;
สำหรับ i: = 0 ถึง J-1 ทำ
เริ่ม
ถ้า Tobj (olst.items [i]^) เลือกแล้ว
เริ่ม
ผลลัพธ์: = tobj (olst.items [i]^);
หยุดพัก;
จบ;
จบ;
จบ;
ฟังก์ชั่น tfrm_tree.haveselect: บูลีน;
วาจา
ฉัน, J: จำนวนเต็ม;
เริ่ม
ผลลัพธ์: = เท็จ;
J: = olst.count;
สำหรับ i: = 0 ถึง J-1 ทำ
เริ่ม
ถ้า Tobj (olst.items [i]^) เลือกแล้ว
เริ่ม
ผลลัพธ์: = จริง;
หยุดพัก;
จบ;
จบ;
จบ;
ขั้นตอน tfrm_tree.drawfull;
วาจา
ฉัน, J: จำนวนเต็ม;
เริ่ม
//paintbox1.canvas.fillrect(rect(0,0,0,0,0,0,0,0,0,0, paintbox1.width, paintbox1.height));
Clearcanvas;
J: = olst.count;
สำหรับ i: = 0 ถึง J-1 ทำ
เริ่ม
DrawNode (Tobj (olst.items [i]^). objid);
จบ;
จบ;
โพรซีเดอร์ tfrm_tree.paintbox1paint (ผู้ส่ง: tobject);
เริ่ม
Drawfull;
จบ;
ขั้นตอน TFRM_TREE.SetSelected (x, y: จำนวนเต็ม);
วาจา
ฉัน, J: จำนวนเต็ม;
เริ่ม
J: = olst.count;
สำหรับ i: = 0 ถึง J-1 ทำ
เริ่ม
Tobj (olst.items [i]^). เลือก: = false;
if (tobj (olst.items [i]^). centerx-10 <x) และ (tobj (olst.items [i]^). centerx+10> x)
และ (tobj (olst.items [i]^). Centery-10 <y) และ (tobj (olst.items [i]^). Centery+10> y)
เริ่ม
Tobj (olst.items [i]^). เลือก: = true;
label1.caption: = 'เลือกรหัสโหนด:'+ TOBJ (olst.items [i]^). objid;
จบ;
จบ;
Drawfull;
จบ;
ขั้นตอน tfrm_tree.button3click (ผู้ส่ง: tobject);
เริ่ม
Toolno: = 3;
จบ;
ฟังก์ชั่น tfrm_tree.setshowsel (x, y: จำนวนเต็ม): tobj;
วาจา
ฉัน, J: จำนวนเต็ม;
เริ่ม
J: = olst.count;
สำหรับ i: = 0 ถึง J-1 ทำ
เริ่ม
Tobj (olst.items [i]^). เลือก: = false;
if (tobj (olst.items [i]^). centerx-10 <x) และ (tobj (olst.items [i]^). centerx+10> x)
และ (tobj (olst.items [i]^). Centery-10 <y) และ (tobj (olst.items [i]^). Centery+10> y)
เริ่ม
Tobj (olst.items [i]^). แสดง: = true;
label1.caption: = 'ดูโหนด ID:'+ TOBJ (olst.items [i]^). objid;
ผลลัพธ์: = tobj (olst.items [i]^);
หยุดพัก;
จบ;
จบ;
จบ;
ขั้นตอน tfrm_tree.clearshowed;
วาจา
ฉัน, J: จำนวนเต็ม;
เริ่ม
J: = olst.count;
สำหรับ i: = 0 ถึง J-1 ทำ
เริ่ม
Tobj (olst.items [i]^). แสดง: = false;
จบ;
จบ;
โพรซีเดอร์ tfrm_tree.setfnode (id: string);
วาจา
CUROBJ:^TOBJ;
เริ่ม
ถ้า id <> '' แล้ว
เริ่ม
// ใหม่ (curobj);
curobj: = getPoBJ (id);
ในขณะที่ curobj^.typeno = 1 ทำ
เริ่ม
curobj^. showed: = true;
curobj: = getPoBJ (cUROBJ^.fnode);
จบ;
curobj^. showed: = true;
// กำจัด (curobj);
จบ;
จบ;
โพรซีเดอร์ TFRM_TREE.SetCnode (ID: String);
วาจา
CUROBJ:^TOBJ;
ฉัน, J: จำนวนเต็ม;
เริ่ม
// curobj: = getobj (id);
J: = olst.count;
สำหรับ i: = 0 ถึง J-1 ทำ
เริ่ม
ถ้า TOBJ (olst.items [i]^). fnode = id แล้ว
เริ่ม
curobj: = getPoBJ (TOBJ (olst.Items [i]^). objid);
curobj^. showed: = true;
setcnode (curobj^.objid);
จบ;
จบ;
จบ;
ขั้นตอน tfrm_tree.clearcanvas;
เริ่ม
//paintbox1.canvas
Paintbox1.canvas.fillrect (rect (0,0, paintbox1.width, paintbox1.height));
จบ;
ขั้นตอน tfrm_tree.button4click (ผู้ส่ง: tobject);
เริ่ม
คลิก: = false;
Paintbox1.canvas.fillrect (rect (0,0, paintbox1.width, paintbox1.height));
Olst.Clear;
รูท: = จริง;
selid: = '';
searilid: = 0;
{ด้วย paintbox1.canvas ทำ
เริ่ม
Pen.Width: = 2;
Pen.Color: = Clblack;
Pen.style: = psclear;
Brush.style:=bssolid;
Brush.Color: = ClWhite;
สี่เหลี่ยมผืนผ้า (0,0, paintbox1.width, paintbox1.height);
จบ;}
จบ;
ขั้นตอน tfrm_tree.button5click (ผู้ส่ง: tobject);
วาจา
ฉัน, J: จำนวนเต็ม;
เริ่ม
J: = olst.count;
สำหรับ i: = 0 ถึง J-1 ทำ
เริ่ม
Tobj (olst.items [i]^). แสดง: = true;
จบ;
Drawfull;
จบ;
ฟังก์ชั่น tfrm_tree.getpobj (id: string): ตัวชี้;
วาจา
ฉัน, J: จำนวนเต็ม;
เริ่ม
ผลลัพธ์: = ไม่มี;
J: = olst.count;
สำหรับ i: = 0 ถึง J-1 ทำ
เริ่ม
ถ้า Tobj (olst.items [i]^). objid = id แล้ว
เริ่ม
ผลลัพธ์: = olst.items [i];
หยุดพัก;
จบ;
จบ;
จบ;
ฟังก์ชั่น tfrm_tree.clickobj (x, y: จำนวนเต็ม): สตริง;
วาจา
ฉัน, J: จำนวนเต็ม;
เริ่ม
ผลลัพธ์: = '';
J: = olst.count;
setSelected (x, y);
สำหรับ i: = 0 ถึง J-1 ทำ
เริ่ม
if (tobj (olst.items [i]^). centerx-10 <x) และ (tobj (olst.items [i]^). centerx+10> x)
และ (tobj (olst.items [i]^). Centery-10 <y) และ (tobj (olst.items [i]^). Centery+10> y)
เริ่ม
label1.caption: = 'คลิกที่โหนด ID:'+ TOBJ (olst.items [i]^). objid;
ผลลัพธ์: = tobj (olst.items [i]^). objid;
หยุดพัก;
จบ;
จบ;
จบ;
ขั้นตอน tfrm_tree.button6click (ผู้ส่ง: tobject);
เริ่ม
Toolno: = 4;
จบ;
ขั้นตอน tfrm_tree.moveobj (dx, dy: จำนวนเต็ม);
วาจา
ฉัน, J: จำนวนเต็ม;
เริ่ม
J: = olst.count;
สำหรับ i: = 0 ถึง J-1 ทำ
เริ่ม
Tobj (olst.items [i]^). centerx: = tobj (olst.items [i]^). centerx+dx;
Tobj (olst.items [i]^). Centery: = Tobj (olst.items [i]^). Centery+Dy;
จบ;
// drawfull;
จบ;
ขั้นตอน tfrm_tree.paintbox1mouseup (ผู้ส่ง: tobject; ปุ่ม: tmouseButton;
SHIFT: TSHIFTSTATE;
เริ่ม
เครื่องมือกรณีไม่มี
4:
เริ่ม
ถ้าคลิกแล้ว
เริ่ม
endx: = x;
Endy: = y;
MoveObj ((endx-beginx), (endy-beginy));
จบ;
คลิก: = false;
จบ;
5:
เริ่ม
คลิก: = false;
จบ;
จบ;
จบ;
โพรซีเดอร์ tfrm_tree.paintbox1mousemove (ผู้ส่ง: tobject; shift: tshiftstate;
x, y: จำนวนเต็ม);
เริ่ม
ถ้า (คลิก) จากนั้น
เริ่ม
เครื่องมือกรณีไม่มี
4:
เริ่ม
MoveObj ((x-beginx), (y-beginy));
beginx: = x; starty: = y;
Drawfull;
จบ;
5:
เริ่ม
Movenode ((x-beginx), (y-beginy), getselect.objid);
Movelocal ((x-beginx), (y-beginy));
beginx: = x; starty: = y;
Drawfull;
จบ;
จบ;
จบ;
จบ;
ขั้นตอน tfrm_tree.fadestream1click (ผู้ส่ง: tobject);
วาจา
Selfile: String;
Curid: String;
CUROBJ: TOBJ;
lstdate: tinifile32;
ฉัน, J: จำนวนเต็ม;
เริ่ม
J: = olst.count;
ถ้า savedialog1.execute แล้ว
เริ่ม
selfile: = savedialog1.filename;
lstdate: = tinifile32.create (selfile+'. dat');
lstdate.writeInteger ('title', 'num', j);
สำหรับ i: = 0 ถึง J-1 ทำ
เริ่ม
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, 'เลือก', curobj.Selected);
lstdate.writestring (Curid, 'fnode', curobj.fnode);
lstdate.writeBool (Curid, 'แสดง', curobj.showed);
จบ;
จบ;
จบ;
ขั้นตอน TFRM_TREE.RANDOMRANDOMSELECTION1CLICK (ผู้ส่ง: TOBJECT);
วาจา
Selfile: String;
// Curid: String;
lstdate: tinifile32;
ฉัน, J: จำนวนเต็ม;
เริ่ม
ถ้า opendialog1.execute แล้ว
เริ่ม
selfile: = opendialog1.filename;
คลิก: = false;
Paintbox1.canvas.fillrect (rect (0,0, paintbox1.width, paintbox1.height));
Olst.Clear;
รูท: = จริง;
selid: = '';
searilid: = 0;
lstdate: = tinifile32.create (selfile);
j: = lstdate.readinteger ('title', 'num', 0);
สำหรับ i: = 1 ถึง J Do
เริ่ม
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), 'เลือก', true), lstdate.readstring (inttoStr (i), 'fnode', '') lstdate.readbool (inttoStr (i), 'แสดง', จริง));
จบ;
searilid: = j;
รูท: = เท็จ;
Drawfull;
จบ;
จบ;
ขั้นตอน tfrm_tree.button7click (ผู้ส่ง: tobject);
เริ่ม
Toolno: = 5;
จบ;
โพรซีเดอร์ tfrm_tree.movenode (dx, dy: จำนวนเต็ม; id: string);
วาจา
ฉัน, J: จำนวนเต็ม;
CUROBJ:^TOBJ;
เริ่ม
J: = olst.count;
สำหรับ i: = 0 ถึง J-1 ทำ
เริ่ม
ถ้า TOBJ (olst.items [i]^). fnode = id แล้ว
เริ่ม
curobj: = getPoBJ (TOBJ (olst.Items [i]^). objid);
curobj^.centerx: = curobj^.centerx+dx;
curobj^.centery: = curobj^.centery+dy;
movenode (dx, dy, curobj^.objid);
จบ;
จบ;
จบ;
ขั้นตอน TFRM_TREE.MOVELOCAL (DX, DY: จำนวนเต็ม);
วาจา
ฉัน, J: จำนวนเต็ม;
// CUROBJ: TOBJ;
เริ่ม
J: = olst.count;
สำหรับ i: = 0 ถึง J-1 ทำ
เริ่ม
ถ้า Tobj (olst.items [i]^) เลือกแล้ว
เริ่ม
Tobj (olst.items [i]^). centerx: = tobj (olst.items [i]^). centerx+dx;
Tobj (olst.items [i]^). Centery: = Tobj (olst.items [i]^). Centery+Dy;
หยุดพัก;
จบ;
จบ;
จบ;
จบ.