丰富编辑控件:防止丰富格式?
如何防止用户更改 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个答案可能有点晚了,但对于寻找这个问题答案的其他人来说,我发现在富编辑控件中完全控制粘贴操作的最佳方法是提供
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.
即使后来:)
似乎也能做到这一点:粘贴粘贴纯文本,并且格式化热键被禁用。
您仍然保留了 richedit 的一些“额外”功能,例如按需滚动条。
注意:虽然这会阻止将格式丰富的文本粘贴到 RichEdit 控件中,但它也会阻止您以编程方式设置文本格式;所有丰富的格式都被禁用。
Even later :)
seems to do the trick: paste pastes plain text, and the formatting hot keys are disabled.
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.
我从来没有找到一种特别优雅的方法来处理这个问题:我过去所做的是:
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.