Autor Beitrag
msphp
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 20

WIN 2000
D6 Pers
BeitragVerfasst: Sa 07.02.04 16:06 
Altes Problem neu aufgerollt: Ich möchte einen Text im Programm formatieren, und dann den aufbereiteten Text einfügen bzw. anhängen in einem RichEdit. Die Formatierung ist kein Problem, aber mit dem einfügen happerts noch ein bischen: ich nehme zum Beispiel folgenden String:
{\rtf1\ansi\ansicpg1252\deff0\deflang1031{\fonttbl{\f0\fswiss\fcharset0 Arial;}} {\colortbl ;\red0\green0\blue255;} \viewkind4\uc1\pard\f0\fs20 Hallo\par \par Ich sage \b Hallo User\b0\par \par \i Kursiv\par \par \b\i0 Fett\par \b0 einzeilig\par \cf1 Blau\par }
und will ihn mit richedit.lines.add hinzufügen. Das klappt ja eigentlich ganz gut, kommt perfekt formatiert heraus, aber es gibt jedes Mal eine Fehlermeldung! "Fehler bei einfügen von Richedit-Zeile" :shock: das ist ärgerlich, da sie total überflüssig ist. Es wird nämlich korrekt dargestellt.
Was lässt sich da machen :?:
Chris1308
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 220

Win XP Prof.
D2005 Prof, D7 Prof, D7 Pers
BeitragVerfasst: Mi 11.02.04 21:15 
Hi,
wenn es eine Delphi-Fehlermeldung ist, könnte man sie so abfangen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
try
  {füge hier die Zeile ein}
except
  // do nothing...
end;

Allerdings ist das nicht sonderlich ratsam, weil in diesem Fall alle Fehler übergangen werden. Wenn du jedoch den Namen der Exception kennst, dann kannst du so nur diesen Fehler übergehen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
// ...
except
  on EDerName do: Application.ProcessMessages;
end;


Hope that helps,
Chris
obbschtkuche
Gast
Erhaltene Danke: 1



BeitragVerfasst: Mi 11.02.04 21:51 
Das sollte ohne Exception funzen:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
procedure AddToRichedit(RE: TRichEdit; s: string);
begin
 RE.SelStart := Length(RE.Text);
 SendMessage(RE.Handle, EM_REPLACESEL, 0, DWORD(PChar(s)));
end;
msphp Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 20

WIN 2000
D6 Pers
BeitragVerfasst: Do 12.02.04 09:54 
Das ist perfekt :D

Vielen dank für die Prozedur AddToRichedit, sie funktioniert sehr gut. Einfach klasse.
Noch eine Bemerkung zu dem Tip mit 'try..except': das habe ich bereits ausprobiert, aber ohne Erfolg. ich habe das ganze in eine Try konstruktion eingefügt, aber ohne Erfolg. Der 'Fehler' wurde trotzdem angezeigt.

Nochmals vielen Dank für eure Hilfe

Michael
Sven
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 314


D6 Ent, K3 Pro (patched)
BeitragVerfasst: Do 12.02.04 10:16 
Um Text direkt in ein RTF-Doku zu schrieben habe ich mal folgende Routinen verwendet. Ich hatte nämlich mal auch das Problem und dann wurde mir mit dem obigen Code auf dem SwissDelphiCenter-Forum geholfen. So, ich hoffe es hilft Dir jetzt :D
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
unit DirectRTF;

interface

uses
  Windows, SysUtils, Types, Classes,  RichEdit, RxRichEd;

// Stream Callback function
type
  TEditStreamCallBack = function(dwCookie: Longint; pbBuff: PByte;
    cb: Longint; var pcb: Longint): DWORD;
  stdcall;

  TEditStream = record
    dwCookie: Longint;
    dwError: Longint;
    pfnCallback: TEditStreamCallBack;
  end;

const
  EditStreamCookieDoOut = 0;
  EditStreamCookieDoIn = 1;

// RichEdit Type
type
  TMyRichEdit = TRxRichEdit;

var
  EditStreamCallBackData: PChar;
  EditStreamCallBackPos: Longint;

procedure PutRTFSelection(RichEdit: TMyRichEdit; SourceStream: TStream);
function StreamOutRtf(const RichEdit: HWND; out Stream: PChar; const Sel: boolean): Cardinal;
function StreamInRtf(const RichEdit: HWND; const Stream: PChar): Cardinal;

implementation

// 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; { EditStreamInCallback }

function EditStreamCallBack(dwCookie: Longint; pbBuff: PByte; cb: Longint;
  var pcb: Longint): DWORD; stdcall;
var
  Size: Integer;
  Data: PChar;
