📄 initdbsetunit.pas
字号:
unit InitDbSetUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, VirtualTrees, Buttons, ExtCtrls, IniFiles,
ADODB, StdCtrls,Commfunc;
type
TFormInitDBSet = class(TForm)
PanelToolBar: TPanel;
SpeedButtonNew: TSpeedButton;
SpeedButtonDel: TSpeedButton;
VStrTree: TVirtualStringTree;
SpeedButtonSav: TSpeedButton;
LabelMsg: TLabel;
procedure FormCreate(Sender: TObject);
procedure VStrTreeInitNode(Sender: TBaseVirtualTree; ParentNode,
Node: PVirtualNode; var InitialStates: TVirtualNodeInitStates);
procedure VStrTreeGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
Column: TColumnIndex; TextType: TVSTTextType;
var CellText: WideString);
procedure SpeedButtonNewClick(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure SpeedButtonDelClick(Sender: TObject);
procedure VStrTreeDblClick(Sender: TObject);
procedure SpeedButtonSavClick(Sender: TObject);
procedure VStrTreeChecked(Sender: TBaseVirtualTree;
Node: PVirtualNode);
procedure VStrTreeClick(Sender: TObject);
private
ActCntName:String;{活动列表名称}
NameList,CntList:TStringList;{显示项目列表}
procedure RefCntList;
public
{ Public declarations }
end;
implementation
{$R *.dfm}
type
PEntry = ^TEntry;
TEntry = record
Name: String;
Value:String;
end;
procedure TFormInitDBSet.FormCreate(Sender: TObject);
var{初始化函数}
IniF:TIniFile;
I:Integer;
begin
CntList:=TStringList.Create;
NameList:=TStringList.Create;
IniF:=TIniFile.Create(IniFileName);
IniF.ReadSection('数据连接',NameList);
for I:=0 to NameList.Count-1 do
CntList.Add(IniF.ReadString('数据连接',NameList.Strings[I],''));
ActCntName:=IniF.ReadString('设置','活动连接','');
IniF.Free;
VStrTree.NodeDataSize:=SizeOf(TEntry);
RefCntList;
end;
procedure TFormInitDBSet.VStrTreeInitNode(Sender: TBaseVirtualTree;
ParentNode, Node: PVirtualNode;
var InitialStates: TVirtualNodeInitStates);
var{连接列表处世化函数}
pData: PEntry;
begin
if ParentNode=nil then begin
Node.CheckType := ctRadioButton;
pData := Sender.GetNodeData(Node);
pData.Name:=NameList.Strings[Node.Index];
pData.Value:=CntList.Strings[Node.Index];
if(pData.Name=ActCntName)then
Node.CheckState:=csCheckedPressed;
end;
end;
procedure TFormInitDBSet.VStrTreeGetText(Sender: TBaseVirtualTree;
Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType;
var CellText: WideString);
var{连接列表内容填充函数}
pData: PEntry;
begin
pData := Sender.GetNodeData(Node);
case Column of
0:CellText := pData.Name;
1:CellText := pData.Value;
end;
end;
procedure TFormInitDBSet.SpeedButtonNewClick(Sender: TObject);
var{新增按钮功能}
CntStr,Setting:String;
I:Integer;
begin
CntStr:='';
if (InputQuery('新建数据库连接','连接名称:', CntStr))and(CntStr<>'') then begin
CntStr:=Trim(CntStr);
for I:=0 to NameList.Count-1 do
if NameList.Strings[I]=CntStr then begin
ShowMessage('['+CntStr+']已经存在了');
Exit;
end;
Setting:=PromptDataSource(Handle,'');
if Setting<>'' then begin
CntList.Add(EncrypPassword(Setting,False));
NameList.Add(CntStr);
if NameList.Count=1 then
ActCntName:=CntStr;
RefCntList;
end;
end;
end;
procedure TFormInitDBSet.FormDestroy(Sender: TObject);
begin{释放函数}
NameList.Free;
CntList.Free;
end;
procedure TFormInitDBSet.RefCntList;
begin{刷新显示列表}
VStrTree.Clear;
VStrTree.RootNodeCount:=NameList.Count;
end;
procedure TFormInitDBSet.SpeedButtonDelClick(Sender: TObject);
begin{删除按钮功能}
if VStrTree.SelectedCount=0 then
ShowMessage('请选定要删除的连接')
else begin
if MessageDlg('真的决定删除 ['+NameList.Strings[VStrTree.FocusedNode.Index]+'] 吗?',mtConfirmation,[mbOk,mbCancel],0)=mrOK then begin
CntList.Delete(VStrTree.FocusedNode.Index);
if NameList.Strings[VStrTree.FocusedNode.Index]=ActCntName then ActCntName:='';
NameList.Delete(VStrTree.FocusedNode.Index);
if NameList.Count=0 then ActCntName:='';
RefCntList;
end;
end;
end;
procedure TFormInitDBSet.VStrTreeDblClick(Sender: TObject);
var{编辑数据库设置}
MousePt:TPoint;
pData:PEntry;
pNode:PVirtualNode;
OldSetting,NewSetting:String;
begin
if(VStrTree.FocusedNode<>nil)and(VStrTree.Selected[VStrTree.FocusedNode]) then begin
GetCursorPos(MousePt);
MousePt:=VStrTree.ScreenToClient(MousePt);
pNode:=VStrTree.GetNodeAt(MousePt.X,MousePt.Y);
if pNode<>nil then begin
pData:=VStrTree.GetNodeData(pNode);
OldSetting:=EncrypPassword(pData.Value,True);
NewSetting:=PromptDataSource(Handle,OldSetting);
if(OldSetting<>NewSetting)then{用户改变了数据库设置}
pData.Value:=EncrypPassword(NewSetting,False);
CntList.Strings[pNode.Index]:=pData.Value;
end;
end
end;
procedure TFormInitDBSet.SpeedButtonSavClick(Sender: TObject);
var{存盘退出}
IniF:TIniFile;
I:Integer;
begin
if NameList.IndexOf(ActCntName)<0 then begin
if MessageDlg('没有指定活动连接,程序无法正常运行',mtWarning,[mbOk,mbCancel],0)=mrCancel then
Exit;
end;
IniF:=TIniFile.Create(IniFileName);
IniF.EraseSection('数据连接');
for I:=0 to NameList.Count-1 do
IniF.WriteString('数据连接',NameList.Strings[I],CntList.Strings[I]);
IniF.WriteString('设置','活动连接',ActCntName);
IniF.Free;
ModalResult:=mrOK;
end;
procedure TFormInitDBSet.VStrTreeChecked(Sender: TBaseVirtualTree;
Node: PVirtualNode);
var{设置活动连接}
pData:PEntry;
begin
pData:=VStrTree.GetNodeData(Node);
ActCntName:=pData.Name;
end;
procedure TFormInitDBSet.VStrTreeClick(Sender: TObject);
begin{操作后去掉警告提示}
LabelMsg.Visible:=False;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -