⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rm_wwrichedit.pas

📁 这是一个功能强大
💻 PAS
📖 第 1 页 / 共 3 页
字号:
    liRichEdit.Perform(EM_FORMATRANGE, 0, 0);
    SetMapMode(liFormatRange.hdc, liSaveMapMode);
  end;
end;

procedure TRMwwRichView.ShowRichText(aRender: Boolean);
var
  liCharRange: TCharRange;

  procedure _ShowRichOnPrinter;
  var
    liPrinter: TRMPrinter;
  begin
    liPrinter := GetPrinter;
    DrawRichText(Canvas.Handle, liPrinter.DC, RealRect, liCharRange);
  end;

  procedure _ShowRichOnScreen;
  var
    liMetaFile: TMetaFile;
    liMetaFileCanvas: TMetaFileCanvas;
    liDC: HDC;
    liPrinter: TRMPrinter;
    liBitmap: TBitmap;
    liCanvasRect: TRect;
    liWidth, liHeight: Integer;
  begin
    liPrinter := RMPrinter;
    if liPrinter.DC <> 0 then
      liDC := liPrinter.DC
    else
      liDC := GetDC(0);

    liMetaFile := TMetaFile.Create;
    try
      if aRender then
      begin
        liWidth := mmSaveWidth - mmSaveGapX * 2 - _CalcHFrameWidth(mmSaveFWLeft, mmSaveFWRight);
        liHeight := mmSaveHeight - mmSaveGapY * 2 - _CalcVFrameWidth(mmSaveFWTop, mmSaveFWBottom);
      end
      else
      begin
        liWidth := mmWidth - mmGapLeft * 2 - _CalcHFrameWidth(LeftFrame.mmWidth, RightFrame.mmWidth);
        liHeight := mmHeight - mmGapTop * 2 - _CalcVFrameWidth(TopFrame.mmWidth, BottomFrame.mmWidth);
      end;

      liCanvasRect := Rect(0, 0,
        Round(RMFromMMThousandths_Printer(liWidth, rmrtHorizontal, liPrinter)) + 1,
        Round(RMFromMMThousandths_Printer(liHeight, rmrtVertical, liPrinter)));
      liMetaFile.Width := liCanvasRect.Right - liCanvasRect.Left;
      liMetaFile.Height := liCanvasRect.Bottom - liCanvasRect.Top;

      liMetaFileCanvas := TMetaFileCanvas.Create(liMetaFile, liDC);
      liMetaFileCanvas.Brush.Style := bsClear;

      FEndCharPos := FormatRange(liMetaFileCanvas.Handle, liDC, liCanvasRect, liCharRange, aRender);

      liMetaFileCanvas.Free;
      if liPrinter.DC = 0 then
        ReleaseDC(0, liDC);

      if aRender then
      begin
        if DocMode = rmdmDesigning then
        begin
          liBitmap := TBitmap.Create;
          liBitmap.Width := RealRect.Right - RealRect.Left + 1;
          liBitmap.Height := RealRect.Bottom - RealRect.Top + 1;
          liBitmap.Canvas.StretchDraw(Rect(0, 0, liBitmap.Width, liBitmap.Height), liMetaFile);
          Canvas.Draw(RealRect.Left, RealRect.Top, liBitmap);
          liBitmap.Free;
        end
        else
          Canvas.StretchDraw(RealRect, liMetaFile);
      end;
    finally
      liMetaFile.Free;
    end;
  end;

begin
  FEndCharPos := FStartCharPos;
  liCharRange.cpMax := -1;
  liCharRange.cpMin := FEndCharPos;
  if DocMode = rmdmPrinting then
    _ShowRichOnPrinter
  else
    _ShowRichOnScreen;
end;
{$WARNINGS ON}

procedure TRMwwRichView.Draw(aCanvas: TCanvas);
begin
  BeginDraw(aCanvas);
  CalcGaps;
  with aCanvas do
  begin
    ShowBackground;
    FStartCharPos := 0;
    InflateRect(RealRect, -RMToScreenPixels(mmGapLeft, rmutMMThousandths),
      -RMToScreenPixels(mmGapTop, rmutMMThousandths));
    if (spWidth > 0) and (spHeight > 0) then
      ShowRichText(True);
    ShowFrame;
  end;
  RestoreCoord;
end;

procedure TRMwwRichView.Prepare;
begin
  inherited Prepare;
  FStartCharPos := 0;
end;

procedure TRMwwRichView.GetMemoVariables;
begin
  if OutputOnly then Exit;

  if DrawMode = rmdmAll then
  begin
    Memo1.Assign(Memo);
    InternalOnBeforePrint(Memo1, Self);
    RMwwAssignRich(SRichEdit, FRichEdit);
    if not TextOnly then
      GetRichData(SRichEdit);

	  if (not OutputOnly) and Assigned(OnBeforePrint) then
  	  OnBeforePrint(Self);
  end;
end;

procedure TRMwwRichView.PlaceOnEndPage(aStream: TStream);
var
  n: integer;
begin
  BeginDraw(Canvas);
  if not Visible then Exit;

  GetMemoVariables;
  if DrawMode = rmdmPart then
  begin
    FStartCharPos := FEndCharPos;
    ShowRichText(False);
    n := SRichEdit.GetTextLen - FEndCharPos + 1;
    if n > 0 then
    begin
      SRichEdit.SelStart := FEndCharPos;
      SRichEdit.SelLength := n;
      SRichEdit.SelText := '';
    end;

    SRichEdit.SelStart := 0;
    SRichEdit.SelLength := FSaveCharPos;
    SRichEdit.SelText := '';

    FSaveCharPos := FEndCharPos;
  end;

  aStream.Write(Typ, 1);
  RMWriteString(aStream, ClassName);
  FUseSRichEdit := True;
  try
    SaveToStream(aStream);
  finally
    FUseSRichEdit := False;
  end;
end;

function TRMwwRichView.CalcHeight: Integer;
begin
  FEndCharPos := 0;
  FSaveCharPos := 0;
  Result := 0;
  if not Visible then
    Exit;

  CalcGaps;
  DrawMode := rmdmAll;
  GetMemoVariables;
//  DrawMode := rmdmAfterCalcHeight;

  FStartCharPos := 0;
  CalculatedHeight := RMToMMThousandths(spGapTop * 2 + _CalcVFrameWidth(TopFrame.spWidth, BottomFrame.spWidth), rmutScreenPixels) +
    DoCalcHeight;
  RestoreCoord;
  Result := CalculatedHeight;
end;

function TRMwwRichView.RemainHeight: Integer;
begin
  DrawMode := rmdmAll;
  GetMemoVariables;
//  DrawMode := rmdmAfterCalcHeight;

  FStartCharPos := FEndCharPos + 1;
  ActualHeight := RMToMMThousandths(spGapTop * 2 + _CalcVFrameWidth(TopFrame.spWidth, BottomFrame.spWidth), rmutScreenPixels) +
    DoCalcHeight;
  Result := ActualHeight;
end;

procedure TRMwwRichView.LoadFromStream(aStream: TStream);
var
  b: Byte;
  liSavePos: Integer;
begin
  inherited LoadFromStream(aStream);
  RMReadWord(aStream);
  b := RMReadByte(aStream);
  liSavePos := RMReadInt32(aStream);
  if b > 0 then
    FRichEdit.Lines.LoadFromStream(aStream);
  aStream.Seek(liSavePos, soFromBeginning);
end;

procedure TRMwwRichView.SaveToStream(aStream: TStream);
var
  b: Byte;
  liSavePos, liPos: Integer;
  liRichEdit: TwwDBRichEdit;
begin
  inherited SaveToStream(aStream);
  RMWriteWord(aStream, 0);
  if FUseSRichEdit then
    liRichEdit := SRichEdit
  else
    liRichEdit := FRichEdit;

  if liRichEdit.Lines.Count > 0 then b := 1 else b := 0;
  RMWriteByte(aStream, b);

  liSavePos := aStream.Position;
  RMWriteInt32(aStream, liSavePos);
  if b > 0 then
    liRichEdit.Lines.SaveToStream(aStream);

  liPos := aStream.Position;
  aStream.Seek(liSavePos, soFromBeginning);
  RMWriteInt32(aStream, liPos);
  aStream.Seek(liPos, soFromBeginning);
end;

procedure TRMwwRichView.GetBlob;
var
  liStream: TMemoryStream;
begin
  liStream := TMemoryStream.Create;
  try
    if not ParentReport.Flag_TableEmpty then
      FDataSet.AssignBlobFieldTo(FDataFieldName, liStream);
    FRichEdit.Lines.LoadFromStream(liStream);
  finally
    liStream.Free;
  end;
end;

procedure TRMWWRichView.ShowEditor;
var
  tmpForm: TRMwwRichForm;
begin
  tmpForm := TRMwwRichForm.Create(Application);
  try
    RMwwAssignRich(tmpForm.Editor, RichEdit);
    tmpForm.Editor.MeasurementUnits := RichEdit.MeasurementUnits;
    if tmpForm.ShowModal = mrOK then
    begin
      RMDesigner.BeforeChange;
      RMwwAssignRich(RichEdit, tmpForm.Editor);
      RMDesigner.AfterChange;
    end;
  finally
    tmpForm.Free;
  end;
end;

procedure TRMwwRichView.DefinePopupMenu(aPopup: TRMCustomMenuItem);
begin
  inherited DefinePopupMenu(aPopup);
end;

procedure TRMwwRichView.LoadFromRichEdit(aRichEdit: TwwDBRichEdit);
begin
  RMwwAssignRich(FRichEdit, aRichEdit);
end;

function TRMWWRichView.GetViewCommon;
begin
  Result := '[ww Rich]';
end;

procedure TRMWWRichView.ClearContents;
begin
  FRichEdit.Clear;
  inherited;
end;

{------------------------------------------------------------------------------}
{------------------------------------------------------------------------------}
{TRMWWRichForm}

procedure TRMwwRichForm.Localize;
begin
  Font.Name := RMLoadStr(SRMDefaultFontName);
  Font.Size := StrToInt(RMLoadStr(SRMDefaultFontSize));
  Font.Charset := StrToInt(RMLoadStr(SCharset));

//  Caption := RMLoadStr(rmRes + 560);
  RMSetStrProp(btnFileNew, 'Hint', rmRes + 155);
  RMSetStrProp(btnFileOpen, 'Hint', rmRes + 561);
  RMSetStrProp(btnFileSave, 'Hint', rmRes + 562);
  RMSetStrProp(btnUndo, 'Hint', rmRes + 94);
  RMSetStrProp(btnRedo, 'Hint', rmRes + 95);
  RMSetStrProp(btnFind, 'Hint', rmRes + 582);
  RMSetStrProp(btnFontBold, 'Hint', rmRes + 564);
  RMSetStrProp(btnFontItalic, 'Hint', rmRes + 565);
  RMSetStrProp(btnFontUnderline, 'Hint', rmRes + 569);
  RMSetStrProp(btnAlignLeft, 'Hint', rmRes + 566);
  RMSetStrProp(btnAlignCenter, 'Hint', rmRes + 567);
  RMSetStrProp(btnAlignRight, 'Hint', rmRes + 568);
  RMSetStrProp(btnBullets, 'Hint', rmRes + 570);
  RMSetStrProp(btnInsertField, 'Hint', rmRes + 575);
  RMSetStrProp(btnCut, 'Hint', rmRes + 91);
  RMSetStrProp(btnCopy, 'Hint', rmRes + 92);
  RMSetStrProp(btnPaste, 'Hint', rmRes + 93);

  ItmCut.Caption := btnCut.Hint;
  ItmCopy.Caption := btnCopy.Hint;
  ItmPaste.Caption := btnPaste.Hint;
  RMSetStrProp(MenuFile, 'Caption', rmRes + 154);
  RMSetStrProp(ItemFileNew, 'Caption', rmRes + 155);
  RMSetStrProp(ItemFileOpen, 'Caption', rmRes + 156);
  RMSetStrProp(ItemFileSaveAs, 'Caption', rmRes + 157);
  RMSetStrProp(ItemFilePrint, 'Caption', rmRes + 159);
  RMSetStrProp(ItemFileExit, 'Caption', rmRes + 162);
  RMSetStrProp(MenuEdit, 'Caption', rmRes + 163);
  RMSetStrProp(ItemEditUndo, 'Caption', rmRes + 164);
  RMSetStrProp(ItemEditRedo, 'Caption', rmRes + 165);
  RMSetStrProp(ItemEditCut, 'Caption', rmRes + 166);
  RMSetStrProp(ItemEditCopy, 'Caption', rmRes + 167);
  RMSetStrProp(ItemEditPaste, 'Caption', rmRes + 168);
  RMSetStrProp(ItemEditSelectAll, 'Caption', rmRes + 170);
  RMSetStrProp(ItemEditFind, 'Caption', rmRes + 582);
  RMSetStrProp(ItemEditFindNext, 'Caption', rmRes + 583);
  RMSetStrProp(ItemEditReplace, 'Caption', rmRes + 584);
  RMSetStrProp(MenuInsert, 'Caption', rmRes + 586);
  RMSetStrProp(ItemInserObject, 'Caption', rmRes + 587);
  RMSetStrProp(ItemInsertPicture, 'Caption', rmRes + 588);
  RMSetStrProp(ItemInsertField, 'Caption', rmRes + 575);
  RMSetStrProp(MenuFormat, 'Caption', rmRes + 589);
  RMSetStrProp(ItemFormatFont, 'Caption', rmRes + 576);
  RMSetStrProp(ItemFormatParagraph, 'Caption', rmRes + 852);

  btnOK.Hint := RMLoadStr(rmRes + 573);
  btnCancel.Hint := RMLoadStr(rmRes + 574);