begin 
  case dwCookie of
    EditStreamCookieDoOut: 
      begin 
        if EditStreamCallBackData = nil then 
        begin 
          Data := GetMemory(cb); 
          if Data <> nil then 
            try 
              CopyMemory(Data, pbBuff, cb); 
              EditStreamCallBackData := Data; 
              EditStreamCallBackPos := cb; 
              pcb := cb; 
              Result := ERROR_SUCCESS; 
            except 
              Result := ERROR_CANNOT_COPY; 
            end
          else 
          begin 
            Result := ERROR_NOT_ENOUGH_MEMORY; 
          end; 
        end 
        else 
        begin 
          Data := GetMemory(EditStreamCallBackPos + cb); 
          if Data <> nil then 
            try 
              CopyMemory(Data, EditStreamCallBackData, EditStreamCallBackPos); 
              CopyMemory(@Data[EditStreamCallBackPos], pbBuff, cb); 
              FreeMemory(EditStreamCallBackData); 
              EditStreamCallBackData := Data; 
              EditStreamCallBackPos := EditStreamCallBackPos + cb; 
              pcb    := cb; 
              Result := ERROR_SUCCESS;
            except 
              Result := ERROR_CANNOT_COPY; 
            end 
          else
          begin 
            Result := ERROR_NOT_ENOUGH_MEMORY;
          end; 
        end; 
      end; 
    EditStreamCookieDoIn: 
      begin 
        if EditStreamCallBackData <> nil then 
        begin 
          Size := lstrlen(EditStreamCallBackData) + 1 - EditStreamCallBackPos; 
          if Size > 0 then 
          begin 
            if cb < Size then 
              pcb := cb 
            else 
              pcb := Size; 
            try 
              CopyMemory(pbBuff, @EditStreamCallBackData[EditStreamCallBackPos], pcb);
              EditStreamCallBackPos := EditStreamCallBackPos + pcb;
              Result := ERROR_SUCCESS;
            except
              Result := ERROR_CANNOT_COPY;
            end;
          end
          else
          begin
            Result := ERROR_INSUFFICIENT_BUFFER;
          end;
        end
        else
        begin
          Result := ERROR_NO_DATA;
        end;
      end;
    else
      Result := ERROR_INVALID_PARAMETER;
  end;
end; { EditStreamCallback }

// 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;

{------------------------------------------------------------------------------}

function StreamOutRtf(const RichEdit: HWND; out Stream: PChar; const Sel: boolean): Cardinal;
var
  EditStream: TEditStream;
begin
  if (RichEdit <> 0) and IsWindow(RichEdit) then
  begin
    if (EditStreamCallBackData = nil) then
      try
        EditStream.dwCookie := EditStreamCookieDoOut;
        EditStream.dwError := ERROR_NO_DATA;
        EditStream.pfnCallback := EditStreamCallBack;
        if Sel then
          SendMessage(RichEdit, EM_STREAMOUT, SF_RTF or SFF_SELECTION, lParam(@EditStream))
        else
          SendMessage(RichEdit, EM_STREAMOUT, SF_RTF, lParam(@EditStream));
        Result := EditStream.dwError;
        //RaiseLastWin32Error;
        if Result <> ERROR_SUCCESS then
        begin
          if EditStreamCallBackData <> nil then
            FreeMemory(EditStreamCallBackData);
        end
        else
        begin
          Stream := GetMemory(EditStreamCallBackPos + 1);
          if Stream <> nil then
            try
              ZeroMemory(Stream, EditStreamCallBackPos + 1);
              CopyMemory(Stream, EditStreamCallBackData, EditStreamCallBackPos);
            except
              FreeMemory(Stream);
              Stream := nil;
              Result := ERROR_CANNOT_COPY;
            end 
          else 
          begin 
            Result := ERROR_NOT_ENOUGH_MEMORY; 
          end; 
          if Result <> ERROR_SUCCESS then 
            FreeMemory(EditStreamCallBackData); 
        end; 
      finally
        EditStreamCallBackData := nil;
        EditStreamCallBackPos  := 0;
      end
    else
    begin
      Result := ERROR_NOT_READY;
    end;
  end
  else
  begin
    Result := ERROR_INVALID_PARAMETER;
  end;
end; { StreamOutRtf }

function StreamInRtf(const RichEdit: HWND; const Stream: PChar): Cardinal;
var
  EditStream: TEditStream;
begin
  if (RichEdit <> 0) and IsWindow(RichEdit) and (Stream <> nil) then
  begin
    if (EditStreamCallBackData = nil) then
      try
        EditStreamCallBackData := Stream;
        EditStreamCallBackPos := 0;
        EditStream.dwCookie := EditStreamCookieDoIn;
        EditStream.dwError := ERROR_NO_DATA;
        EditStream.pfnCallback := EditStreamCallBack;
        SendMessage(RichEdit, EM_STREAMIN, SF_RTF, lParam(@EditStream));
        Result := EditStream.dwError;
      finally
        EditStreamCallBackData := nil;
        EditStreamCallBackPos  := 0;
      end
    else
    begin
      Result := ERROR_NOT_READY;
    end;
  end
  else
  begin
    Result := ERROR_INVALID_PARAMETER;
  end;
end; { StreamInRtf }

end.


Gruß Sven

_________________
MDK 9.1, Kernel 2.4.21, KDE 3.1 Kylix 3 Pro (patched), nutze aber auch Windows