📄 generallibunit.pas
字号:
unit GeneralLibUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, dxsbar, ComCtrls, ImgList, dxCntner, dxTL, dxDBCtrl,
dxDBGrid, ToolWin, Buttons, DB, ADODB,iniFiles;
procedure LoadGrid(striniFileName:String;strViewName:String;
AdoQuery:TAdoQuery;DBGrid:TDxDBGrid);//获得配置文件中关于DBGrid设置的相关信息
procedure SaveGrid(striniFileName:String;strViewName:String;
AdoQuery:TAdoQuery;DBGrid:TDXDBGrid);//将DBGrid的信息回存到配置文件中
var
gstrConnectionString:string;//数据库连接字符串
gstrOperaterCode:string;//操作员编码
implementation
////////////////////////////////////////////////////////////////
//函数名称:LoadGrid //
//函数功能;设置DBGrid的状态 //
//输入参数:StrIniFilePath,配置文件完全文件名 //
// strViewName, 表名 //
// AdoQuery, AdoQuery组件名 //
// DBGrid, DBGrid组件名 //
//输出参数:无 //
//编写人: //
//编写时间:2001年9月7日
//修改人:
//修改时间:2003.11.04
//修改内容:增加字段格式读取程序段 //
////////////////////////////////////////////////////////////////
procedure LoadGrid(striniFileName, strViewName: String;
AdoQuery: TAdoQuery; DBGrid: TDxDBGrid);
var
i,j:integer;
strReturned:array[0..200] of char;//
strContent,strVisible,strLength,strDivideFlag:string;//
intPosition:integer;//
//iPosition,iExists:Integer;//iPosition查找F的位置 ,iExists:F是否存在 luxu03.11.04add
sFormat,sBackColor,sTextColor:String;//保留格式、背景色、字体颜色
iPosA,iPosB,iPosC:Integer; //'F'的位置,'BackGroundColor'的位置,'TextColor'的位置
colorBack,colorText:TColor;//表格的背景色,字体颜色
begin
strDivideFlag:='/';
for i:=0 to AdoQuery.Fields.Count -1 do
begin
FillChar(strReturned,SizeOf(strReturned),#0); //luxu 03.11.04 add
GetPrivateProfileString(PChar(strViewName),
pchar(AdoQuery.Fields[i].FieldName),
'',strReturned,255,pchar(striniFileName));
j:=0;
strContent:='';
while strReturned[j]<>'' do
begin
strContent:=strContent+strReturned[j];
j:=j+1;
end;
intPosition:=Pos(strDivideFlag,strContent); //查找第一个"/"
strVisible:=Copy(strContent,1,intPosition-1); //strVisible='1'
Delete(strContent,1,intPosition); //删除"1/"
intPosition:=Pos(strDivideFlag,strContent);
strLength:=Copy(strContent,1,intPosition-1); //strLength=100
//luxu 03.11.04 add
iPosA:=Pos('F',strContent);
iPosB:=Pos('BackGroundColor',strContent);
iPosC:=Pos('TextColor',strContent);
if iPosA>0 then
begin
sFormat:=Copy(strContent,iPosA+2,(iPosB-1)-(iPosA+2)); //取字段格式
sBackColor:=Copy(strContent,iPosB+16,(iPosC)-(iPosB+17));//取背景色
Delete(strContent,1,iPosC+9);
sTextColor:=Copy(strContent,1,Length(strContent)-1);//取字体颜色
try //如果取得的颜色不合法,设置默认颜色
colorBack:=StringToColor(sBackColor);
colorText:=StringToColor(sTextColor);
except
colorBack:=clWindow;
colorText:=clWindowText;
end;//except
DBGrid.Columns[i].Color:=colorBack;
DBGrid.Columns[i].Font.Color:=colorText;
if (AdoQuery.Fields[i].DataType in [ftInteger,ftBCD]) and (sFormat<>'') then
TIntegerField(AdoQuery.Fields[i]).DisplayFormat:=sFormat;
if (adoQuery.Fields[i].DataType in [ftDateTime]) and (sFormat<>'')then
begin
TDateTimeField(AdoQuery.Fields[i]).DisplayFormat:=sFormat;
end;
end; //End iPosA
//luxu 03.11.04 add
{if strVisible='1' then
DBGrid.columns[i].Visible:=True
else
DBGrid.columns[i].Visible:=False; }
if strLength<>'' then
DBGrid.columns[i].width:=StrToInt(strLength)
else
DBGrid.columns[i].width:=Length(AdoQuery.Fields[i].FieldName )*8;
end;
end;
////////////////////////////////////////////////////////////////
//函数名称:SaveGrid
//函数功能;保存DBGrid的状态 //
//输入参数:StrIniFilePath,配置文件完全文件名 //
// strViewName, 表名 //
// AdoQuery, AdoQuery组件名 //
// DBGrid, DBGrid组件名 //
//输出参数:无 //
//编写人:李刚 //
//编写时间:2001年12月7日
//修改人:
//修改时间:2003.11.04
//修改内容:增加字段格式保存程序段 //
////////////////////////////////////////////////////////////////
procedure SaveGrid(striniFileName, strViewName: String;
AdoQuery: TAdoQuery; DBGrid: TDXDBGrid);
var
i,j:integer;
strFields,StrStr:pChar;//
strVisible,strLength,StrIndex:string;//
AppIni: TIniFile;
strIniFieldName,strDateFieldName:TStrings;
intYesNo:integer;
sFieldFormat:String; //定义字段的显示格式 luxu03.11.04add
tmpType:TFieldType; //字段类型
sFormatString:String;// 字符串组合 "/F/格式/"
colorBack,colorText:TColor; //在DBGrid中用于显示不同的列的颜色和字体颜色
sColorString,sColorBack,sColorText:String;
ini:TiniFile;
iPosition:integer;
sValue:String;
begin
strIniFieldName:=TStringlist.Create;
strDateFieldName:=TStringlist.Create;
ini:=TiniFile.Create(striniFileName);
//strDivideFlag:='/';
for i:=0 to AdoQuery.Fields.Count -1 do
begin
strfields:= PChar(AdoQuery.Fields[i].FieldName);
strDateFieldName.Append(strFields);
if DBGrid.columns[i].Visible=True then
strVisible:='1'
else
strVisible:='0';
//luxu 03.11.04add
{ tmpType:=adoQuery.Fields[i].DataType; //字段类型
if tmpType in [ftInteger,ftBCD] then
begin
sFieldFormat:=TNumericField(adoQuery.Fields[i]).DisplayFormat;
if sFieldFormat='' then sFieldFormat:='0';
end
Else
if tmpType in [ftDateTime] then
begin
sFieldFormat:=TDateTimeField(adoQuery.Fields[i]).DisplayFormat;
if sFieldFormat='' then sFieldFormat:='c';
end
Else
sFieldFormat:=' '; //字符类型不需要进行字符串格式化}
///////////////////////////////////////////////////////
sValue:=ini.ReadString(strViewName,AdoQuery.Fields[i].FieldName,'Error');
iPosition:=Pos('F',sValue);
if iPosition>0 then
begin
Delete(sValue,1,iPosition+1);
iPosition:=Pos('/',sValue);
sFieldFormat:=Copy(sValue,1,iPosition-1);
end;
///////////////////////////////////////////////////////
sFormatString:='F/'+sFieldFormat+'/'; // 例如:sFormatString=F/#.00/
//luxu 03.11.04 add
sColorBack:=ColorToString(DBGrid.Columns[i].Color); //列颜色
sColorText:=ColorToString(DBGrid.Columns[i].Font.color); //字体颜色
//将列的颜色和字体颜色组合成连接字符串
sColorString:='BackGroundColor/'+sColorBack+'/TextColor/'+sColorText+'/';
strLength:=IntToStr(DBGrid.columns[i].width);
strIndex:=IntToStr(i+1);
StrStr:=PChar(strVisible+'/'+strLength+'/'+strIndex+'/'+sFormatString+sColorString);
WritePrivateProfileString(PChar(strViewName),strFields,StrStr,
pChar(striniFileName));
end;
AppIni := TIniFile.Create(striniFileName);
AppIni.ReadSection(strViewName,strIniFieldName);
for i:= 0 to strIniFieldName.Count -1 do
begin
intYesNo:=0 ;
for j:= 0 to strDateFieldName.Count -1 do
begin
if trim(strIniFieldName[i])=trim(strDateFieldName[j]) then
begin
intYesNo:=1;
break;
end;
end;
if intYesNo=0 then
begin
strFields:=PChar(strIniFieldName[i]);
{ if (adoQuery.FieldByName(strFields).DataType=ftBCD) or
(adoQuery.FieldByName(strFields).DataType=ftInteger) then m:='0'
Else if (adoQuery.FieldByName(strFields).DataType=ftDateTime) then
m:='C' Else m:=' '; }
StrStr:=PChar('0'+'/'+'100'+'/'+'0'+'/'+'F/'+''+'/'+'BackGroundColor/clWindow/TextColor/clWindowText/');
WritePrivateProfileString(PChar(strViewName),strFields,StrStr,
pChar(striniFileName));
end;
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -