http://blog.sina.com.cn/game7788
夸父
下面例子实现ms word风格的段落整体对齐功能,当在段落头按下tab键时,段落整体向右收缩,当按下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;
//实现过程
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; // 内容为空,不作处理
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;
|