unit UnitToChinese;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls;<br><br>type<br> TForm1 = class(TForm)<br> Memo1: TMemo;<br> Button1: TButton;<br> Memo2: TMemo;<br> Button2: TButton;<br> procedure Button1Click(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1 : TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>//通用的常规字符到全角字符的转化函数<br>function ToChinesChar(AChar: Char): WChar;<br>begin<br> Result := WideChar(65248 + Ord(AChar));<br>end;<br><br>//简单的转换函数,复杂的话恐怕要加上脚本标记才行,<br>//因为有很多情况全角字符对应半角字符是多对一的关系<br>//例如:< 《 < 〈 ≮ ≤;或者:[ 【 [ 〔 〖 「 『<br>//<br>function ConvertToChinses(ASource: string): string;<br>var<br> PSource : PChar;<br> TmpChar : string;<br> DoubleFH : Boolean;<br>begin<br> Result := '';<br> PSource := PChar(ASource);<br> DoubleFH := True;<br> while not (PSource^ = #0) do<br> begin<br> case PSource^ of<br> '(', ')', '[', ']', '{', '}', '-', '$', ':', '!', '?', ';':<br> TmpChar := ToChinesChar(PSource^);<br> '.':<br> begin<br> if ((PSource - 1)^ = '.') or ((PSource + 1)^ = '.') then<br> begin<br> TmpChar := '·'; //省略号<br> end<br> else<br> TmpChar := '。'; //句号<br> end;<br> '/':<br> TmpChar := '、'; //顿号<br> '"':<br> begin<br> if DoubleFH then<br> begin<br> TmpChar := '“'; //左双引号<br> end<br> else<br> begin<br> TmpChar := '”'; //右双引号<br> end;<br> DoubleFH := not DoubleFH;<br> end;<br> '''':<br> begin<br> if DoubleFH then<br> begin<br> TmpChar := '‘'; //左单引号<br> end<br> else<br> begin<br> TmpChar := '’'; //右单引号<br> end;<br> DoubleFH := not DoubleFH;<br> end;<br> '<':<br> begin<br> TmpChar := '《'; //左书名号<br> end;<br> '>':<br> begin<br> TmpChar := '》'; //右书名号<br> end;<br> else<br> TmpChar := PSource^;<br> end;<br> Result := Result + TmpChar;<br> Inc(PSource);<br> end;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> Memo2.Text := ConvertToChinses(Memo1.Text);<br>end;<br><br>end.<br><br>