http://blog.sina.com.cn/game7788
夸父
下面例子實(shí)現(xiàn)ms word風(fēng)格的段落整體對齊功能,當(dāng)在段落頭按下tab鍵時,段落整體向右收縮,當(dāng)按下backspace鍵時,段落整體向左收縮
//定義向右收縮樣式
ChangeFirstIndentConversion(Sender: TCustomRichViewEdit;
StyleNo, UserData: Integer; AppliedToText: Boolean;
var NewStyleNo: Integer);
var ParaStyle: TParaInfo;
begin
ParaStyle := TParaInfo.Create(nil);
ParaStyle.Assign(Sender.Style.ParaStyles[StyleNo]);
ParaStyle.FirstIndent := UserData;
ParaStyle.Standard := False;
NewStyleNo := Sender.Style.ParaStyles.FindSuchStyle(StyleNo, ParaStyle,
RVAllParaInfoProperties);
if NewStyleNo<0 then begin
Sender.Style.ParaStyles.Add.Assign(ParaStyle);
NewStyleNo := Sender.Style.ParaStyles.Count-1;
end;
ParaStyle.Free;
end;
//定義向左收縮樣式
ChangeLeftIndentConversion(Sender: TCustomRichViewEdit;
StyleNo, UserData: Integer; AppliedToText: Boolean;
var NewStyleNo: Integer);
var ParaStyle: TParaInfo;
begin
ParaStyle := TParaInfo.Create(nil);
ParaStyle.Assign(Sender.Style.ParaStyles[StyleNo]);
ParaStyle.LeftIndent := UserData;
if ParaStyle.LeftIndent=0 then
ParaStyle.FirstIndent := 0;
ParaStyle.Standard := False;
NewStyleNo := Sender.Style.ParaStyles.FindSuchStyle(StyleNo, ParaStyle,
RVAllParaInfoProperties);
if NewStyleNo<0 then begin
Sender.Style.ParaStyles.Add.Assign(ParaStyle);
NewStyleNo := Sender.Style.ParaStyles.Count-1;
end;
ParaStyle.Free;
end;
//實(shí)現(xiàn)過程
function ChangeIndent(rve: TCustomRichViewEdit;
Step, Max: Integer): Boolean;
var OldParaStyleConversion: TRVStyleConversionEvent;
FirstIndent, LeftIndent: Integer;
ListNo, ListLevel, StartFrom: Integer;
Reset: Boolean;
begin
Result := False;
OldParaStyleConversion := rve.OnParaStyleConversion;
try
rve := rve.TopLevelEditor;
if rve.SelectionExists then
exit;
if (Step>0) and (rve.OffsetInCurItem<=rve.GetOffsBeforeItem(rve.CurItemNo)) and
(rve.CurItemNo>0) and (rve.GetItemStyle(rve.CurItemNo-1)=rvsListMarker) then begin
// 改變段落位置
rve.GetListMarkerInfo(rve.CurItemNo, ListNo, ListLevel, StartFrom, Reset);
if (ListNo>=0) and (ListNo<rve.Style.ListStyles.Count) and
(ListLevel+1<rve.Style.ListStyles[ListNo].Levels.Count) then begin
rve.ChangeListLevels(+1);
Result := True;
end;
exit;
end;
if (rve.OffsetInCurItem>rve.GetOffsBeforeItem(rve.CurItemNo)) or
not rve.IsParaStart(rve.CurItemNo) then
exit; // 不在段落開頭不作處理
if (rve.OffsetInCurItem>=rve.GetOffsAfterItem(rve.CurItemNo)) and
((rve.CurItemNo=rve.ItemCount-1) or rve.IsParaStart(rve.CurItemNo)) then
exit; // 內(nèi)容為空,不作處理
FirstIndent := rve.Style.ParaStyles[rve.GetItemPara(rve.CurItemNo)].FirstIndent;
LeftIndent := rve.Style.ParaStyles[rve.GetItemPara(rve.CurItemNo)].LeftIndent;
if Step>0 then begin
if FirstIndent=0 then begin
rve.OnParaStyleConversion := ChangeFirstIndentConversion;
rve.ApplyParaStyleConversion(Step);
Result := True;
end
else begin
inc(LeftIndent, Step);
if LeftIndent>Max then
LeftIndent := Max;
if LeftIndent>rve.Style.ParaStyles[rve.GetItemPara(rve.CurItemNo)].LeftIndent then begin
rve.OnParaStyleConversion := ChangeLeftIndentConversion;
rve.ApplyParaStyleConversion(LeftIndent);
Result := True;
end;
end
end
else begin
if FirstIndent>0 then begin
rve.OnParaStyleConversion := ChangeFirstIndentConversion;
rve.ApplyParaStyleConversion(0);
Result := True;
end
else begin
inc(LeftIndent, Step);
if LeftIndent<0 then
LeftIndent := 0;
if (LeftIndent
(FirstIndent<>0) then begin
rve.OnParaStyleConversion := ChangeLeftIndentConversion;
rve.ApplyParaStyleConversion(LeftIndent);
Result := True;
end;
end;
end;
finally
rve.OnParaStyleConversion := OldParaStyleConversion;
end;
end;
//最后在keypress下面處理
var step: Integer;
begin
if Key=#9 then begin
step := 48;
if GetAsyncKeyState(VK_SHIFT)and$8000<>0 then
step := -step;
if ChangeIndent(TCustomRichViewEdit(Sender), step, 480) then
Key := #0;
end;
end;
|