试一试这个:<br>unit HzSpell;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes;<br><br>type<br> THzSpell = class(TComponent)<br> protected<br> FHzText: String;<br> FSpell: String;<br> FSpellH: String;<br> procedure SetHzText(const Value: String);<br> function GetHzSpell: String;<br> function GetPyHead: String;<br> public<br> class function PyOfHz(Hz: String): String;<br> class function PyHeadOfHz(Hz: String): String;<br> published<br> property HzText: String read FHzText write SetHzText;<br> property HzSpell: String read GetHzSpell;<br> property PyHead: String read GetPyHead;<br> end;<br><br>{$I HzSpDat2.inc}<br><br>procedure Register;<br><br>function GetHzPy(HzChar: PChar; Len: Integer): String;<br>function GetHzPyFull(HzChar: String): String;<br>function GetHzPyHead(HzChar: PChar; Len: Integer): String;<br>function GetPyChars(HzChar: String): String;<br><br>implementation<br><br>procedure Register;<br>begin<br> RegisterComponents('System', [THzSpell]);<br>end;<br><br>function GetHzPy(HzChar: PChar; Len: Integer): String;<br>var<br> C: Char;<br> Index: Integer;<br>begin<br> Result := '';<br> if (Len > 1) and (HzChar[0] >= #129) and (HzChar[1] >= #64) then<br> begin<br> //是否为 GBK 字符<br> case HzChar[0] of<br> #163: // 全角 ASCII<br> begin<br> C := Chr(Ord(HzChar[1]) - 128);<br> if C in ['a'..'z', 'A'..'Z', '0'..'9', '(', ')', '[', ']'] then<br> Result := C<br> else<br> Result := '';<br> end;<br> #162: // 罗马数字<br> begin<br> if HzChar[1] > #160 then<br> Result := CharIndex[Ord(HzChar[1]) - 160]<br> else<br> Result := '';<br> end;<br> #166: // 希腊字母<br> begin<br> if HzChar[1] in [#$A1..#$B8] then<br> Result := CharIndex2[Ord(HzChar[1]) - $A0]<br> else if HzChar[1] in [#$C1..#$D8] then<br> Result := CharIndex2[Ord(HzChar[1]) - $C0]<br> else<br> Result := '';<br> end;<br> else<br> begin // 获得拼音索引<br> Index := PyCodeIndex[Ord(HzChar[0]) - 128, Ord(HzChar[1]) - 63];<br> if Index = 0 then<br> Result := ''<br> else<br> Result := PyMusicCode[Index];<br> end;<br> end;<br> end<br> else if Len > 0 then<br> begin<br> //在 GBK 字符集外, 即半角字符<br> if HzChar[0] in ['a'..'z', 'A'..'Z', '0'..'9', '(', ')', '[', ']'] then<br> Result := HzChar[0]<br> else<br> Result := '';<br> end;<br>end;<br><br>function GetHzPyFull(HzChar: String): String;<br>var<br> i, len: Integer;<br> Py: String;<br> function IsDouByte(C: Char): Boolean;<br> begin<br> Result := C >= #129;<br> end;<br>begin<br> Result := '';<br> i := 1;<br> while i <= Length(HzChar) do<br> begin<br> if IsDouByte(HzChar) and (Length(HzChar) - i > 0) then<br> len := 2<br> else<br> len := 1;<br> Py := GetHzPy(@HzChar, len);<br> Inc(i, len);<br> if (Result <> '') and (Py <> '') then<br> Result := Result + ' ' + Py // + ' '<br> else<br> Result := Result + Py;<br> end;<br>end;<br><br>function GetHzPyHead(HzChar: PChar; Len: Integer): String;<br>begin<br> Result := Copy(GetHzPy(HzChar, Len), 1, 1);<br>end;<br><br>function GetPyChars(HzChar: String): String;<br>var<br> i, len: Integer;<br> Py: String;<br> function IsDouByte(C: Char): Boolean;<br> begin<br> Result := C >= #129;<br> end;<br>begin<br> Result := '';<br> i := 1;<br> while i <= Length(HzChar) do<br> begin<br> if IsDouByte(HzChar) and (Length(HzChar) - i > 0) then<br> len := 2<br> else<br> len := 1;<br> Py := GetHzPyHead(@HzChar, len);<br> Inc(i, len);<br> Result := Result + Py;<br> end;<br>end;<br><br>{ THzSpell }<br><br>function THzSpell.GetHzSpell: String;<br>begin<br> if FSpell = '' then<br> begin<br> Result := GetHzPyFull(FHzText);<br> FSpell := Result;<br> end<br> else Result := FSpell;<br>end;<br><br>function THzSpell.GetPyHead: String;<br>begin<br> if FSpellH = '' then<br> begin<br> Result := GetPyChars(FHzText);<br> FSpellH := Result;<br> end<br> else Result := FSpellH;<br>end;<br><br>class function THzSpell.PyHeadOfHz(Hz: String): String;<br>begin<br> Result := GetPyChars(Hz);<br>end;<br><br>class function THzSpell.PyOfHz(Hz: String): String;<br>begin<br> Result := GetHzPyFull(Hz);<br>end;<br><br>procedure THzSpell.SetHzText(const Value: String);<br>begin<br> FHzText := Value;<br> FSpell := '';<br> FSpellH := '';<br>end;<br><br>end.<br>