?? unit1.pas
字號:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, ExtCtrls, Menus, StdCtrls, jpeg,Unit3;
const
MACHINE_TYPE_ZJ='s1';
MACHINE_TYPE_EH='s2';
MACHINE_TYPE_EF='s3';
MACHINE_TYPE_TX='s5';
MACHINE_TYPE_GF='s4';
MACHINE_PORT_XY='p0';
MACHINE_PORT_EH='p1';
MACHINE_PORT_ZT='p2';
MACHINE_PORT_P1='p1';
MACHINE_PORT_P2='p2';
type
TForm1 = class(TForm)
StatusBar1: TStatusBar;
pbMachines: TPaintBox;
pmMachines: TPopupMenu;
machineOp: TMenuItem;
machineAdd: TMenuItem;
machineDel: TMenuItem;
machinePro: TMenuItem;
machineMain: TMenuItem;
machineOh: TMenuItem;
machineEg: TMenuItem;
machineTx: TMenuItem;
imgS2: TImage;
imgS1: TImage;
imgS3: TImage;
imgS4: TImage;
imgS5: TImage;
machineGf: TMenuItem;
procedure machineOhClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure machineEgClick(Sender: TObject);
procedure machineTxClick(Sender: TObject);
procedure machineGfClick(Sender: TObject);
procedure N20bt1Click(Sender: TObject);
procedure pbMachinesContextPopup(Sender: TObject; MousePos: TPoint;
var Handled: Boolean);
procedure pbMachinesPaint(Sender: TObject);
procedure pbMachinesMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure pbMachinesMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure pbMachinesMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure machineDelClick(Sender: TObject);
procedure machineMainClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
//設備類
TMachine = class
mtype:string; //設備類型
wiretype:double; //線類型也是每米衰減
wirelen:integer; //線長度
img:TImage; //圖片
x:integer;
mx:integer; //鼠標在當前控件中的位置
y:integer;
my:integer; //鼠標在當前控件中的位置
width:integer;
height:integer;
p0:TMachine; //信源口
p1:TMachine; //耦合口
p2:TMachine; //直通口
pl:integer; //頻率
sj:double;
function inMyRang(mx:integer;my:integer):boolean;
procedure setMousePos(mpx:integer;mpy:integer);
procedure move(mpx:integer;mpy:integer);
function toString:string;
end;
//設備管理類
TMachineManage = class
machine:TMachine;
current:TMachine;
function getMachinePort(mtype:string):TStrings;
function getPortType(pname:string):string;
function getMachineName(mtype:string):string;
procedure addMainMachine(pl:integer;x:integer;y:integer);
procedure addMachine(mtype:string;mport:string;wtype:double;wlen:integer;xh:integer;sj:double);
procedure drawImage(canvas:TCanvas;mch:TMachine);
procedure findMachine(x:integer;y:integer;mch:TMachine);
function getImageByType(mtype:string):TImage;
procedure drawLine(m1:TMachine;m2:TMachine);
end;
var
Form1: TForm1;
machineManage:TMachineManage;
cpMousePoint:TPoint;
draged:boolean=false;
dragMouse:TPoint;
implementation
{$R *.dfm}
procedure TForm1.machineOhClick(Sender: TObject);
begin
mtype:=MACHINE_TYPE_EH;
form3.ShowModal;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
machineManage:=TMachineManage.Create;
end;
procedure TForm1.machineEgClick(Sender: TObject);
begin
mtype:=MACHINE_TYPE_EF;
form3.ShowModal;
end;
procedure TForm1.machineTxClick(Sender: TObject);
begin
mtype:=MACHINE_TYPE_TX;
form3.ShowModal;
end;
procedure TForm1.machineGfClick(Sender: TObject);
begin
mtype:=MACHINE_TYPE_GF;
form3.ShowModal;
end;
procedure TForm1.machineMainClick(Sender: TObject);
begin
mtype:=MACHINE_TYPE_ZJ;
form3.ShowModal;
end;
procedure TForm1.N20bt1Click(Sender: TObject);
begin
machineManage.addMainMachine(5,cpMousePoint.X,cpMousePoint.Y);
pbMachines.Repaint;
//showmessage(inttostr(machineManage.machine.x));
end;
procedure TForm1.pbMachinesContextPopup(Sender: TObject; MousePos: TPoint;
var Handled: Boolean);
begin
cpMousePoint:=MousePos;
if(machineManage.machine=nil) then
handled:=false
else begin
machineManage.findMachine(MousePos.X,MousePos.Y,machineManage.machine);
if not(machineManage.current.inMyRang(MousePos.X,MousePos.Y)) then
handled:=true
else
handled:=false;
end;
//可用菜單處理
machineMain.Enabled:=false;
machineOh.Enabled:=false;
machineEg.Enabled:=false;
machineTx.Enabled:=false;
machineGf.Enabled:=false;
if( not handled) then
begin
//如果沒有主機,可以添加主機
if(machineManage.machine=nil) then
begin
machineMain.Enabled:=true;
//如果主機的輸出口沒有設備可以加入設備
end else if(machineManage.machine.p1=nil) or
(machineManage.current.mtype=MACHINE_TYPE_EH) or
(machineManage.current.mtype=MACHINE_TYPE_EF) or
(machineManage.current.mtype=MACHINE_TYPE_GF) then
begin
machineOh.Enabled:=true;
machineEg.Enabled:=true;
machineTx.Enabled:=true;
machineGf.Enabled:=true;
//如果輸出口有設備,判斷當前設備
end;
end;
end;
procedure TForm1.pbMachinesPaint(Sender: TObject);
begin
machineManage.drawImage(pbMachines.Canvas,machineManage.machine);
end;
procedure TForm1.pbMachinesMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if(button=mbLeft) then
begin
machineManage.findMachine(x,y,machineManage.machine);
if(machineManage.current.inMyRang(x,y)) then
begin
draged:=true;
machineManage.current.setMousePos(x,y);
end;
end;
end;
procedure TForm1.pbMachinesMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if(button=mbLeft) then
begin
if(machineManage.current.inMyRang(x,y)) then
statusBar1.Panels[0].Text:=machineManage.current.toString;
draged:=false;
end;
end;
procedure TForm1.pbMachinesMouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
begin
if(draged)then
begin
machineManage.current.move(x,y);
end;
end;
procedure TForm1.machineDelClick(Sender: TObject);
var
m:TMachine;
p:TMachine;
begin
p:=machineManage.current.p0;
if(p=nil) then
begin
machineManage.machine:=nil;
machineManage.current:=nil;
end else
begin
if(p.p1=machineManage.current)then
begin
p.p1:=nil;
end else
begin
p.p2:=nil;
end;
machineManage.current:=p;
end;
pbMachines.Repaint;
end;
//以下為自定義類TMachineManage的實現
function TMachine.toString:string;
var
str:TStringList;
begin
str:=TStringList.Create;
str.Add(machineManage.getMachineName(mtype));
str.Add(' 頻率'+inttostr(pl));
str.Add(' 衰減'+floattostr(sj));
Result:=str.Text;
end;
//移動
procedure TMachine.move(mpx:integer;mpy:integer);
var
mouseX:integer;
mouseY:integer;
begin
mouseX:=mpx-x;
mouseY:=mpy-y;
x:=x+mouseX-mx;
y:=y+mouseY-my;
//mx:=mouseX;
//my:=mouseY;
//form1.Edit1.Text:=inttostr(x)+' '+inttostr(y)+' '+inttostr(mx)+' '+inttostr(my);
form1.pbMachines.Repaint;
end;
//設置鼠標在當前的設備中的位置
procedure TMachine.setMousePos(mpx:integer;mpy:integer);
begin
mx:=mpx-x;
my:=mpy-y;
end;
//畫線
procedure TMachineManage.drawLine(m1:TMachine;m2:TMachine);
begin
form1.pbMachines.Canvas.MoveTo(m1.x+m1.width div 2,m1.y+m1.height div 2);
form1.pbMachines.Canvas.LineTo(m2.x+m2.width div 2,m2.y+m2.height div 2);
end;
//通過類型獲得圖片
function TMachineManage.getImageByType(mtype:string):TImage;
begin
if(mtype=MACHINE_TYPE_ZJ) then
Result:=form1.imgS1
else if(mtype=MACHINE_TYPE_EH) then
Result:=form1.imgS2
else if(mtype=MACHINE_TYPE_EF) then
Result:=form1.imgS3
else if(mtype=MACHINE_TYPE_TX) then
Result:=form1.imgS5
else
Result:=form1.imgS4;
end;
//添加設備
procedure TMachineManage.addMachine(mtype:string;mport:string;wtype:double;wlen:integer;xh:integer;sj:double);
var
m:TMachine;
begin
m:=TMachine.Create;
m.mtype:=mtype;
m.wiretype:=wtype;
//m.wirelen:=wlen;
m.pl:=xh;
m.sj:=sj;
m.img:=machineManage.getImageByType(mtype);
m.x:=cpMousePoint.X;
m.y:=cpMousePoint.Y;
m.width:=m.img.Width;
m.height:=m.img.Height;
//if(mport='p0') then
//machineManage.current.p0:=m
if(mport='p1') then
if(machineManage.current.p1<>nil) then
showMessage('此端口上已有設備')
else
machineManage.current.p1:=m
else if(mport='p2') then
if(machineManage.current.p2<>nil) then
showMessage('此端口上已有設備')
else
machineManage.current.p2:=m;
m.p0:=machineManage.current;
machineManage.current.wirelen:=wlen;
form1.pbMachines.Repaint;
end;
//指定的鼠標位置是否在我這個設備區域中
function TMachine.inMyRang(mx:integer;my:integer):boolean;
begin
if(mx>=x) and (mx<=x+width) and (my>=y) and (my<=y+height) then
Result:=true
else
Result:=false;
end;
//設置當前設備
procedure TMachineManage.findMachine(x:integer;y:integer;mch:TMachine);
begin
if(mch<>nil) then
if(mch.inMyRang(x,y)) then
begin
current:=mch;
end else
begin
//if(mch.p0<>nil) then
//findMachine(x,y,mch.p0);
if(mch.p1<>nil) then
findMachine(x,y,mch.p1);
if(mch.p2<>nil) then
findMachine(x,y,mch.p2);
end;
end;
//畫圖
procedure TMachineManage.drawImage(canvas:TCanvas;mch:TMachine);
begin
if(mch<>nil) then
begin
// if(mch.p0<>nil) then
// begin
// drawLine(mch,mch.p0);
// drawImage(canvas,mch.p0);
// end;
if(mch.p1<>nil) then
begin
drawLine(mch,mch.p1);
drawImage(canvas,mch.p1);
end;
if(mch.p2<>nil) then
begin
drawLine(mch,mch.p2);
drawImage(canvas,mch.p2);
end;
canvas.Draw(mch.x,mch.y,mch.img.Picture.Graphic);
end;
end;
procedure TMachineManage.addMainMachine(pl:integer;x:integer;y:integer);
var
m:TMachine;
begin
m:=TMachine.Create;
m.img:=form1.imgS1;
m.x:=x;
m.y:=y;
m.pl:=pl;
m.width:=form1.imgS1.Width;
m.height:=form1.imgS1.Height;
m.mtype:=MACHINE_TYPE_ZJ;
machine:=m;
current:=m;
form1.pbMachines.Repaint;
end;
//獲得設備名
function TMachineManage.getMachineName(mtype:string):string;
begin
if(mtype=MACHINE_TYPE_ZJ) then
Result:='主設備'
else if(mtype=MACHINE_TYPE_EH) then
Result:='耦合器'
else if(mtype=MACHINE_TYPE_EF) then
Result:='二功分'
else if(mtype=MACHINE_TYPE_TX) then
Result:='天線'
else
Result:='干放';
end;
//根據字符口串獲得端口類型
function TMachineManage.getPortType(pname:string):string;
begin
if(pname='輸出口') then
Result:=MACHINE_PORT_P1
else if(pname='耦合口') then
Result:=MACHINE_PORT_EH
else if(pname='直通口') then
Result:=MACHINE_PORT_ZT
else if(pname='端口1') then
Result:=MACHINE_PORT_P1
else
Result:=MACHINE_PORT_p2;
end;
//獲得可用設備的端口集合
function TMachineManage.getMachinePort(mtype:string):TStrings;
var
strs:TStrings;
begin
strs:=TStringList.Create;
if(mtype=MACHINE_TYPE_ZJ) then
begin
strs.Add('輸出口');
end else if(mtype=MACHINE_TYPE_EH) then
begin
strs.Add('耦合口');
strs.Add('直通口');
end else if(mtype=MACHINE_TYPE_EF) then
begin
strs.Add('端口1');
strs.Add('端口2');
end else
begin
strs.Add('端口1');
strs.Add('端口2');
end;
Result:=strs;
end;
end.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -