稍微修改了下,加了字母和数字的转换控制
//简单的转换函数,复杂的话恐怕要加上脚本标记才行,
//因为有很多情况全角字符对应半角字符是多对一的关系
//例如:< 《 < 〈 ≮ ≤;或者:[ 【 [ 〔 〖 「 『
function ConvertToChinses(ASource:string;
CovNum:boolean=false;
CovUChar:boolean=false;
CovLChar:boolean=false): string;
function ToChinesChar(AChar: Char): WChar;
begin
{通用的常规字符到全角字符的转化函数}
Result := WideChar(65248 + Ord(AChar));
end;
var
TmpWChar:string;
do
ubleFH:Boolean;
m,n,l,x,i:cardinal;
TmpChar:Char;
begin
Result := '';
do
ubleFH := True;
l:=length(ASource);
setlength(result,l*2);
{假设全部转换为双字节,分配足够大空间}
m:=1;
n:=1;
while m<ldo
begin
TmpChar:=ASource[m];
if (ord(TmpChar)>127) or (ord(TmpChar)<33) or
((Not CovNum) and (TmpChar in ['0'..'9'])) or
((Not CovUChar) and (TmpChar in ['A'..'Z'])) or
((Not CovLChar) and (TmpChar in ['a'..'z'])) then
begin
Result[n]:=TmpChar;
inc(m);
inc
;
continue;
end;
case TmpChar of
'.':
begin
if (ASource[m-1] = '.') or (ASource[m+1] = '.') then
begin
TmpWChar := '·';
{省略号}
end
else
TmpWChar := '。';
{句号}
end;
'/':
TmpWChar := '、';
{顿号}
'"':
begin
ifdo
ubleFH then
TmpWChar := '“' {左双引号}
else
TmpWChar := '”';
{右双引号}
do
ubleFH := notdo
ubleFH;
end;
'''':
begin
ifdo
ubleFH then
TmpWChar := '‘' {左单引号}
else
TmpWChar := '’';
{右单引号}
do
ubleFH := notdo
ubleFH;
end;
'<':
TmpWChar := '《';
{左书名号}
'>':
TmpWChar := '》';
{右书名号}
else
TmpWChar := ToChinesChar(TmpChar);
end;
x:=length(TmpWChar);
for i:=0 to x-1do
Result[n+i]:=TmpWChar[i+1];
Inc(m);
Inc(n,x);
end;
setlength(result,n-1);
end;
测试:
memo2.Lines.Text:=ConvertToChinses(memo1.Lines.Text,true,true,true);
原文:
procedure TForm1.FormCreate(Sender: TObject);
var
i,j:byte;
str:string;
begin
_hint:=THintWindow.Create(self);
combobox1.Items.Clear;
for i:=0 to 9do
begin
str:='';
setlength(str,31);
for j:=1 to 30do
str[j]:=inttostr(i)[1];
str[31]:='A';
//检测是否显示全最后一个字符
combobox1.Items.Add(str);
end;
combobox1.Items.Add('短字符');
combobox1.Style:= csOwnerDrawFixed;
end;
转换后:
procedure TForm1。FormCreate(Sender: TObject);
var
i,j:byte;
str:string;
begin
_hint:=THintWindow。Create(self);
combobox1。Items。Clear;
for i:=0 to 9 do
begin
str:=‘’;
setlength(str,31);
for j:=1 to 30 do
str[j]:=inttostr(i)[1];
str[31]:=‘A’; //检测是否显示全最后一个字符
combobox1。Items。Add(str);
end;
combobox1。Items。Add(‘短字符’);
combobox1。Style:= csOwnerDrawFixed;
end;