在RealStudio中,如何拦截富文本的粘贴?

发布于 2024-12-17 09:58:59 字数 306 浏览 2 评论 0原文

我正在尝试创建一个简单的文本字段以进行所见即所得编辑。但是,我只想允许某些类型的格式(例如粗体、斜体、下划线和单个标题类型,但没有颜色或不同的字体。)

问题是,如果我使用可以接受格式的编辑器,有人可以创建或复制格式化在另一个程序中输入文本,然后只需将其粘贴到文本字段中,所有格式都会随之而来,允许我不感兴趣的内容,例如不同的字体、颜色等。我不想允许这样做。

充其量,我想自动删除任何我不支持的格式。最糟糕的是,我想简单地将任何内容粘贴为纯文本,使他们必须重新格式化它。但在任何情况下,我都不想将剪贴板转储到文本区域。

关于如何做到这一点有什么想法吗?

I'm trying to create a simple text field for WYSIWYG editing. However, I only want to allow certain types of formatting (e.g. Bold, Italic, Underline and a single heading type but no colors or different fonts.)

The issue is if I use an editor that can accept formatting, someone can create or copy formatted text in another program, then simply paste it into the text field and all that formatting goes with it, allowing things I'm not interested in, such as different fonts, colors, etc. I don't want to allow that.

At best, I want to automatically strip out any formatting that I don't support. At worst, I want to simply paste whatever as plain text making them have to reformat it. But in no case do I want to just dump the clipboard to the text area.

Any thoughts on how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不再见 2024-12-24 09:58:59

我建议创建一个新的文本字段/文本区域类并创建一个 EditPaste 菜单处理程序,该处理程序 (a) 执行您在处理剪贴板文本方面所需的操作,(b) 返回 true 以防止发生默认粘贴。这比使用按键事件更安全,因为用户可以从编辑菜单中手动选择粘贴。

您可以通过创建 Clipboard 对象来访问剪贴板上的文本。

要对文本字段进行子类化并拦截粘贴菜单命令:

  1. 打开项目后,转到“项目菜单”>“粘贴菜单”。添加>类
  2. 在项目选项卡中选择新类,然后在属性面板中将超级设置为 TextField
  3. 双击该类进行编辑 单击
  4. 类中工具栏中间的“添加菜单处理程序”按钮
  5. 将菜单项名称更改为“EditPaste” ”。将代码放在“return true”之前,并确保将 return true 保留在那里。

然后,您的代码可以手动格式化和粘贴文本并覆盖默认的粘贴功能。

该文本字段中的任何 command-V 或 control-V 都会导致该菜单处理程序触发。无论如何,您都会添加任何上下文菜单,因为真正的基本不会创建默认的上下文菜单,因此您也可以控制它。

要将文本字段添加到窗口,只需将对象列表上方的过滤器更改为项目控件,然后将类从那里拖入即可。

I would recommend creating a new text field/text area class and creating an EditPaste menu handler that (a) does what you're looking for in terms of handling the clipboard's text and (b) returns true to prevent the default pasting from happening. This is safer than using the Key down events because the user might manually select paste from the edit menu.

You can access the text on the clipboard by creating a Clipboard object.

To subclass the textfield and intercept the paste menu command:

  1. With your Project open, go to Project Menu > Add > Class
  2. Select the new class in the project's tab and in the properties panel set the super to TextField
  3. Double-click on the class to edit it
  4. Click the "Add Menu Handler" mid-toolbar button in your class
  5. Change the menu item name to "EditPaste". Put your code in before the "return true" and be sure to leave the return true in there.

Your code can then format and paste the text manually and override the default paste function.

Any command-V or control-V in that text field will cause that menu handler to fire. Any contextual menus would be added by you anyway since real basic does not create the default contextual menus, so you'd have control over that as well.

To add the text field to a window, just change the filter above the objects list to Project controls, and drag the class in from there.

雨巷深深 2024-12-24 09:58:59

您可以通过在 KeyDown 事件中拦截粘贴来自行拦截粘贴。然后,您可以自己解析它。这可能有点棘手,但我认为这是唯一的方法。

粘贴后解析生成的 StyleRun 可能会更容易,并删除您不需要的格式。

或者,您可以查看 True North Software 的格式化文本控件,并覆盖该控件的粘贴方法(您获得所有源代码),然后自己处理它。

不管怎样,我认为这将是一项相当大的工作量。

You could intercept the paste yourself by intercepting it in the KeyDown events. Then, you could look to parse it yourself. That could be kind of tricky but I think that's about the only way you could do it.

It might just be easier to parse the resulting StyleRun after the paste and strip out formatting you don't want.

Alternately, you could look at the Formatted Text Control from True North Software and override the paste methods of the control (you get all the source) and just handle it yourself.

Either way, I think it will be a fair amount of work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文