type TForm1 = class(TForm) RxRichEdit1: TRxRichEdit; Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;
type TEditStreamCallBack = function(dwCookie: Longint; pbBuff: PByte; cb: Longint; var pcb: Longint): DWORD; stdcall;
TEditStream = record dwCookie: Longint; dwError: Longint; pfnCallback: TEditStreamCallBack; end;
type TMyRichEdit = TRxRichEdit;
var Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject); var SS: TStringStream; BMP: TBitmap; begin BMP := TBitmap.Create; BMP := Image1.Picture.Bitmap; SS := TStringStream.Create(BitmapToRTF(BMP)); try PutRTFSelection(RxRichEdit1, SS); finally SS.Free; end; end;
function EditStreamInCallback(dwCookie: Longint; pbBuff: PByte; cb: Longint; var pcb: Longint): DWORD; stdcall; // by P. Below var theStream: TStream; dataAvail: LongInt; begin theStream := TStream(dwCookie); with theStream do begin dataAvail := Size - Position; Result := 0; if dataAvail <= cb then begin pcb := read(pbBuff^, dataAvail); if pcb <> dataAvail then Result := UINT(E_FAIL); end else begin pcb := read(pbBuff^, cb); if pcb <> cb then Result := UINT(E_FAIL); end; end; end;
procedure PutRTFSelection(RichEdit: TMyRichEdit; SourceStream: TStream); // by P. Below var EditStream: TEditStream; begin with EditStream do begin dwCookie := Longint(SourceStream); dwError := 0; pfnCallback := EditStreamInCallBack; end; RichEdit.Perform(EM_STREAMIN, SF_RTF or SFF_SELECTION, Longint(@EditStream)); end;
function BitmapToRTF(pict: TBitmap): string; // by D3k var bi, bb, rtf: string; bis, bbs: Cardinal; achar: ShortString; hexpict: string; I: Integer; begin GetDIBSizes(pict.Handle, bis, bbs); SetLength(bi, bis); SetLength(bb, bbs); GetDIB(pict.Handle, pict.Palette, PChar(bi)^, PChar(bb)^); rtf := '{\rtf1 {\pict\dibitmap '; SetLength(hexpict, (Length(bb) + Length(bi)) * 2); I := 2; for bis := 1 to Length(bi) do begin achar := Format('%x', [Integer(bi[bis])]); if Length(achar) = 1 then achar := '0' + achar; hexpict[I - 1] := achar[1]; hexpict[I] := achar[2]; Inc(I, 2); end; for bbs := 1 to Length(bb) do begin achar := Format('%x', [Integer(bb[bbs])]); if Length(achar) = 1 then achar := '0' + achar; hexpict[I - 1] := achar[1]; hexpict[I] := achar[2]; Inc(I, 2); end; rtf := rtf + hexpict + ' }}'; Result := rtf; end;
type TEditStreamCallBack = function(dwCookie: Longint; pbBuff: PByte; cb: Longint; var pcb: Longint): DWORD; stdcall;
TEditStream = record dwCookie: Longint; dwError: Longint; pfnCallback: TEditStreamCallBack; end;
// RichEdit Type type TMyRichEdit = TrxRichEdit;
// EditStreamInCallback callback function function EditStreamInCallback(dwCookie: Longint; pbBuff: PByte; cb: Longint; var pcb: Longint): DWORD; stdcall; // by P. Below var theStream: TStream; dataAvail: LongInt; begin theStream := TStream(dwCookie); with theStream do begin dataAvail := Size - Position; Result := 0; if dataAvail <= cb then begin pcb := read(pbBuff^, dataAvail); if pcb <> dataAvail then Result := UINT(E_FAIL); end else begin pcb := read(pbBuff^, cb); if pcb <> cb then Result := UINT(E_FAIL); end; end; end;
// Insert Stream into RichEdit procedure PutRTFSelection(RichEdit: TMyRichEdit; SourceStream: TStream); // by P. Below var EditStream: TEditStream; begin with EditStream do begin dwCookie := Longint(SourceStream); dwError := 0; pfnCallback := EditStreamInCallBack; end; RichEdit.Perform(EM_STREAMIN, SF_RTF or SFF_SELECTION, Longint(@EditStream)); end;
// Convert Bitmap to RTF Code function BitmapToRTF(pict: TBitmap): string; // by D3k var bi, bb, rtf: string; bis, bbs: Cardinal; achar: ShortString; hexpict: string; I: Integer; begin GetDIBSizes(pict.Handle, bis, bbs); SetLength(bi, bis); SetLength(bb, bbs); GetDIB(pict.Handle, pict.Palette, PChar(bi)^, PChar(bb)^); rtf := '{\rtf1 {\pict\dibitmap '; SetLength(hexpict, (Length(bb) + Length(bi)) * 2); I := 2; for bis := 1 to Length(bi) do begin achar := Format('%x', [Integer(bi[bis])]); if Length(achar) = 1 then achar := '0' + achar; hexpict[I - 1] := achar[1]; hexpict[I] := achar[2]; Inc(I, 2); end; for bbs := 1 to Length(bb) do begin achar := Format('%x', [Integer(bb[bbs])]); if Length(achar) = 1 then achar := '0' + achar; hexpict[I - 1] := achar[1]; hexpict[I] := achar[2]; Inc(I, 2); end; rtf := rtf + hexpict + ' }}'; Result := rtf; end;
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject); var SS: TStringStream; BMP: TBitmap; begin BMP := Image1.Picture.Bitmap; SS := TStringStream.Create(BitmapToRTF(BMP)); try PutRTFSelection(rxRichEdit1, SS); finally SS.Free; end; end;
procedure TForm1.Button2Click(Sender: TObject); begin if Op.Execute then begin currentfile:= Op.FileName; image1.picture.LoadFromFile(currentfile);
end; end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin Image1.picture:= nil; end;
procedure TForm1.Button3Click(Sender: TObject); begin RxRichedit1.Lines.SaveToFile('c:\temp\test.doc'); end;
procedure TForm1.Button4Click(Sender: TObject); begin RxRichedit1.Lines.LoadFromFile('c:\temp\test.doc'); end;
Ved du noget om hvordan man "bruger" richedit? Jeg tænker på fonttyper, sideopbygning og især på "billed-og-tekst-wrap" heh. (Som du kan se fra billedet står teksten kun for oven og for neden).
Hvis du har links til kodeeksempler eller hvor jeg kan læse om det er det super.
procedure TForm1.Button8Click(Sender: TObject); var s: TMemoryStream; begin s:=TMemoryStream.Create(); try if GetAttachedData(s) then s.SaveToFile('hmm.doc'); //RxRichedit1.Lines.LoadFromFile(s); finally s.Free(); end; end;
i rxlib under demo er der en komplet editor som du kan lure koden af. ellers prøv at søge på richedit her: http://www.swissdelphicenter.ch/en/tipsindex.php her er mange eksempler på små kodestumper til løsning af mange funktioner i richedit. husk at læse comment for oven her kan være et bedre eksempel eller rettelse til koden.
følgende parameter....[ ] bold, italic, underline, strikeout
virker som on/off
if fsBold in RichEdit1.SelAttributes.Style then RichEdit1.SelAttributes.Style:= RichEdit1.SelAttributes.Style-[fsBold] else RichEdit1.SelAttributes.Style:= RichEdit1.SelAttributes.Style+[fsBold]; RichEdit1.SetFocus;
her kan du indsætte tal osv...
//uses richedit
procedure TForm1.BulletClick(Sender: TObject); var fmt: TParaformat2; begin FillChar(fmt, SizeOf(fmt), 0); fmt.cbSize := SizeOf(fmt); // The PARAFORMAT2 structure is used to set the numbering style. // This is done through the following structure members: fmt.dwMask := PFM_NUMBERING or PFM_NUMBERINGSTART or PFM_NUMBERINGSTYLE or PFM_NUMBERINGTAB; // Set the following values (bitwise-or them together) to identify // which of the remaining structure members are valid: // PFM_NUMBERING, PFM_NUMBERINGSTART, PFM_NUMBERINGSTYLE, and PFM_NUMBERINGTAB fmt.wNumbering := 1; //0 no numbering or bullets //1 (PFN_BULLET) uses bullet character //2 Uses Arabic numbers (1, 2, 3, ...). //3 Uses lowercase letters (a, b, c, ...). //4 Uses uppercase letters (A, B, C, ...). //5 Uses lowercase Roman numerals (i, ii, iii, ...). //6 Uses uppercase Roman numerals (I, II, III, ...). //7 Uses a sequence of characters beginning with the Unicode // character specified by the wNumberingStart member. fmt.wNumberingStart := 1; // Starting value for numbering. fmt.wNumberingStyle := $200; // Styles for numbering: // 0 : Follows the number with a right parenthesis. 1) // $100 : Encloses the number in parentheses. (1) // $200 : Follows the number with a period. 1. // $300 : Displays only the number. 1 // $400 : Continues a numbered list without applying the next number or bullet. // $8000 : Starts a new number with wNumberingStart. fmt.wNumberingTab := 1440 div 4; // Minimum space between a paragraph number and the paragraph text, in twips
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.