如何在鼠标单击时更改 MSHTML 中插入符号的位置?
我在应用程序中嵌入了一个基于 MSHTML 的控件,ContentEditable 模式用于编辑其中的文档。 HTML 文档的正文最初包含以下几行:
<div></div>
<div id="signature"></div>
插入符号放置在文档的开头,即第一个 DIV 元素内。现在,当用户用鼠标单击控件内最后一行下方的位置时,插入符号将移动到第二个 DIV 元素中,因为它是距离用户单击点最近的元素。
我想在单击鼠标时将指针移动到第一个 DIV 的末尾。现在我有了计算插入符的预期位置的代码,即 IMarkupPointer
和 IDisplayPointer
。我需要知道的是在哪里拦截 MSHTML 事件管道以执行实际的插入符移动。
我编写了实现 IHTMLEditDesigner 的代码,并使用 IHTMLCaret.MoveCaretToPointer 将插入符号移动到预期位置。问题是,无论我在哪里拦截事件(PreHandleEvent、PostHandleEvent 或 PostEditorEventNotify),插入符号位置最终都会在单击时恢复为默认位置(但如果我按住鼠标左键一段时间,或者如果我用鼠标右键单击)。
I have a MSHTML-based control embedded in an application and the ContentEditable mode is used to edit documents inside it. The body of the HTML document initially contains the following lines:
<div></div>
<div id="signature"></div>
The caret is placed at the beginning of the document, that is inside the first DIV element. Now when user clicks with mouse inside the control in a place below the last line, the caret is moved into the second DIV element, as it's the closest one to the point where the user clicked.
I want to move the pointer to the end of the first DIV on the mouse click. Now I have the code to calculate the intended position of the caret as IMarkupPointer
and IDisplayPointer
. What I need to know is where to intercept the MSHTML event pipeline to do the actual caret move.
I've written code that implements IHTMLEditDesigner
and moves the caret using IHTMLCaret.MoveCaretToPointer
to the intended position. The problem is that no matter where I intercept the event (PreHandleEvent, PostHandleEvent or PostEditorEventNotify) the caret position is eventually reverted to the default one on single click (but it is not reverted if I hold the left mouse button pressed for a while or if I click with right mouse button).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在加载到 IE 控件中的 HTML 中使用 jscript。如果您不太了解 HTML 和 jscript,您会发现这项任务非常痛苦。
当我尝试这样做时,请参阅这些问题以了解我的经验。
IE
从 div 中删除“ID”
我还遇到了很多其他问题,包括必须在 jscript 中编写调整大小逻辑,以使 HTML 编辑器与 WinForm 表单一起调整大小,以及必须将默认表单/控制颜色传递到HTML 编辑器使其看起来很漂亮,然后用户更改了 Windows 上的配色方案。
更好的是找到一个 HTML 编辑器并将其加载到 IE 控件中,您仍然需要自己使用标准窗口颜色等进行编码。
您还可以使用第 3 方 winforms HTML 编辑器。如果可能的话,我认为您应该购买解决方案,因为 ContentEditable 在现实生活中比看起来要困难得多。
谷歌很快就找到了。
Use jscript inside of the HTML that you load into the IE control. If you do not know HTML and jscript very well you will find this task very painful.
See these questions for my experience when I tried do so something like this.
IE
removing “ID” from div
I also had lots of other problem, including have to write resize logic in jscript to get the HTML editor to size along with the WinForm form and having to pass the default form/coontrol colours into the HTML editor so that it looked write then users changed colour schemes on Windows.
Even better just find a HTML editor and load it into the IE control, you will still have to code with standard window colours etc yourself.
There are also 3rd party winforms HTML editors you can use. If possible I think you should buy in a solution as ContentEditable is a lot harder in real life then it look.
A quick google found.
您是否尝试过使用超时为 0 的 winforms 计时器?
使用 Spy++ 查看 MSHTML 控件中不同窗口之间发送的事件,以获得其他理想结果。 MSHTML 控件与其他 winforms 控件不同,您必须回到 C 和 Win32 编程时代使用的所有技巧。
Have you tried using a winforms timer with a timeout of 0?
Have a look with Spy++ to see what events are being sent between the diffent windows in the MSHTML control to get other ideals. The MSHTML control is like no other winforms control and you have to go back to all the trick you used in the days of C and Win32 programming.
也许 mouseDown mouseUp 和 mouseClick 有单独的事件。
您拦截 mouseClick,但在 mouseUp 上执行默认行为。
Maybe there are separate events for mouseDown mouseUp and mouseClick.
You intercept mouseClick but default behavior gets executed on mouseUp.
您是否通过查找第一个 div 的 dom 项并调用 setFocus (或任何名称) dom 方法将“焦点”设置为第一个 div?插入符应该移动到焦点所在的位置。
(MSHTML 公开了一些接口来查找 dom 项并调用它们的方法。抱歉,我不记得如何执行此操作的详细信息)
Have you tied setting the "focus" to the first div by finding the dom item for it, and calling the setFocus (or whatever it is called) dom method? The caret should move to where the fosus is.
(There are interfaces that MSHTML expose to find dom items and call methods on them. Sorry I don't recall the details of how to do this)
我相信您需要更改 SelectStart 属性并保留 SelectionLength = 0。这会将插入符号移动到新位置。
I believe you need to change the SelectStart property and leave the SelectionLength = 0. That will move the caret to a new position.