rxrichedit 内容 转换 成 HTMLEDT

unit MainForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, Grids, DBGrids, StdCtrls, DB, ADODB, ExtCtrls,
  RxRichEd, Buttons, ExtDlgs, RVScroll, RichView, RVEdit, OleCtrls,
  SHDocVw, uHtmlEdit, RVStyle;

type
  TMainFrm = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    DBGrid_ITEM: TDBGrid;
    DataSource_item: TDataSource;
    ADOQuery_Item: TADOQuery;
    RxRichEdit1: TRxRichEdit;
    Panel1: TPanel;
    Label1: TLabel;
    btn_save: TBitBtn;
    ADOConnection1: TADOConnection;
    btn_pic: TBitBtn;
    OpenPictureDialog1: TOpenPictureDialog;
    GroupBox3: TGroupBox;
    ADOQGlo: TADOQuery;
    DBGrid_OBJ: TDBGrid;
    DataSource_OBJ: TDataSource;
    ADOQuery_OBJ: TADOQuery;
    Label2: TLabel;
    Label3: TLabel;
    edt_id: TEdit;
    TabSheet3: TTabSheet;
    BitBtn1: TBitBtn;
    RVStyle1: TRVStyle;
    HtmlEdit1: THtmlEdit;
    ADOQuery1: TADOQuery;
    ADOQuery2: TADOQuery;
    RichViewEdit1: TRichViewEdit;
    DBGrid_HTML: TDBGrid;
    DataSource_html: TDataSource;
    ADOQuery_html: TADOQuery;
    HtmlEdit_TXT: THtmlEdit;
    Label4: TLabel;
    Label5: TLabel;
    edt_HTML_ID: TEdit;
    aq_Tmp: TADOQuery;
    ADOQuery3: TADOQuery;
    Button1: TButton;
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure btn_picClick(Sender: TObject);
    procedure btn_saveClick(Sender: TObject);
    procedure DBGrid_ITEMCellClick(Column: TColumn);
    procedure BitBtn1Click(Sender: TObject);
    procedure DBGrid_HTMLCellClick(Column: TColumn);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure refresh_Grid_item;
    procedure refresh_Grid_obj;
    procedure InitConnection();
    function ChangeItemText(var fromrichview: TRichViewEdit; var tohtml: THtmlEdit): Boolean;
    function saveItemPic(var fromrichview: TRichViewEdit; var itemcode: string): Boolean;
  end;

var
  MainFrm: TMainFrm;

implementation

{$R *.dfm}

uses Clipbrd, jpeg, RVTypes, RVItem;

procedure TMainFrm.FormCreate(Sender: TObject);
var
  i: integer;
begin
  InitConnection;

  for i := 0 to ComponentCount - 1 do
    if (Components[i] is TCustomADODataSet) then
      TCustomADODataSet(Components[i]).Connection := ADOConnection1;
end;

procedure TMainFrm.FormShow(Sender: TObject);
begin
  refresh_Grid_item;
end;


{======================================================================}

procedure TMainFrm.InitConnection();
begin
  ADOConnection1.Close;
  ADOConnection1.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source='
    + ExtractFilePath(Application.ExeName)
    + 'ITEMbank.mdb;Persist Security Info=False';

  ADOConnection1.LoginPrompt := false;
  try
    ADOConnection1.Connected := true;

  except on E: exception do
    begin
      showmessage('数据库连接失败,原因:' + #13#10 + E.message);
      Application.Terminate;
    end;
  end;
end;

procedure TMainFrm.refresh_Grid_item;
begin
  ADOQuery_Item.Close;
  ADOQuery_Item.SQL.Clear;
  ADOQuery_Item.SQL.Text := 'select * from subject_item';
  ADOQuery_Item.Open;

  ADOQuery_OBJ.Close;
  ADOQuery_OBJ.SQL.Clear;
  ADOQuery_OBJ.SQL.Text := 'select * from subject_item_obj';
  ADOQuery_OBJ.Open;
end;

procedure TMainFrm.btn_picClick(Sender: TObject);
var
  Pict: TPicture;
begin
  RxRichEdit1.SetFocus;
  with OpenPictureDialog1 do
  begin
    if Execute then
    begin
      Pict := TPicture.Create;
      try
        Pict.LoadFromFile(FileName);
        Clipboard.Assign(Pict);
        RxRichEdit1.PasteFromClipboard;
      finally
        Pict.Free;
      end;
    end;
  end;
end;

procedure TMainFrm.btn_saveClick(Sender: TObject);
var
  id: string;
  MemStream: TMemoryStream;
  MS: TMemoryStream;
  BlobStream: TADOBlobStream;
begin
  if RxRichEdit1.Lines.Text = '' then
  begin
    showmessage('正文不能为空');
    RxRichEdit1.SetFocus;
    abort;
  end;


  if edt_id.Text = '' then
  begin
    ADOQGlo.Close;
    ADOQGlo.SQL.Text := ' select * from subject_item where id=0';
    ADOQGlo.Open;
    ADOQGlo.append;

  end
  else
  begin
    id := edt_id.Text;
    ADOQGlo.Close;
    ADOQGlo.SQL.Text := ' select * from subject_item where id=' + id;
    ADOQGlo.Open;
    ADOQGlo.Edit;

  end;


    //text  正文
  if RxRichEdit1.Text = '' then
    ADOQGlo.FieldByName('txt').Assign(nil)
  else
  begin
    MS := TMemoryStream.Create;
    RxRichEdit1.Lines.SaveToStream(MS);
    BlobStream := TADOBlobStream.Create(TBlobField(ADOQGlo.FieldByName('txt')), bmWrite);
    try
      BlobStream.Seek(0, soFromBeginning);
      MS.Position := 0;
      BlobStream.CopyFrom(MS, MS.Size);
    finally
      MS.Free;
      BlobStream.Free;
    end;
  end;

  ADOQGlo.Post;

  refresh_Grid_item;
end;

procedure TMainFrm.DBGrid_ITEMCellClick(Column: TColumn);
var
  MemStream: TMemoryStream;
  MS: TMemoryStream;
  BlobStream: TADOBlobStream;
begin
  if not ADOQuery_Item.Active then exit;
  if ADOQuery_Item.RecordCount <= 0 then exit;

  edt_id.Text := ADOQuery_Item.FieldByName('id').AsString;

  MemStream := TMemoryStream.Create;
  with ADOQGlo do
  begin
    BlobStream := TADOBlobStream.Create(TBlobField(ADOQuery_Item.FieldByName('txt')), bmRead);
    try
      MemStream.CopyFrom(BlobStream, BlobStream.Size);
      MemStream.Position := 0;

      RxRichEdit1.Clear;
      RxRichEdit1.Lines.LoadFromStream(MemStream);
    finally
      BlobStream.Free;
      MemStream.Free;
    end;
  end;
end;

procedure TMainFrm.BitBtn1Click(Sender: TObject);
var
  MemStream: TMemoryStream;
  MS: TMemoryStream;
  BlobStream: TADOBlobStream;
  itemcode: string;
begin

  ADOQuery1.Close;
  ADOQuery1.SQL.Clear;
  ADOQuery1.SQL.Text := 'delete from subject_item_obj ';
  ADOQuery1.ExecSQL;

  ADOQuery1.Close;
  ADOQuery1.SQL.Clear;
  ADOQuery1.SQL.Text := 'delete from subject_item_html';
  ADOQuery1.ExecSQL;

  ADOQuery1.Close;
  ADOQuery1.SQL.Clear;
  ADOQuery1.SQL.Text := 'select * from subject_item';
  ADOQuery1.open;
  ADOQuery1.First;

  while not ADOQuery1.Eof do
  begin

    MemStream := TMemoryStream.Create;

    BlobStream := TADOBlobStream.Create(TBlobField(ADOQuery1.FieldByName('txt')), bmRead);
    try
      MemStream.CopyFrom(BlobStream, BlobStream.Size);
      MemStream.Position := 0;

      RichViewEdit1.Clear;
      RichViewEdit1.LoadFromStream(MemStream, rvynaAuto);
    finally
      BlobStream.Free;
      MemStream.Free;
    end;



    //转换过程

    ChangeItemText(RichViewEdit1, HtmlEdit1);

    //转换完成,写入主表
    ADOQGlo.Close;
    ADOQGlo.SQL.Text := ' select * from subject_item_html where id=0';
    ADOQGlo.Open;
    ADOQGlo.append;


       //text  正文
    if HtmlEdit1.Text = '' then
      ADOQGlo.FieldByName('txt').Assign(nil)
    else
    begin
      ADOQGlo.FieldByName('txt').AsString := HtmlEdit1.HTML;
    end;
    ADOQGlo.Post;

    itemcode := ADOQGlo.FieldByName('id').AsString;

    saveItemPic(RichViewEdit1, itemcode) ;






    ADOQuery1.Next;

  end;

  refresh_Grid_obj;
  ShowMessage('处理完成');

end;

function TMainFrm.saveItemPic(var fromrichview: TRichViewEdit; var itemcode: string): Boolean;
var
  HTMLSaveOptions: TRVSaveOptions;
  MS, MemStream: TMemoryStream;
  itemno: integer;
  agr: TGraphic;
  pname: TRVAnsiString;
  ITag: integer;
  algin: TRVVAlign;
  filename, altname: string;
  BlobStream: TADOBlobStream;
begin
  Result := false;
    //处理richview中图片读取到流存到数据库中,CODE字段按顺序自动生成
  for itemno := 0 to fromrichview.ItemCount - 1 do
  begin
    if RichViewEdit1.GetItemStyle(itemno) = rvsPicture then
    begin
      fromrichview.GetPictureInfo(itemno, pname, agr, algin, itag);
      fromrichview.GetItemExtrastrProperty(itemno, rvespAlt, filename);

        {======================}
        //获取图片并存入图片表
      ADOQuery2.Close;
      ADOQuery2.SQL.Text := ' select * from subject_item_obj where id=0';
      ADOQuery2.Open;
      ADOQuery2.append;

      ADOQuery2.FieldByName('code').AsString := itemcode;
      ADOQuery2.FieldByName('filename').AsString := filename;
      MS := TMemoryStream.Create;
      agr.SaveToStream(MS);
      BlobStream := TADOBlobStream.Create(TBlobField(ADOQuery2.FieldByName('txt')), bmWrite);
      try
        BlobStream.Seek(0, soFromBeginning);
        MS.Position := 0;
        BlobStream.CopyFrom(MS, MS.Size);
      finally
        MS.Free;
        BlobStream.Free;
      end;


      ADOQuery2.Post;
      {=============================}

    end;
  end;

end;

function TMainFrm.ChangeItemText(var fromrichview: TRichViewEdit; var tohtml: THtmlEdit): Boolean;
var
  HTMLSaveOptions: TRVSaveOptions;
  MS, MemStream: TMemoryStream;
  itemno: integer;
  agr: TGraphic;
  pname: TRVAnsiString;
  ITag: integer;
  algin: TRVVAlign;
  filename, altname: string;
  BlobStream: TADOBlobStream;
begin

  Result := false;

  try
    MemStream := TMemoryStream.Create;
    //altname
    altname := FormatDateTime('yyyymmddhhnnsszzz', now);
    for itemno := 0 to fromrichview.ItemCount - 1 do
      if RichViewEdit1.GetItemStyle(itemno) = rvsPicture then
      begin
        fromrichview.SetItemExtraStrProperty(itemno, rvespAlt, altname);
         //fromrichview.GetItemExtrastrProperty(itemno,rvespAlt,altname);
      end;

    // HTML with CSS
    HTMLSaveOptions := [rvsoImageSizes, rvsoUseCheckpointsNames];
    fromrichview.SaveHTMLToStreamEx(MemStream, '', '', '', '', '', '', HTMLSaveOptions);

    //htmledit 加载试题
    MemStream.Position := 0;
    tohtml.Clear;
    tohtml.LoadFromStream(MemStream);
  finally
    MemStream.Free;
  end;
end;

procedure TMainFrm.refresh_Grid_obj;
begin
  ADOQuery_html.Close;
  ADOQuery_html.SQL.Clear;
  ADOQuery_html.SQL.Text := 'select * from subject_item_html';
  ADOQuery_html.Open;

  ADOQuery_OBJ.Close;
  ADOQuery_OBJ.SQL.Clear;
  ADOQuery_OBJ.SQL.Text := 'select * from subject_item_obj';
  ADOQuery_OBJ.Open;
end;

procedure TMainFrm.DBGrid_HTMLCellClick(Column: TColumn);
var
  id, answer: string;
  i, A: Integer;
  BlobStream: TADOBlobStream;
  MemStream: TMemoryStream;
  after, before: string;


   function DownLoadFile(ItemCode: string; filecode: string; aq_Tmp: TADOQuery): string;
  var
    fullname: string;
    newFileName: string;
    MS: TMemoryStream;
    BlobStream: TADOBlobStream;
  begin
    result := '';

    aq_Tmp.Close; // 树
    aq_Tmp.sql.clear;
    aq_Tmp.sql.Text := ' select * from  subject_item_obj where code=''' + ItemCode + ''' and filename=''' + filecode + '''';
    aq_Tmp.Open;
    aq_Tmp.First;

    while not aq_Tmp.Eof do
    begin
 

      newFileName := ExtractFilePath(Application.ExeName)  + aq_Tmp.FieldByName('filename').AsString+'.bkp';
      fullname := 'file:///' + newFileName;
      fullname := StringReplace(fullname, '\', '/', [rfreplaceall]);

      if (FileExists(newFileName)) then
        DeleteFile(newFileName);

      MS := TMemoryStream.Create;
      BlobStream := TADOBlobStream.Create(TBlobField(aq_Tmp.FieldByName('txt')), bmRead);
      try
        MS.CopyFrom(BlobStream, BlobStream.Size);
        MS.Position := 0;
        MS.SaveToFile(newFileName);
      finally
        MS.Free;
        BlobStream.Free;
      end;

      aq_Tmp.Next;
      result := fullname;
    end;

  end;

  procedure BeforeShow(var html_edit: THtmlEdit; ItemCode: string);
  var
    index: Integer;
    newname, name, src,alt: string;
  begin
    if html_edit.Edit.getImageCount() > 0 then
    begin
      for index := 0 to html_edit.Edit.getImageCount() - 1 do
      begin
        if html_edit.Edit.getImageName(index, name, src,alt) then
        begin
          newname := DownLoadFile(ItemCode, alt, ADOQuery3);

          html_edit.Edit.setImageAttribute(name,alt, newname);
        end;
      end;

    end;
  end;

begin
  if not ADOQuery_html.Active then exit;
  if ADOQuery_html.RecordCount <= 0 then exit;

  edt_HTML_ID.Text := ADOQuery_html.FieldByName('id').AsString;

  HtmlEdit_TXT.Clear;
  HtmlEdit_TXT.HTML := ADOQuery_html.FieldByName('txt').AsString;

  BeforeShow(HtmlEdit_TXT, ADOQuery_html.FieldByName('id').AsString);

 
end;

procedure TMainFrm.Button1Click(Sender: TObject);
begin
 
 Memo1.Clear;
 Memo1.Text :=  HtmlEdit_TXT.HTML;

end;

end.
														 

rxrichedit 内容 转换 成 HTMLEDT_第1张图片 rxrichedit 内容 转换 成 HTMLEDT_第2张图片

你可能感兴趣的:(Delphi)