end;

procedure TRMwwRichForm.SelectionChange(Sender: TObject);
begin
  RefreshControls;
end;

procedure TRMwwRichForm.RefreshControls;
var
  haveSelection, haveText: boolean;
  lFont: TFont;
  lFontHeight: Integer;
begin
  try
    FUpdating := True;
    FRuler.UpdateInd;
    btnFontBold.down := fsBold in Editor.SelAttributes.Style;
    btnFontUnderline.down := fsUnderline in Editor.SelAttributes.Style;
    btnFontItalic.down := fsItalic in Editor.SelAttributes.Style;
    FCmbFont.FontName := Editor.SelAttributes.Name;

    lFont := TFont.Create;
    lFont.Size := Editor.SelAttributes.Size;
    lFontHeight := lFont.Height;
    lFont.Free;
    RMSetFontSize(TComboBox(FCmbFontSize), lFontHeight, Editor.SelAttributes.Size);

    btnBullets.down := Editor.Paragraph.Numbering = nsBullet;
//	  btnHighlight.Down:= (Editor.GetTextBackgroundColor <> 0) and
//                         (Editor.GetTextBackgroundColor <> ColorToRGB(clWindow));
    if ord(Editor.Paragraph.Alignment) = PFA_FULLJUSTIFY - 1 then
      btnJustify.Down := True
    else
    begin
      case Editor.Paragraph.Alignment of
        taLeftJustify: btnAlignLeft.Down := True;
        taCenter: btnAlignCenter.Down := True;
        taRightJustify: btnAlignRight.Down := True;
      end;
    end;

    ItemEditPaste.Enabled := Editor.CanPaste and (not Editor.readonly);
    btnPaste.Enabled := ItemEditPaste.Enabled;
    ItmPaste.Enabled := ItemEditPaste.Enabled;
    ItemEditUndo.Enabled := Editor.CanUndo;
    ItemEditRedo.Enabled := Editor.CanRedo;
    btnUndo.Enabled := ItemEditUndo.Enabled;
    btnRedo.Enabled := ItemEditRedo.Enabled;

    haveSelection := Editor.SelLength > 0; //Editor.CanCut;
    haveText := (Editor.Lines.Count > 1) or
      (Editor.Lines.Count = 1) and (Editor.Lines[0] <> '');
    ItemEditCut.Enabled := haveSelection and (not Editor.readonly);
    btnCut.Enabled := ItemEditCut.Enabled;
    ItmCut.Enabled := ItemEditCut.Enabled;
    ItemEditCopy.Enabled := haveSelection;
    btnCopy.Enabled := ItemEditCopy.Enabled;
    ItmCopy.Enabled := ItemEditCopy.Enabled;
    ItemEditSelectAll.Enabled := haveText;
    ItemEditFind.Enabled := haveText;
    btnFind.Enabled := ItemEditFind.Enabled;
    ItemEditFindNext.Enabled := Editor.CanFindNext;
    ItemEditReplace.Enabled := haveText and (not Editor.readOnly);
  finally
    FUpdating := False;
  end;
end;

procedure TRMwwRichForm.FocusEditor;
begin
  with Editor do
    if CanFocus then
      SetFocus;
end;

procedure TRMwwRichForm.SetFileName(const FileName: string);
begin
  FFileName := FileName;
  Editor.Title := ExtractFileName(FileName);
end;

{ Event Handlers }

procedure TRMwwRichForm.FormCreate(Sender: TObject);
var
  i, liOffset: Integer;
  s, s1: string;
begin
  Localize;
  Editor := TwwDBRichEdit.Create(Self);
  with Editor do
  begin
    Parent := Self;
    Align := alClient;
    HideSelection := False;
    PopupMenu := EditPopupMenu;
    WantTabs := True;
    ScrollBars := ssBoth;

    OnSelectionChange := SelectionChange;
    OnChange := RichEditChange;
  end;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -