http://blog.sina.com.cn/game7788
夸父
段落縮進
在輸入文字的過程中,如果按下回車鍵,新成生的段落會與當前段落對齊,下面例子只處理了回車,如果要實現自動換行時也達到同樣效果,可以在其文字錄入事情中作相同處理!
實現過程主要是通得到當前光標所在段落(行)前面的空格數,然后在新段落頭中插入同相的數目的空格
function GetLeadingSpacesCount(rve: TCustomRichViewEdit): Integer;
var StartItemNo, ItemNo, i: Integer;
s: String;
begin
rve := rve.TopLevelEditor;
ItemNo := rve.CurItemNo;
while not rve.IsParaStart(ItemNo) do
dec(ItemNo);
Result := 0;
StartItemNo := ItemNo;
while ItemNo
if (ItemNo>StartItemNo) and rve.IsParaStart(ItemNo) then
exit; //如果在段落頭則不處理
if rve.GetItemStyle(ItemNo)<0 then //如果不是文字也不處理
exit;
s := rve.GetItemText(ItemNo);
for i := 1 to Length(s) do
if s[i]=' ' then
inc(Result) //計算空格數
else
exit;
inc(ItemNo);
end;
end;
//通過空格數返回字符個數,空格也是字符
function GetSpaces(Count: Integer): String;
var i: Integer;
begin
SetLength(Result, Count);
for i := 1 to Count do
Result[i] := ' ';
end;
最后在KeyDown下面實現
if Key=VK_RETURN then begin
RichViewEdit1.InsertText(#13+GetSpaces(GetLeadingSpacesCount(RichViewEdit1)));
Key := 0;
end;
|