我創建了一個讀取文本檔案的小程式。
在 RichEdit 中打開文本檔案后,我想更改包含某個字串的行的背景顏色,或者隱藏所有不包含該字串的行。是否可以?
我試圖搜索字串,但我不知道如何做我所要求的。
function SearchText(Control: TCustomEdit; Search: string; SearchOptions: TSearchOptions): Boolean;
var
Text: string;
Index: Integer;
begin
if soIgnoreCase in SearchOptions then
begin
Search := UpperCase(Search);
Text := UpperCase(Control.Text);
end
else
Text := Control.Text;
Index := 0;
if not (soFromStart in SearchOptions) then
Index := PosEx(Search, Text, Control.SelStart Control.SelLength 1);
if (Index = 0) and
((soFromStart in SearchOptions) or
(soWrap in SearchOptions)) then
Index := PosEx(Search, Text, 1);
Result := Index > 0;
if Result then
begin
Control.SelStart := Index - 1;
Control.SelLength := Length(Search);
end;
end;
uj5u.com熱心網友回復:
最好在將文本放入 RichEdit之前對其進行搜索和過濾。
但是,如果文本已加載到 RichEdit 中,TRichEdit并且確實有FindText()可以使用的方法,則不應Text手動搜索其屬性。例如:
function SearchText(Control: TCustomRichEdit; const Search: string; SearchOptions: TSearchOptions): Boolean;
var
StartPos, SearchLen, Index: Integer;
Options: TSearchTypes;
begin
if soIgnoreCase in SearchOptions then
Options := []
else
Options := [stMatchCase];
if soFromStart in SearchOptions then
begin
StartPos := 0;
SearchLen := Control.GetTextLen;
Index := Control.FindText(Search, StartPos, SearchLen, Options);
end else
begin
StartPos := Control.SelStart Control.SelLength;
SearchLen := Control.GetTextLen - StartPos;
Index := Control.FindText(Search, StartPos, SearchLen, Options);
if (Index = -1) and (soWrap in SearchOptions) then
Index := Control.FindText(Search, 0, StartPos, Options);
end;
Result := Index <> -1;
if Result then
begin
Control.SelStart := Index;
Control.SelLength := Length(Search);
end;
end;
話雖如此,設定線條的背景顏色或洗掉線條(沒有“隱藏”線條的選項)相當簡單。
給定任何字符索引,您可以向 RichEdit 發送EM_LINEFROMCHAR訊息以確定字符出現的行索引。
然后,您可以使用該TRichEdit.Lines.Delete()方法從 RichEdit 中洗掉該行。
設定線條的背景顏色需要幾個步驟:
發送 RichEdit
EM_LINEINDEX和EM_LINELENGTH訊息以確定行的開始和結束字符索引。設定 RichEdit 的
SelStart和SelLength屬性(或向 RichEdit 發送EM_EXSETSEL訊息)。向 RichEdit 發送一條
EM_SETCHARFORMAT訊息,指定 SCF_SELECTION 標志并使用CHARFORMAT2記錄來設定選擇的背景顏色。
uj5u.com熱心網友回復:
這是一個糟糕的 SO 問題,因為它有點像“請為我撰寫代碼”。
自然的方法是找到問題的獨立部分:
如何在 Delphi 中表示字串(行)陣列?
如何將 Delphi 中的文本檔案加載到一些記憶體中的字串陣列中?
如何在Delphi中搜索字串中的子字串?
如何過濾 Delphi 記憶體中的字串陣列?[如果你知道 1 并且聽說過回圈,這很簡單。有效地執行它會更有趣。]
如何填充 Delphi
TRichEdit控制元件?
確實,如果您知道 1--5 的答案,那么做您想做的事就是微不足道的!
我現在可能看起來像一個脾氣暴躁的老人,但我認為我確實有一個非常重要的觀點,即如何解決編程問題。
無論如何,讓我們一次解決一個問題:
array of string今天的書面TArray<string>作品的老派方法。這是一個動態的字串陣列。由于 Delphi 動態陣列是由編譯器管理的,所以它們很方便,因為您不需要手動創建和釋放它們。但是,它們有點低級,有時會被濫用。可能對您來說更好的選擇是使用
TStringList該類。在
IOUtils中,您會發現TFile.ReadAllLineswhich 采用檔案名并將(文本)檔案的內容作為字串陣列回傳。或者
TStringList.LoadFromFile,如果您有TStringList.傳統上,您將使用該
Pos功能。但是今天你可以使用stringhelper:MyString.Contains()。顯然,您需要決定是否要將大寫字母和小寫字母視為相同。根據 3 中的測驗,使用瑣碎
for或for in回圈從原始陣列填充第二個陣列。如果你有
TStringList,只需使用TRichEdit.Lines.Assign.
將它們放在一起,使用相當聰明的string陣列組合和TStringList:
procedure TForm1.Button1Click(Sender: TObject);
begin
var Lines := TFile.ReadAllLines('K:\test.txt');
var FilteredLines := TStringList.Create;
try
for var Line in Lines do
if Line.Contains('MyString') then
FilteredLines.Add(Line);
RichEdit1.Lines.Assign(FilteredLines);
finally
FilteredLines.Free;
end;
end;
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/424532.html
