请教:如何减少文件路径名的长度?
窗口宽度可调,Label的长度跟着变,Label的字体可由用户设置,
问题是如何根据Label的长度、字体,
来显示一个可省略部分路径的文件名?
如有这样一个文件:
C:\Program Files\Microsoft SQL Server\MSSQL$WINCC\abc.abc
希望它能缩短成:
C:\Program Files\..\abc.abc
窗口达到最小宽度时:
c:\..\abc.abc
//======================================
//以下是找到的一个不能用的函数
如何写程序呢?试试下面的函数:
////////////////////////////////////////////
function shortenfilename(s : string) : string;
var
drive,curdrive : string[2];
dir,curdir : string[80];
name : string[20];
ext : string[5];
i : byte;
begin
for i:=1 to length(s) do s[i]:=upcase(s[i]);
s:=fexpand(s);
fsplit(s,dir,name,ext);
drive:=copy(dir,1,2);
dir:=copy(dir,4,length(dir)-3);
getdir(0,curdir);
curdrive:=copy(curdir,1,2);
curdir:=copy(curdir,4,length(curdir)-3)+‘\'';
if drive=curdrive then begin
if copy(dir,1,length(curdir))=curdir then begin
i:=length(curdir);
if length(dir)<>i then dir:=dir+‘\'';
shortenfilename:=copy(dir,i+1,length(dir)-i-1)+name+ext;
end else shortenfilename:=copy(s,3,length(s)-2);
end else shortenfilename:=s;
end;
//////////////////////////////////
编译不过去:
s:=fexpand(s);
fsplit(s,dir,name,ext);
有这两个函数没?在哪个单元?
2楼: 给你一个函数试试看:
function MinimizeName(const Filename: TFileName; Canvas: TCanvas;
MaxLen: Integer): TFileName;
var
Drive: TFileName;
Dir: TFileName;
Name: TFileName;
begin
Result := FileName;
Dir := ExtractFilePath(Result);
Name := ExtractFileName(Result);
if (Length(Dir) >= 2) and (Dir[2] = '':'') then
begin
Drive := Copy(Dir, 1, 2);
Delete(Dir, 1, 2);
end
else
Drive := '''';
while ((Dir <> '''') or (Drive <> '''')) and (Canvas.TextWidth(Result) > MaxLen) do
begin
if Dir = ''\...\'' then
begin
Drive := '''';
Dir := ''...\'';
end
else if Dir = '''' then
Drive := ''''
else
CutFirstDirectory(Dir);
Result := Drive + Dir + Name;
end;
end; 如免费金蝶财务软件
3楼: 补上上一个函数中引用的一个函数:
procedure CutFirstDirectory(var S: TFileName);
var
Root: Boolean;
P: Integer;
begin
if S = ''\'' then
S := ''''
else
begin
if S[1] = ''\'' then
begin
Root := True;
Delete(S, 1, 1);
end
else
Root := False;
if S[1] = ''.'' then
Delete(S, 1, 4);
P := AnsiPos(''\'',S);
if P <> 0 then
begin
Delete(S, 1, P);
S := ''...\'' + S;
end
else
S := '''';
if Root then
S := ''\'' + S;
end;
end;
4楼: 使用方法:
label1.Caption := MinimizeName(AFilename, Label1.Canvas, label1.Width);
5楼: 老大:
CutFirstDirectory(Dir)
是个什么东西?在哪个单元中?
6楼: 上面已经贴出来了!给分吧!呵呵
记账软件版7楼: label1.Caption := MinimizeName(''C:\Program Files\Borland\Delphi7\三方控件\a.exe'', Label1.Canvas, label1.Width);
后为啥只显示“a.exe”?
label1.width:=400;
8楼: 设置属性 label1.autoSize := False;
我这边都可以的!
9楼: 好了,万分感谢!
不过本贴没分,对不住了,要不要我再开一贴?
10楼: 无所谓了,助人乃快乐之本!
11楼: 谢谢您!
祝顺!
12楼: 不用客气!以后相互学习! 如用友软件记账
13楼: 呵呵,您太谦了!
记账软件版14楼: 谦虚使人进步,骄傲使人落后!(不知道是谁说的)
15楼: 接受答案了.