丰富编辑控件:防止丰富格式?

发布于 2024-11-08 21:31:21 字数 202 浏览 0 评论 0原文

如何防止用户更改 Win32 Rich Edit 控件?

(即他们不应该能够更改任何文本的格式、添加图形等;如果他们复制粘贴新文本,则应仅保留文本,并应丢弃相关的格式。)

How do you prevent the user from changing anything other than the text in a Win32 Rich Edit control?

(i.e. They shouldn't be able to change the formatting of any text, add graphics, etc.; if they copy-paste new text, only the text should be kept, and associated formatting should be discarded.)

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

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

发布评论

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

评论(3

﹉夏雨初晴づ 2024-11-15 21:31:22

这个答案可能有点晚了,但对于寻找这个问题答案的其他人来说,我发现在富编辑控件中完全控制粘贴操作的最佳方法是提供 IRichEditOleCallback::QueryAcceptData,然后返回 S_FALSE 以将它们全部停止或通过更改 lpcfFormat 参数过滤掉某些剪贴板格式。

MFC 中的 CRichEditView::QueryAcceptData 函数提供了一个很好的示例来说明如何完成此操作。这适用于各种粘贴操作,包括拖放,因此是完全控制所发生情况的最佳方法。

This answer is probably a bit late but for anybody else looking for an answer to this questions, the best way that I've found have total control of paste operations in a Rich edit control is to provide an implementation of IRichEditOleCallback::QueryAcceptData and then return S_FALSE to stop them all together or to filter out certain clip board formats by changing the lpcfFormat parameter.

The CRichEditView::QueryAcceptData function in MFC provides an excellent example of how this can be done. This will work for all kinds of paste operations including drag and drop so is the best way to get full control of what happens.

睡美人的小仙女 2024-11-15 21:31:22

即使后来:)

SendMessage(wndEdit, EM_SETEDITSTYLE, SES_EMULATESYSEDIT, SES_EMULATESYSEDIT)

似乎也能做到这一点:粘贴粘贴纯文本,并且格式化热键被禁用。

SES_EMULATESYSEDIT:当该位打开时,Rich Edit 会尝试模拟系统编辑控件(默认值:0)。

您仍然保留了 richedit 的一些“额外”功能,例如按需滚动条。

注意:虽然这会阻止将格式丰富的文本粘贴到 RichEdit 控件中,但它也会阻止您以编程方式设置文本格式;所有丰富的格式都被禁用。

Even later :)

SendMessage(wndEdit, EM_SETEDITSTYLE, SES_EMULATESYSEDIT, SES_EMULATESYSEDIT)

seems to do the trick: paste pastes plain text, and the formatting hot keys are disabled.

SES_EMULATESYSEDIT: When this bit is on, rich edit attempts to emulate the system edit control (default: 0).

You still retain some of the "bonus" features of the richedit, such as scrollbars on demand.

Note: While this will prevent the pasting of richly formatted text into the RichEdit control, it will also prevent you from programatically formatting the text; all rich formatting is disabled.

享受孤独 2024-11-15 21:31:21

我从来没有找到一种特别优雅的方法来处理这个问题:我过去所做的是:

1)捕获控件的 WM_KEYDOWN 消息并丢弃所有格式化键(Ctrl + E,J,R,L,1,2 ,5,+, 和 Ctrl+Shift+A,7)

2) 通过捕获 ID 为 ID_EDIT_PASTE 的 WM_COMMAND 消息来捕获所有粘贴操作,并将粘贴消息替换为消息EM_PASTESPECIAL,CF_UNICODETEXT 到控件。 (这是使用 MFC 的情况:根据您使用的框架或语言,这可能需要捕获 Ctrl+V 和类似的命令,而不是 ID_EDIT_PASTE。)

我承认,这不太漂亮,但它似乎可以工作。

I've never found a particularly elegant way to handle this: what I've done in the past is:

1) Catch WM_KEYDOWN messages for the control and discard all formatting keys (Ctrl+E,J,R,L,1,2,5,+, and Ctrl+Shift+A,7)

2) Catch all paste operations by catching WM_COMMAND messages with an id of ID_EDIT_PASTE, and replace the paste message with a message EM_PASTESPECIAL,CF_UNICODETEXT to the control. (This is with MFC: Depending on what framework or language you're using, this may require catching Ctrl+V and similar rather than ID_EDIT_PASTE.)

Not pretty, I conceed, but it seems to work.

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