TMemo无法正确处理Unix文本(LF为行尾) [英] TMemo cannot handle Unix text (LF as line ending) correctly

查看:463
本文介绍了TMemo无法正确处理Unix文本(LF为行尾)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TMemo无法正确处理Unix输入(LF)。用LF分隔的两条线显示为一行。我想处理所有可能的文本格式(Mac,Win,Unix)。

TMemo cannot handle Unix enters (LF) correctly. Two lines separated with a LF are shown and treated as one line. I want to handle all possible text formating (Mac, Win, Unix).

显然,我可以检查文字,每次我使用CRLF替换LF:

Obviously I could check the text and replace the LF with CRLF every time I:


  • 加载文本格式文件

  • 粘贴文本

  • 使用Add()函数

  • 使用Appen()函数


  • 通过Text属性更改内容

  • load text form file
  • paste text
  • use the Add() function
  • use the Insert() function
  • use the Appen() function
  • change the content via Text property

但这不会是一个优雅的解决方案。

But this won't be an elegant solution.

Lazarus用Lines.TextLineBreakStyle属性解决了这个问题。在Delphi XE中有类似的东西?

Lazarus solved this problem with the Lines.TextLineBreakStyle property. There is anything similar in Delphi XE?

推荐答案

您正在专门询问有关线条序列,但是我们可以轻松扩展讨论控制一般编辑框的内容。

You're asking specifically about line-ending sequences, but we can easily broaden the discussion to controlling the contents of an edit box in general.

2001年,Peter Below写道:您只需接受数字的编辑控件所需处理的所有内容的大纲。这些技术今天仍然适用,至少对于Windows开发。以下是一个摘要:

In 2001, Peter Below wrote an outline of all the things you need to handle for an edit control that accepts only numerals. The techniques are still applicable today, at least for Windows development. Here's a summary:


  • 覆盖 KeyPress 以过滤不需要的按键。 (在你的情况下,你不需要这个,因为你不想排除任何。)

  • 处理 wm_Paste 消息以记录从剪贴板粘贴的文本。

  • 处理 wm_SetText 消息以记录文本以大多数其他常规方式设置。 (这大部分是你的列表: LoadFromFile 添加追加插入 Text:= ... 。)

  • 处理 em_ReplaceSel 消息以考虑被覆盖的所选文字。

  • Override KeyPress to filter out unwanted keystrokes. (In your case, you don't need this because there aren't any keys you want to exclude.)
  • Handle the wm_Paste message to account for text pasted from the clipboard.
  • Handle the wm_SetText message to account for text being set in most other conventional ways. (This takes care of most of your list: LoadFromFile, Add, Append, Insert, Text := ....)
  • Handle the em_ReplaceSel message to account for selected text being overwritten.

在下面的写作中,他只是拒绝任何不合格的输入。这可能不是你想要的,但是。相反,您需要规范化输入,以便使用统一的行结束序列。而不是只是吞下上面列出的文本更改消息,您将需要按下输入,然后将其转发到正常的处理程序。

In Below's write-up, he simply rejects any non-conforming input. That's probably not what you want, though. Instead, you'll want to normalize the input so that it uses uniform line-ending sequences. Rather than just swallow the text-changing messages listed above, you'll want to massage the input and then forward it to the normal handler.

为了控制行尾,Delphi已经提供了一种按您需要的按摩功能: AdjustLineBreaks 。您可以选择是否要使用Unix或Windows样式的行尾。 (请注意,即使Macintosh不再使用Mac风格的线条结尾。)您可能想要 tlbsCRLF ,因为这是Windows编辑控件知道如何显示的样式;如你所说,如果没有回车,它不会显示换行符。

For controlling line endings, Delphi already provides a function that does the kind of massaging you want: AdjustLineBreaks. You can pick whether you want Unix- or Windows-style line endings. (Note that not even Macintosh uses Mac-style line endings anymore.) You probably want tlbsCRLF because that's the style the Windows edit control knows how to display; as you've noted, it doesn't display line breaks if there aren't any carriage returns.

对于 wm_SetText em_ReplaceSel ,更改很容易。这是一个草图:

For wm_SetText and em_ReplaceSel, the change is easy. Here's a sketch:

procedure TLineNormalizingMemo.WMSetText(var Message: TWMSetText);
var
  s: string;
begin
  s := Message.Text;
  s := AdjustLineBreaks(s);
  Message.Text := PChar(s);
  inherited;
end;

对于 wm_Paste 不太明显,因为你没有被赋予新的文本,你不应该改变当前在剪贴板上的文本。相反,您可以选择不调用继承,来处理粘贴自己的效果。这样做:

It's not quite so obvious for wm_Paste because you're not given the new text, and you're not supposed to change the text that's currently on the clipboard. Instead, you can opt not to call inherited, handling the effects of pasting yourself. Something like this:

procedure TLineNormalizingMemo.WMPaste(var Message: TWMPaste);
var
  s: string;
begin
  if Clipboard.HasFormat(cf_Text) then begin
    s := Clipboard.AsText;
    s := AdjustLineBreaks(s);
    SelText := s;
  end;
end;

由于分配 SelText c $ c> em_ReplaceSel 消息,您可能甚至不需要调用 AdjustLineBreaks

Since assigning SelText goes through the em_ReplaceSel message, you might not even need to call AdjustLineBreaks there.

这篇关于TMemo无法正确处理Unix文本(LF为行尾)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