下面这些代码是我自己用程序来控制图层,类似于Map1.LayersDLG。
unit uLayer_Control;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, OleCtrls, MapXLib_TLB;
type
TfLayerControl = class(TForm)
Label1: TLabel;
ListBox1: TListBox;
sbUP: TSpeedButton;
sbDown: TSpeedButton;
sbAdd: TSpeedButton;
sbRemove: TSpeedButton;
GroupBox1: TGroupBox;
cbVisible: TCheckBox;
cbEditable: TCheckBox;
cbAutoLabel: TCheckBox;
cbSelectable: TCheckBox;
cbInsertionLayer: TCheckBox;
cbShowCentroids: TCheckBox;
cbShowNodes: TCheckBox;
cbShowLineDirection: TCheckBox;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
BitBtn3: TBitBtn;
procedure FormActivate(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
procedure cbVisibleClick(Sender: TObject);
procedure cbEditableClick(Sender: TObject);
procedure cbAutoLabelClick(Sender: TObject);
procedure cbShowNodesClick(Sender: TObject);
procedure cbSelectableClick(Sender: TObject);
procedure cbInsertionLayerClick(Sender: TObject);
procedure cbShowCentroidsClick(Sender: TObject);
procedure cbShowLineDirectionClick(Sender: TObject);
procedure BitBtn3Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure sbUPClick(Sender: TObject);
procedure sbDownClick(Sender: TObject);
procedure sbAddClick(Sender: TObject);
procedure sbRemoveClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure InitialLayerOptionArr;
function CheckInsertionLayer(Lyr:CMapxLayer):boolean;
procedure UpdateLayerListbox;
//Update the ListBox1;
procedure UpdateLayerOptionArr;
//Update the ChkLayerOptionArr;
procedure UpdateLayerOption;
//Update the map1's LayerOption;
procedure UpdateLayerOptionChkBox;
//update the checkbox's checked state when the listbox'selection is changed
procedure MoveArray(iFrom,iTo:integer);
//change the LayerOptionArr between the iFrom and iTo;
end;
var
fLayerControl: TfLayerControl;
implementation
uses uMain, uInter;
{$R *.dfm}
procedure TfLayerControl.FormActivate(Sender: TObject);
begin
initialLayerOptionArr;
UpdateLayerListbox;
updateLayerOptionChkBox;
end;
procedure TflayerControl.UpdateLayerListbox;
var
i:integer;
begin
ListBox1.Clear;
for i:=1 to fMain.Map1.Layers.Countdo
begin
Listbox1.Items.Add(fMain.Map1.Layers.item(i).Name);
end;
Listbox1.ItemIndex:=0;
updateLayerOptionChkBox;
end;
procedure TfLayerControl.UpdateLayerOptionArr;
var
i,j:integer;
begin
for i:=1 to fMain.map1.Layers.Countdo
begin
ChkLayerOptionArr[i-1,OptVisible]:= fMain.map1.Layers.Item(i).Visible
ChkLayerOptionArr[i-1,OptEditalbe]:= fMain.map1.Layers.Item(i).Editable;
ChkLayerOptionArr[i-1,OptAutoLabel]:= fMain.map1.Layers.Item(i).AutoLabel;
ChkLayerOptionArr[i-1,OptShowNodes]:= fMain.map1.Layers.Item(i).ShowNodes;
ChkLayerOptionArr[i-1,OptSelectable]:= fMain.map1.Layers.Item(i).Selectable;
ChkLayerOptionArr[i-1,OptInsertionLayer]:= CheckInsertionLayer(fMain.map1.Layers.Item(i));
ChkLayerOptionArr[i-1,OptShowCentroids]:= fMain.map1.Layers.Item(i).ShowCentroids;
ChkLayerOptionArr[i-1,OptShowLineDirection]:= fMain.map1.Layers.Item(i).ShowLineDirection;
end;
end;
procedure TfLayerControl.UpdateLayerOption;
var
i:integer;
begin
for i:=1 to fMain.map1.Layers.Countdo
begin
fMain.map1.Layers.Item(i).Visible:= ChkLayerOptionArr[i-1,OptVisible];
fMain.map1.Layers.Item(i).Editable:=ChkLayerOptionArr[i-1,OptEditalbe]
fMain.map1.Layers.Item(i).AutoLabel:= ChkLayerOptionArr[i-1,OptAutoLabel];
fMain.map1.Layers.Item(i).ShowNodes:= ChkLayerOptionArr[i-1,OptShowNodes];
fMain.map1.Layers.Item(i).Selectable:= ChkLayerOptionArr[i-1,OptSelectable];
if ChkLayerOptionArr[i-1,OptInsertionLayer] then
fMain.map1.Layers.insertionLayer:= fMain.map1.Layers.Item(i);
fMain.map1.Layers.Item(i).ShowCentroids:= ChkLayerOptionArr[i-1,OptShowCentroids];
fMain.map1.Layers.Item(i).ShowLineDirection:= ChkLayerOptionArr[i-1,OptShowLineDirection];
end;
end;
procedure TfLayerControl.UpdateLayerOptionChkBox;
var
lyr:CMapXLayer;
i:integer;
begin
if listbox1.Items.Count>0 then
begin
if Listbox1.ItemIndex<0 then
Listbox1.ItemIndex:=0
i:=listbox1.ItemIndex;
cbVisible.Checked:=ChkLayerOptionArr[i,OptVisible]
cbEditable.Checked:=ChkLayerOptionArr[i,OptEditalbe]
cbAutoLabel.Checked:=ChkLayerOptionArr[i,OptAutoLabel]
cbSelectable.Checked:=ChkLayerOptionArr[i,OptSelectable]
cbInsertionLayer.Checked:=ChkLayerOptionArr[i,OptInsertionLayer]
cbShowCentroids.Checked:=ChkLayerOptionArr[i,OptShowCentroids]
cbShowLineDirection.Checked:=ChkLayerOptionArr[i,OptShowLineDirection]
end
else
begin
cbVisible.Checked:=false;
cbEditable.Checked:=false;
cbAutoLabel.Checked:=false;
cbSelectable.Checked:=false;
cbInsertionLayer.Checked:=false;
cbShowCentroids.Checked:=false;
cbShowLineDirection.Checked:=false;
end;
end;
function TfLayerControl.CheckInsertionLayer(Lyr:CMapxLayer):boolean;
begin
result:=(lyr=fmain.Map1.Layers.InsertionLayer);
end;
procedure TfLayerControl.InitialLayerOptionArr;
var
i,j:integer;
begin
for i:=1 to fMain.map1.Layers.Countdo
begin
ChkLayerOptionArr[i-1,OptVisible]:= fMain.map1.Layers.Item(i).Visible
ChkLayerOptionArr[i-1,OptEditalbe]:= fMain.map1.Layers.Item(i).Editable;
ChkLayerOptionArr[i-1,OptAutoLabel]:= fMain.map1.Layers.Item(i).AutoLabel;
ChkLayerOptionArr[i-1,OptShowNodes]:= fMain.map1.Layers.Item(i).ShowNodes;
ChkLayerOptionArr[i-1,OptSelectable]:= fMain.map1.Layers.Item(i).Selectable;
ChkLayerOptionArr[i-1,OptInsertionLayer]:= CheckInsertionLayer(fMain.map1.Layers.Item(i));
ChkLayerOptionArr[i-1,OptShowCentroids]:= fMain.map1.Layers.Item(i).ShowCentroids;
ChkLayerOptionArr[i-1,OptShowLineDirection]:= fMain.map1.Layers.Item(i).ShowLineDirection;
end;
end;
procedure TfLayerControl.ListBox1Click(Sender: TObject);
begin
UpdateLayerOptionChkBox;
end;
procedure TfLayerControl.cbVisibleClick(Sender: TObject);
begin
ChkLayerOptionArr[listbox1.ItemIndex,OptVisible]:=(sender as TCheckBox).Checked;
end;
procedure TfLayerControl.cbEditableClick(Sender: TObject);
begin
ChkLayerOptionArr[listbox1.ItemIndex,OptVisible]:=(sender as TCheckBox).Checked;
end;
procedure TfLayerControl.cbAutoLabelClick(Sender: TObject);
begin
ChkLayerOptionArr[listbox1.ItemIndex,OptAutoLabel]:=(sender as TCheckBox).Checked;
end;
procedure TfLayerControl.cbShowNodesClick(Sender: TObject);
begin
ChkLayerOptionArr[listbox1.ItemIndex,OptShowNodes]:=(sender as TCheckBox).Checked;
end;
procedure TfLayerControl.cbSelectableClick(Sender: TObject);
begin
ChkLayerOptionArr[listbox1.ItemIndex,OptSelectable]:=(sender as TCheckBox).Checked;
end;
procedure TfLayerControl.cbInsertionLayerClick(Sender: TObject);
begin
ChkLayerOptionArr[listbox1.ItemIndex,OptInsertionLayer]:=(sender as TCheckBox).Checked;
end;
procedure TfLayerControl.cbShowCentroidsClick(Sender: TObject);
begin
ChkLayerOptionArr[listbox1.ItemIndex,OptShowCentroids]:=(sender as TCheckBox).Checked;
end;
procedure TfLayerControl.cbShowLineDirectionClick(Sender: TObject);
begin
ChkLayerOptionArr[listbox1.ItemIndex,OptShowLineDirection]:=(sender as TCheckBox).Checked;
end;
procedure TfLayerControl.BitBtn3Click(Sender: TObject);
begin
updateLayerOption;
end;
procedure TfLayerControl.BitBtn1Click(Sender: TObject);
begin
updateLayerOption;
close;
end;
procedure TfLayerControl.BitBtn2Click(Sender: TObject);
begin
close;
end;
procedure TfLayerControl.sbUPClick(Sender: TObject);
var
i:integer;
begin
i:=listbox1.ItemIndex;
if i>0 then
begin
fMain.Map1.Layers.Move(i+1,i);
MoveArray(i,i-1);
end;
UpdateLayerListbox;
end;
procedure TflayerControl.MoveArray(iFrom,iTo:integer);
var
tmpArr:array[TLayerOption]of boolean;
begin
tmpArr[OptVisible]:= ChkLayerOptionArr[iFrom,OptVisible];
tmpArr[OptEditalbe]:=ChkLayerOptionArr[iFrom,OptEditalbe];
tmpArr[OptAutoLabel]:=ChkLayerOptionArr[iFrom,OptAutoLabel];
tmpArr[OptShowNodes]:=ChkLayerOptionArr[iFrom,OptShowNodes];
tmpArr[OptSelectable]:=ChkLayerOptionArr[iFrom,OptSelectable];
tmpArr[OptInsertionLayer]:=ChkLayerOptionArr[iFrom,OptInsertionLayer];
tmpArr[OptShowCentroids]:=ChkLayerOptionArr[iFrom,OptShowCentroids];
tmpArr[OptShowLineDirection]:=ChkLayerOptionArr[iFrom,OptShowLineDirection];
ChkLayerOptionArr[iFrom,OptVisible]:=ChkLayerOptionArr[iTo,OptVisible];
ChkLayerOptionArr[iFrom,OptEditalbe]:=ChkLayerOptionArr[iTo,OptEditalbe];
ChkLayerOptionArr[iFrom,OptAutoLabel]:=ChkLayerOptionArr[iTo,OptAutoLabel];
ChkLayerOptionArr[iFrom,OptShowNodes]:=ChkLayerOptionArr[iTo,OptShowNodes];
ChkLayerOptionArr[iFrom,OptSelectable]:=ChkLayerOptionArr[iTo,OptSelectable];
ChkLayerOptionArr[iFrom,OptInsertionLayer]:=ChkLayerOptionArr[iTo,OptInsertionLayer];
ChkLayerOptionArr[iFrom,OptShowCentroids]:=ChkLayerOptionArr[iTo,OptShowCentroids];
ChkLayerOptionArr[iFrom,OptShowLineDirection]:=ChkLayerOptionArr[iTo,OptShowLineDirection];
ChkLayerOptionArr[iTo,OptVisible]:=tmpArr[OptVisible];
ChkLayerOptionArr[iTo,OptEditalbe]:=tmpArr[OptEditalbe];
ChkLayerOptionArr[iTo,OptAutoLabel]:=tmpArr[OptAutoLabel];
ChkLayerOptionArr[iTo,OptShowNodes]:=tmpArr[OptShowNodes];
ChkLayerOptionArr[iTo,OptSelectable]:=tmpArr[OptSelectable];
ChkLayerOptionArr[iTo,OptInsertionLayer]:=tmpArr[OptInsertionLayer];
ChkLayerOptionArr[iTo,OptShowCentroids]:=tmpArr[OptShowCentroids];
ChkLayerOptionArr[iTo,OptShowLineDirection]:=tmpArr[OptShowLineDirection];
end;
procedure TfLayerControl.sbDownClick(Sender: TObject);
var
i:integer;
begin
i:=listbox1.ItemIndex;
if i<listbox1.Items.Count then
begin
fmain.Map1.Layers.Move(i+1,i+2);
MoveArray(i,i+1);
end;
UpdateLayerListbox;
end;
procedure TfLayerControl.sbAddClick(Sender: TObject);
var
sFileName:string;
begin
//This uses the opemdialog to open a MapInfo Table and add it as a layer.
with fmain.openDlgdo
begin
Title:='Add Layer';
Filter:='MapInfo Tables (*.tab)|*.tab';
if Execute then
begin
if length(filename)=0 then
exit;
sFileName:=fileName;
fmain.map1.Layers.Add(sFileName,emptyParam);
fmain.updatecmbLayer;
UpdateLayerListbox;
UpdateLayerOptionArr;
updateLayerOptionChkBox;
end;
end;
end;
procedure TfLayerControl.sbRemoveClick(Sender: TObject);
begin
fmain.map1.Layers.Remove(listbox1.Items.Text);
fmain.updatecmbLayer;
UpdateLayerListbox;
UpdateLayerOptionArr;
updateLayerOptionChkBox;
end;
end.